【问题标题】:KineticJS: Saving to JSON: Objects saved more than onceKineticJS:保存到 JSON:对象多次保存
【发布时间】:2013-07-03 09:54:32
【问题描述】:

大编辑

分析了我的情况,这似乎是另一个问题/另一个场景,更普遍的是关于保存到 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": []
    }
]

}

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

}

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

** case 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 中对我来说似乎有问题添加新的形状组。非常感谢一种将新组添加到 one 层的不同方法,因为我对 kineticjs 还很陌生,还不知道。

【问题讨论】:

  • 您能否在问题中包含 toJSON 字符串并显示那里到底发生了什么
  • @Ani 刚刚编辑了我的整个问题,因为它通常与我的删除机制无关。我希望现在它变得更清楚了,尽管现在有很多 JSON ;)

标签: javascript json html5-canvas kineticjs


【解决方案1】:

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

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

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

【讨论】:

  • 这只是救了我的命,我也发生了同样的事情,但我不知道如何解决它!
  • 我在同一条船上!谢谢!
猜你喜欢
  • 2014-08-30
  • 2021-01-10
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
  • 2017-09-10
  • 2016-08-15
相关资源
最近更新 更多