我正在尝试删除包含Groups(包含形状)或简单形状(line,Rect,Circle等)作为其元素的数组元素。我的功能如下:
deleteSelectedShape = function () {
var i,
shapeObj,
selectedObjects = currentContext.getSelectedObjects(),
shapeLayer = currentContext.getShapeLayer();
if (selectedObjects && selectedObjects.length > 0) {
for (i = 0; i < selectedObjects.length; i += 1) {
shapeObj = selectedObjects[i];
// shapeObj.remove(); results same error as mentioned at last
if (shapeObj.nodeType === "Group") {
shapeObj.destroyChildren();
}
else{
shapeObj.destroy();
}
}
}
selectedObjects = [];
shapeLayer.draw();
};
我也尝试了这个
if (shapeObj.nodeType === "Group") {
var childs = [];
childs = shapeObj.getChildren();
for (var j = 0; j < childs.length; j++) {
childs[j].remove();
}
}
else{
shapeObj.remove();
}
}
}
此处个别形状被删除,但如果数组中有组,则表示错误:
TypeError: this.getParent(...) is undefined in Kineticjs file
请建议我正确的想法。谢谢!!!
答案 0 :(得分:0)
一个问题:从数组中删除项目时,必须反向遍历该数组。
var i=selectedObjects.length-1;
while( i-- >=0 ){
var shapeObj = selectedObjects[i];
if (shapeObj.nodeType === "Group") {
shapeObj.destroyChildren();
}else{
shapeObj.destroy();
}
}