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

时间:2013-07-03 09:54:32

标签: javascript json html5-canvas kineticjs

大编辑

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

因此,我使用以下代码将新的Shapegroup添加到舞台上的图层:

...
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();
    });
}

有一个按钮'添加新状态'这实际上增加了一个新的组。 代码:

$('#createNode').click(function(e){
        addNode(125, 125);
    });

按钮"删除状态"删除选定的节点组。 代码:

$('#removeNode').click(function(e){
        if(selectedShape){
        var selectedGroup = selectedShape.getParent();
        selectedGroup.removeChildren();
        selectedGroup.remove();
        layer.draw();
        } else {
            writeMessage(messageLayer, 'No Object chosen');
        }
    });

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

        $('#saveJSON').click(function(e){
         json = null;
        json = stage.toJSON();
        console.log(json);
    });

所以,现在我测试以下情况:

案例1 :保存空白阶段

JSON输出:

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

}

状态:似乎没问题。因此,last的格式化问题取决于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"
                    }
                ]
            }
        ]
    }
]

}

状态:在这里您可以看到我的问题的第一次出现:我的舞台上的所有对象在我的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": {},
        "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"
                    }
                ]
            }
        ]
    }
]

}

状态:再次,剩余的节点加倍。

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

JSON输出:

{
"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-function中对我来说似乎有问题;添加新的形状组。将新组添加到一个图层的另一种方法将非常受欢迎,因为我对kineticjs还不太了解并且还不知道。

1 个答案:

答案 0 :(得分:3)

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

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

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