KineticJS .toJSON() メソッドを使用して JSON オブジェクト リテラルとして保存したレイヤーを読み込もうとしました (レイヤーでの試行をあきらめ、ステージでの試行を開始しました)。スクリプトをデバッグしてきましたが、Kinetic.Node.create が呼び出されるまでは問題ないようです。これが私のコードです:
var stage = new Kinetic.Stage({
container: 'container',
width: 1000,
height: 650
});
var check;
$(document).on({
click: function(){
check = stage.toJSON();
}
},'#save');
$(document).on({
click: function(){
try{
//parse check string into object literal
var s = JSON.parse(check);
//check if s is object literal
if( Object.prototype.toString.call(s) === '[object Object]' ) {
//this is where the code stops executing
stage = Kinetic.Node.create(s,'container');
stage.draw();
}
}
catch(e){
console.debug(e.stack);
console.trace();
}
}
},'#load');
私のHTML:
<div id="container">
</div>
<div id="buttons">
<button id="save">
Save
</button>
<button id="load">
Load
</button>
</div>
そしてエラーログ:
SyntaxError: Unexpected token o
at Object.parse (native)
at Function.Kinetic.Node.create (http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js:2:25166)
at HTMLButtonElement.$.on.click (http://localhost/mosaicos/main.js:181:38)
at HTMLDocument.b.event.dispatch (http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:3:28337)
at HTMLDocument.v.handle (http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:3:25042)
私が何か間違っているかどうかわからないので、どんな助けでも大歓迎です。