2013-05-02 61 views
0

我總是得到錯誤:Kineticjs負載JSON帆布

「類型錯誤:動力學【類型】是不是構造」
「的ReferenceError:引用未定義的屬性obj.nodeType」

當我嘗試加載一個json。

json = stage.toJSON(); 

stage = Kinetic.Node.create(json, 'myCanvas'); 

方法_createNode從Kineticjs 4.3.3

var no = new Kinetic[type](obj.attrs); 
在我的畫布

我有一個簡單的組

var circle1 = new Kinetic.Circle({ 
       x: 40, 
       y: 50, 
       radius: 42, 
       fill: 'white', 
       stroke: 'black', 
       strokeWidth: 1, 
       draggable: false 
      }); 

var polygon1Tab1 = new Kinetic.RegularPolygon({ 
         x: 40, 
         y: 50, 
         radius: 27, 
         sides: 4, 
         stroke: 'black', 
         strokeWidth: 4, 
         draggable: false 
        }); 
        polygon1Tab1.rotateDeg(45); 


var group1 = new Kinetic.Group({ 
            draggable: true, 
           }); 

group1.add(circle1.clone()); 
group1.add(polygon1.clone()); 

回答

0

'polygon1'這只是一個名字......一個變化!!!! 我把你的代碼和地方運行,並繼續相同的錯誤! 當我打印json時我發現它有錯誤!

他把這個"children": "[]",不應該有括號內引號 如果我複製並把我的字符串這個固定的字符串糾正輪正常! 我做了一個方法來刪除錯誤....現在我有一個錯誤

SyntaxError: JSON.parse: expected property name or '}' 


var json = polygonLayer.toJSON(); 
json = json.replace('"children":"', '"children":'); 
json = json.substring(0, json.length-2) + json.substring(json.length-1, json.length); 

,而使用相同的固定字符串的作品!

我使用的版本4.3.3

這項工作

var json = '{"attrs":{"clearBeforeDraw":true,"visible":true,"listening":true,"opacity":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"offset":{"x":0,"y":0},"draggable":false,"dragOnTop":true},"nodeType":"Layer","children":[{\"attrs\": {\"width\": 600, \"height\": 400, \"cornerRadius\": 0, \"fillEnabled\": true, \"strokeEnabled\": true, \"shadowEnabled\": true, \"dashArrayEnabled\": true, \"fillPriority\": \"color\", \"visible\": true, \"listening\": true, \"opacity\": 1, \"x\": 0, \"y\": 0, \"scale\": {\"x\": 1, \"y\": 1}, \"rotation\": 0, \"offset\": {\"x\": 0, \"y\": 0}, \"draggable\": false, \"dragOnTop\": true, \"fill\": \"white\"}, \"nodeType\": \"Shape\", \"shapeType\": \"Rect\"}]}'; 
layer = Kinetic.Node.create(json, 'canvas'); 

但這不工作

var json = layer.toJSON(); 
layer = Kinetic.Node.create(json, 'canvas');