Javascript KineticJS:保存为JSON:多次保存的对象

Javascript KineticJS:保存为JSON:多次保存的对象,javascript,json,html5-canvas,kineticjs,Javascript,Json,Html5 Canvas,Kineticjs,大编辑 在分析了我的情况之后,这似乎是另一个问题/另一个场景,更一般地说是关于保存到json 因此,我使用以下代码将一个新的Shapegroup添加到stage上的层中: ... var selectedShape, json = null; ... function addNode(xPos, yPos) { //create the new node var node = new Kinetic.Circle({ id: 'myRect', x

大编辑

在分析了我的情况之后,这似乎是另一个问题/另一个场景,更一般地说是关于保存到json

因此,我使用以下代码将一个新的Shapegroup添加到stage上的层中:

...
var selectedShape, json = null;
...
function addNode(xPos, yPos)
{
    //create the new node
    var node = new Kinetic.Circle({
        id: 'myRect',
        x: xPos,
        y: yPos,
        radius: 30,
        fill: '#FFFFFF',
        stroke: '#000000',
        strokeWidth: 4,
        // test: "testattr"
    });

    // create the label
    var label = new Kinetic.Text({
        x: node.getX() - node.getRadius() + 10,
        y: node.getY() + node.getRadius() + 4,
        text: 'node',
        fontSize: 12,
        fontFamily: 'Arial',
        fill: 'black',
    });

    // create group
    var nodeGroup = new Kinetic.Group({
        draggable: true
    });

    // add shapes to group
    nodeGroup.add(node);
    nodeGroup.add(label);

    // add group to the layer
    layer.add(nodeGroup);

    // add layer to the stage
    stage.add(layer);


    /*
    *  Events for Nodes
    *  all events for the actual states / nodes
    */

    // mouse over => red stroke
    node.on('mouseover', function() {
        $('body').css('cursor', 'pointer');
        this.setStroke('red');
        layer.draw();
    });
    // mouse out => back in black
    node.on('mouseout', function() {
        if(selectedShape != this){
            console.log('mouseout fired, Position: ' + node.getX());
            $('body').css('cursor', 'default');
            this.setStroke('#000000');
            writeMessage(messageLayer, node.getX()); // just for testing purposes
            layer.draw();
        }
    });
    node.on('click tap', function(){ //relevant
        if(selectedShape != null){
            $('body').css('cursor', 'default');
            selectedShape.setStroke('#000000');
            layer.draw();
        }
        selectedShape = null;
        console.log('clicked');
        selectedShape = this;
        this.setStroke('red');
        layer.draw();
    });

    /*
    *  Events for Node-labels
    *  events for labels
    */
    label.on('mouseover', function() {
        $('body').css('cursor', 'text');
        this.setStroke('red');
        this.setStrokeWidth(0.5)
        layer.draw();
    });

    label.on('mouseout', function() {
        $('body').css('cursor', 'default');
        this.setStroke('');
        this.setStrokeWidth(0);
        layer.draw();
    });

    //change the Label of a node, return 'node' if nothing entered or cancelled.
    label.on('click', function(){
        var lblTxt = prompt('Neue Bezeichnung:', ''); 
        if (lblTxt) {
            this.setText(lblTxt);
        } else {
            this.setText('node');
        }
        layer.draw();
    });
}
有一个按钮“添加新状态”,它实际上添加了一个新组。 代码:

以及“移除状态”按钮,用于移除所选节点组。 代码:

此外,还有一个按钮“保存到json”,我想在这里保存舞台上所有实际剩余的形状。 代码:

因此,现在我测试以下情况:

案例1:保存空阶段

JSON输出:

{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    }
]
}

状态:似乎还可以。所以,最后一个}的格式问题取决于stackoverflow,它应该(并且是)实际包含在代码标记中

案例2:保存空阶段后添加一个节点(双击/点击或使用按钮在此处没有区别)。再次保存

JSON输出:

{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    }
]
}

状态:为什么有一个空层?但是:一组,两个物体,似乎没问题

案例3

正在添加另一个节点。保存

JSON输出:

{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            },
            {
                "attrs": {
                    "draggable": true,
                    "x": 206,
                    "y": 75,
                    "rotation": 0,
                    "scaleX": 1,
                    "scaleY": 1,
                    "offsetX": 0,
                    "offsetY": 0,
                    "skewX": 0,
                    "skewY": 0
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "red",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            },
            {
                "attrs": {
                    "draggable": true,
                    "x": 206,
                    "y": 75,
                    "rotation": 0,
                    "scaleX": 1,
                    "scaleY": 1,
                    "offsetX": 0,
                    "offsetY": 0,
                    "skewX": 0,
                    "skewY": 0
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "red",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    }
]
{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    }
]
{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    }
]
}

状态:在这里您可以看到我的问题的第一次出现:我的stage上的所有对象在两个不同的层上的JSON文件中加倍。因此,当添加更多对象时,它们将被放大三倍,以此类推。我的问题是:我想添加一个数据模型,并将数据与数据库一起使用,所以我认为这相当混乱,但我不知道哪里出了问题

**案例4** 从“我的阶段”中删除除一个节点外的所有节点:

JSON输出:

{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            },
            {
                "attrs": {
                    "draggable": true,
                    "x": 206,
                    "y": 75,
                    "rotation": 0,
                    "scaleX": 1,
                    "scaleY": 1,
                    "offsetX": 0,
                    "offsetY": 0,
                    "skewX": 0,
                    "skewY": 0
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "red",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            },
            {
                "attrs": {
                    "draggable": true,
                    "x": 206,
                    "y": 75,
                    "rotation": 0,
                    "scaleX": 1,
                    "scaleY": 1,
                    "offsetX": 0,
                    "offsetY": 0,
                    "skewX": 0,
                    "skewY": 0
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "red",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    }
]
{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    }
]
{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    }
]
}

状态:剩余节点再次加倍

**案例5**:删除所有节点,再次使用空阶段(添加2个节点后,再删除它们)

JSON输出:

{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            },
            {
                "attrs": {
                    "draggable": true,
                    "x": 206,
                    "y": 75,
                    "rotation": 0,
                    "scaleX": 1,
                    "scaleY": 1,
                    "offsetX": 0,
                    "offsetY": 0,
                    "skewX": 0,
                    "skewY": 0
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "red",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            },
            {
                "attrs": {
                    "draggable": true,
                    "x": 206,
                    "y": 75,
                    "rotation": 0,
                    "scaleX": 1,
                    "scaleY": 1,
                    "offsetX": 0,
                    "offsetY": 0,
                    "skewX": 0,
                    "skewY": 0
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "red",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    }
]
{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": [
            {
                "attrs": {
                    "draggable": true
                },
                "className": "Group",
                "children": [
                    {
                        "attrs": {
                            "id": "myRect",
                            "x": 125,
                            "y": 125,
                            "radius": 30,
                            "fill": "#FFFFFF",
                            "stroke": "#000000",
                            "strokeWidth": 4,
                            "test": "testattr"
                        },
                        "className": "Circle"
                    },
                    {
                        "attrs": {
                            "width": "auto",
                            "height": "auto",
                            "x": 105,
                            "y": 159,
                            "text": "node",
                            "fontSize": 12,
                            "fontFamily": "Arial",
                            "fill": "black"
                        },
                        "className": "Text"
                    }
                ]
            }
        ]
    }
]
{
"attrs": {
    "width": 960,
    "height": 600
},
"className": "Stage",
"children": [
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    },
    {
        "attrs": {},
        "className": "Layer",
        "children": []
    }
]
}

状态:阶段为空,但仍保留层。没那么好

结论:我觉得我做错了什么。在这个问题上有很多JSON,我希望有人能仔细阅读,并帮助我找出我做错了什么。那太好了

致以最良好的祝愿, 多米尼克

另一次编辑
在使用stage.add(layer)的addnode函数中,我似乎遇到了问题;添加新的形状组。如果能以不同的方式将新组添加到一个层中,我将不胜感激,因为我对kineticjs还相当陌生。

因此,在写下这个问题后,重写整个问题,在进一步调查后添加另一个编辑,我确实发现了我的问题,我想与大家分享一下:

在addnode函数中,我调用了
stage.add(layer)
——正如代码所说,它为每个新的Shapegroup添加了一个新的层。这导致了我在问题中解释的行为


现在我删除了从addNode到我的
init()
-函数的
stage.add(layer)
,该函数只在启动时调用。在addNode,我现在只说
layer.add(nodeGroup);layer.draw()
;现在它就像一个符咒。很抱歉给您带来不便:(我脑子里有个结。

所以,在写下这个问题,重写整个问题,在进一步调查后添加另一个编辑之后,我发现了我的问题,我想我想与您分享一下:

在addnode函数中,我调用了
stage.add(layer)
——正如代码所说,它为每个新的Shapegroup添加了一个新的层。这导致了我在问题中解释的行为


现在,我将addNode中的
stage.add(layer)
删除到我的
init()
-函数中,该函数仅在启动时调用。在addNode,我现在只说
layer.add(nodeGroup);layer.draw()
;它现在就像一个符咒一样工作。对于给您带来的不便,我深表歉意:(我脑子里有一个结。

你能在问题中加入toJSON字符串并说明到底发生了什么吗there@Ani刚刚编辑了我的整个问题,因为它通常与我的删除机制无关。我希望现在它变得更加清晰,尽管现在有很多JSON;)你能在问题中加入toJSON字符串,并说明到底发生了什么吗there@Ani刚刚编辑了我的整个问题,因为它通常与我的删除机制无关。我希望现在它变得更加清晰,尽管现在有很多JSON;)这救了我的命,我也有同样的事情发生,但我不知道如何解决它!我在同一条船上!谢谢!这救了我的命,我也有同样的事情发生,但我不知道如何解决它!我在同一条船上!谢谢!