Note that there are some explanatory texts on larger screens.

plurals
  1. POKineticJS: Saving to JSON: Objects saved more than once
    primarykey
    data
    text
    <p><strong>big edit</strong></p> <p>Having analyzed my situation, it seems to be another question / another scenario, more generally about saving to json.</p> <p>So, I add a new Shapegroup to a layer on my stage with the following code:</p> <pre><code>... 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 =&gt; red stroke node.on('mouseover', function() { $('body').css('cursor', 'pointer'); this.setStroke('red'); layer.draw(); }); // mouse out =&gt; 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(); }); } </code></pre> <p>Having a button 'add new State' which actually adds a new group. Code: </p> <pre><code>$('#createNode').click(function(e){ addNode(125, 125); }); </code></pre> <p>And a Button "remove State" which removes a selected nodegroup. Code:</p> <pre><code>$('#removeNode').click(function(e){ if(selectedShape){ var selectedGroup = selectedShape.getParent(); selectedGroup.removeChildren(); selectedGroup.remove(); layer.draw(); } else { writeMessage(messageLayer, 'No Object chosen'); } }); </code></pre> <p>Also, there's a button 'save to json' where I want to save all the actually remaining Shapes on my Stage. Code:</p> <pre><code> $('#saveJSON').click(function(e){ json = null; json = stage.toJSON(); console.log(json); }); </code></pre> <p>So, now I test the following cases:</p> <p><strong>Case 1</strong>: Save empty stage</p> <p>JSON output:</p> <pre><code>{ "attrs": { "width": 960, "height": 600 }, "className": "Stage", "children": [ { "attrs": {}, "className": "Layer", "children": [] } ] </code></pre> <p>}</p> <p>Status: Seems to be OK.<em>So, the formatting issue with the last } depends on stackoverflow, it should (and is) actually be included to code tag.</em></p> <p><strong>Case 2</strong>: Add one Node after Saving empty Stage (double-clicking / tapping or using button is no difference here). Save again.</p> <p>JSON Output: </p> <pre><code>{ "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" } ] } ] } ] </code></pre> <p>}</p> <p>Status: Why is there an empty Layer? But: One Group, two Objects, seems to be okay.</p> <p><strong>Case 3</strong></p> <p>Adding another Node. Save.</p> <p>JSON Output:</p> <pre><code>{ "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" } ] } ] } ] </code></pre> <p>}</p> <p>Status: Here you can see the first occurence of my problem: All the objects on my stage are doubled in my JSON file on two different layers. So they are tripled and so on when adding more objects. My Problem: I want to add a data model and use the data with a database, so I think this is pretty messy but I have no clue where I went wrong.</p> <p>** case 4** Removing all but one node from my stage:</p> <p>JSON Output:</p> <pre><code>{ "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" } ] } ] } ] </code></pre> <p>}</p> <p>Status: Again, the remaining nodes are doubled.</p> <p>** case 5**: Removing all nodes, having an empty stage again (after adding 2 nodes, then removing them)</p> <p>JSON Output:</p> <pre><code>{ "attrs": { "width": 960, "height": 600 }, "className": "Stage", "children": [ { "attrs": {}, "className": "Layer", "children": [] }, { "attrs": {}, "className": "Layer", "children": [] }, { "attrs": {}, "className": "Layer", "children": [] } ] </code></pre> <p>}</p> <p>Status: Stage is empty, but layers still remaining. Not that nice.</p> <p><strong>Conclusion</strong>: I think I'm doing something pretty wrong. It's a lot of JSON in this question and I hope someone actually reads through this and may help me figuring out what I did wrong. Would be so great.</p> <p>Best regards, Dominik</p> <p><strong>another edit</strong> Problem seems for me in addnode-function, using stage.add(layer); to add new shapegroups. A different way to add new groups to <em>one</em> layer would be much appreciated for I am fairly new to kineticjs and don't know it yet.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload