finished custom shape serialization with new attrs structure

This commit is contained in:
Eric Rowell 2012-04-07 15:03:19 -07:00
parent 58eb778e9a
commit 1a40e50622
5 changed files with 67 additions and 68 deletions

View File

@ -1121,6 +1121,7 @@ Kinetic.Stage.prototype = {
function addNode(node) { function addNode(node) {
var obj = {}; var obj = {};
obj.attrs = node.attrs; obj.attrs = node.attrs;
obj.nodeType = node.nodeType; obj.nodeType = node.nodeType;
obj.shapeType = node.shapeType; obj.shapeType = node.shapeType;
@ -1145,7 +1146,7 @@ Kinetic.Stage.prototype = {
function loadNode(node, obj) { function loadNode(node, obj) {
// if custom shape then set draw function // if custom shape then set draw function
if(obj.nodeType === 'Shape' && obj.shapeType === undefined) { if(obj.nodeType === 'Shape' && obj.shapeType === undefined) {
node.drawFunc = drawFuncs[obj.drawFuncName]; node.drawFunc = drawFuncs[obj.attrs.drawFuncName];
} }
var children = obj.children; var children = obj.children;
@ -1928,7 +1929,6 @@ Kinetic.Shape = function(config) {
this.attrs.strokeWidth = undefined; this.attrs.strokeWidth = undefined;
this.attrs.lineJoin = undefined; this.attrs.lineJoin = undefined;
this.attrs.detectionType = 'path'; this.attrs.detectionType = 'path';
this.attrs.drawFuncName = undefined;
// special // special
this.drawFunc = config.drawFunc; this.drawFunc = config.drawFunc;

File diff suppressed because one or more lines are too long

View File

@ -25,7 +25,6 @@ Kinetic.Shape = function(config) {
this.attrs.strokeWidth = undefined; this.attrs.strokeWidth = undefined;
this.attrs.lineJoin = undefined; this.attrs.lineJoin = undefined;
this.attrs.detectionType = 'path'; this.attrs.detectionType = 'path';
this.attrs.drawFuncName = undefined;
// special // special
this.drawFunc = config.drawFunc; this.drawFunc = config.drawFunc;

View File

@ -191,6 +191,7 @@ Kinetic.Stage.prototype = {
function addNode(node) { function addNode(node) {
var obj = {}; var obj = {};
obj.attrs = node.attrs; obj.attrs = node.attrs;
obj.nodeType = node.nodeType; obj.nodeType = node.nodeType;
obj.shapeType = node.shapeType; obj.shapeType = node.shapeType;
@ -215,7 +216,7 @@ Kinetic.Stage.prototype = {
function loadNode(node, obj) { function loadNode(node, obj) {
// if custom shape then set draw function // if custom shape then set draw function
if(obj.nodeType === 'Shape' && obj.shapeType === undefined) { if(obj.nodeType === 'Shape' && obj.shapeType === undefined) {
node.drawFunc = drawFuncs[obj.drawFuncName]; node.drawFunc = drawFuncs[obj.attrs.drawFuncName];
} }
var children = obj.children; var children = obj.children;

View File

@ -94,7 +94,7 @@ Test.prototype.tests = {
var expectedJson = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"radius":70,"fill":"green","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"name":"myCircle","alpha":1,"x":289,"y":100,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":true},"nodeType":"Shape","shapeType":"Circle"}]}]}]}'; var expectedJson = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"radius":70,"fill":"green","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"name":"myCircle","alpha":1,"x":289,"y":100,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":true},"nodeType":"Shape","shapeType":"Circle"}]}]}]}';
test(stage.toJSON() === expectedJson, 'problem with serialization'); test(stage.toJSON() === expectedJson, 'problem with serialization');
}, },
'STAGE - load stage with json': function(containerId) { 'STAGE - load stage using json': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,
width: 578, width: 578,
@ -106,7 +106,6 @@ Test.prototype.tests = {
test(stage.toJSON() === json, "serialized stage is incorrect"); test(stage.toJSON() === json, "serialized stage is incorrect");
}, },
/*
'STAGE - serialize stage with custom shape': function(containerId) { 'STAGE - serialize stage with custom shape': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,
@ -138,12 +137,13 @@ Test.prototype.tests = {
group.add(triangle); group.add(triangle);
layer.draw(); layer.draw();
var expectedJson = '{"nodeType":"Stage","width":578,"height":200,"scale":{"x":1,"y":1},"visible":true,"_listening":true,"alpha":1,"x":0,"y":0,"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"_draggable":false,"_children":[{"nodeType":"Layer","visible":true,"_listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"_draggable":false,"_children":[{"nodeType":"Group","visible":true,"_listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"_draggable":false,"_children":[{"nodeType":"Shape","detectionType":"path","visible":true,"_listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"_draggable":false,"drawFuncName":"drawTriangle","fill":"#00D2FF","stroke":"black","strokeWidth":4}]}]}]}'; //console.log(stage.toJSON());
var expectedJson = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"fill":"#00D2FF","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false,"drawFuncName":"drawTriangle"},"nodeType":"Shape"}]}]}]}';
test(stage.toJSON() === expectedJson, "problem with serialization"); test(stage.toJSON() === expectedJson, "problem with serialization");
console.log(stage.toJSON());
}, },
'STAGE - load stage with custom shape': function(containerId) { 'STAGE - load stage with custom shape using json': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,
width: 578, width: 578,
@ -159,14 +159,15 @@ Test.prototype.tests = {
context.closePath(); context.closePath();
this.fillStroke(); this.fillStroke();
}; };
var json = '{"nodeType":"Stage","width":578,"height":200,"scale":{"x":1,"y":1},"visible":true,"_listening":true,"alpha":1,"x":0,"y":0,"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"_draggable":false,"_children":[{"nodeType":"Layer","visible":true,"_listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"_draggable":false,"_children":[{"nodeType":"Group","visible":true,"_listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"_draggable":false,"_children":[{"nodeType":"Shape","detectionType":"path","visible":true,"_listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"_draggable":false,"drawFuncName":"drawTriangle","fill":"#00D2FF","stroke":"black","strokeWidth":4}]}]}]}'; var json = '{"attrs":{"width":578,"height":200,"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Stage","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Layer","children":[{"attrs":{"visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false},"nodeType":"Group","children":[{"attrs":{"fill":"#00D2FF","stroke":"black","strokeWidth":4,"detectionType":"path","visible":true,"listening":true,"alpha":1,"x":0,"y":0,"scale":{"x":1,"y":1},"rotation":0,"centerOffset":{"x":0,"y":0},"dragConstraint":"none","dragBounds":{},"draggable":false,"drawFuncName":"drawTriangle"},"nodeType":"Shape"}]}]}]}';
stage.load(json, { stage.load(json, {
drawTriangle: drawTriangle drawTriangle: drawTriangle
}); });
//console.log(stage.toJSON());
test(stage.toJSON() === json, "serialized stage is incorrect"); test(stage.toJSON() === json, "serialized stage is incorrect");
}, },
*/
'STAGE - set stage size': function(containerId) { 'STAGE - set stage size': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,
@ -649,8 +650,6 @@ Test.prototype.tests = {
} }
}); });
console.log(darth);
layer.add(darth); layer.add(darth);
stage.add(layer); stage.add(layer);
}; };
@ -822,7 +821,7 @@ Test.prototype.tests = {
layer.add(star); layer.add(star);
stage.add(layer); stage.add(layer);
}, },
'SHAPES - add five point star with line join': function(containerId) { 'SHAPES - add five point star with line join (ends up as bevel line join)': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
container: containerId, container: containerId,
width: 578, width: 578,