2013-07-03 49 views
0

大編輯KineticJS:保存到JSON:對象保存不止一次

在分析了我的情況,這似乎是另外一個問題/另一種情況下,更一般約保存到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" 
        } 
       ] 
      } 
     ] 
    } 
] 

}

狀態:同樣,其餘節點被加倍。

**殼體5 **:刪除所有節點,具有一個空的階段再次

JSON輸出(添加2個節點,然後去除它們後):

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

}

狀態:舞臺是空的,但層仍然存在。不是那麼好。

結論:我覺得我做得非常不對的。這是在這個問題上有很多JSON的,我希望有人居然通過這個閱讀並可以幫我找出我做錯了什麼。會很棒。

此致 多米尼克

另一個編輯 問題在addnode的功能似乎對我來說,使用stage.add(層);添加新的形狀組。一種不同的方式來新組添加到一個層,將不勝感激,因爲我是相當新的kineticjs,不知道它。

+0

你可以包括在問題的toJSON串並顯示究竟是什麼發生的事情 – Ani

+0

是的,我將修改它。 – Dominik

+0

@Ani剛編輯我的整個問題,因爲它通常沒有什麼與我的刪除機制我現在希望它變得更爲清楚,雖然現在有很多的JSON) – Dominik

回答

3

所以,寫出這個問題,重寫整個問題,經過進一步investigaten加入另一個編輯後,我居然發現我的問題,我覺得我要與你分享:

在addnode的功能,我叫stage.add(layer) - 作爲代碼表示,它會爲每個新Shapegroup一個新層。這導致了我在問題中解釋的行爲。

現在我從addNode中刪除了stage.add(layer)我的init()-功能,它只在啓動時調用。在ADDNODE,我現在只想說layer.add(nodeGroup); layer.draw();它現在就像一種魅力。很抱歉給您帶來不便:(我在我的大腦有一個結。

+1

這只是救了我的命,我有同樣的事情發生,我沒有知道如何解決它! – gikygik

+1

我在這裏同舟共濟!謝謝! – GRowing