fabricjs serialize problem
by ps7566
HTML
<!-- <button onClick="printObj()">Print rect</button> -->
<!-- <button onClick="addChild()">Add Child</button> -->
<button onClick="serialize()">Serialize</button>
<button onClick="loadJSON()">Load JSON</button>
<!-- <button onClick="canvas.remove(r)">Delete Rect</button> -->
<canvas id="c"></canvas>
JavaScript
var canvas = new fabric.Canvas("c",{
width: 600,
height: 500,
backgroundColor: 'cyan'
});
var a = new fabric.Rect({
left: 70,
top: 70,
fill: 'blue',
width: 100,
height: 100
});
var b = new fabric.Rect({
left: 75,
top: 75,
fill: 'red',
width: 50,
height: 50
});
b.another = 'property';//this custom property is losed in event observer after serialize;
a.childs = [];
a.childs.push(b);
a.childs.push(b);
canvas.add(a);
//function printObj(){
// console.log(r);
//}
//function addChild(){
// r.childs.push(s);
//}
var json;
function serialize(){
json = JSON.stringify(canvas.toJSON(['childs','another']));
}
function loadJSON(){
canvas.loadFromJSON(json);
}
canvas.observe("mouse:up",function(e){
if(e.target != null){
console.log(e.target);
alert(e.target.childs[0].another);//why this is undefined after serialize and load json?
}
});
//The project has to be programmed in this way. with an array, event observer and custom properties. can not it be a different way.