gpt4 book ai didi

javascript - KineticJS:保存到 JSON:对象保存多次

转载 作者:行者123 更新时间:2023-11-28 20:25:48 25 4
gpt4 key购买 nike

大修改

分析了我的情况后,这似乎是另一个问题/另一个场景,更一般地说是关于保存到 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**:删除所有节点,再次出现空舞台(添加2个节点后,然后删除它们)

JSON 输出:

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

}

状态:舞台为空,但仍保留图层。不太好。

结论:我认为我做错了一些事情。这个问题中有很多 JSON,我希望有人真正读完这个并可以帮助我找出我做错了什么。那就太好了。

最诚挚的问候,多米尼克

另一个编辑对我来说问题似乎在 addnode-function 中,使用 stage.add(layer);添加新的形状组。非常感谢将新组添加到一个层的不同方法,因为我对kineticjs相当陌生,而且还不知道。

最佳答案

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

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

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

关于javascript - KineticJS:保存到 JSON:对象保存多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17444897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com