javascript 使用 KineticJS 从图层中移除对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12757176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:02:48  来源:igfitidea点击:

Removing objects from a layer using KineticJS

javascripthtml5-canvaskineticjs

提问by user1724623

I am currently working a project involving KineticJS.

我目前正在开展一个涉及 KineticJS 的项目。

I have to dynamically create and delete shapes constantly, but cannot seem to figure out how to do the latter. I've been trying to do:

我必须不断地动态创建和删除形状,但似乎无法弄清楚如何做后者。我一直在尝试做:

 $ myLayer.remove(myShape)

because this is what most posts seem to recommend. But, the documentation says that this will remove the layer from the stage and not the shape from the layer. When I try this in project it in fact removes the layer from the stage.

因为这是大多数帖子似乎推荐的内容。但是,文档说这将从舞台上删除图层,而不是从图层中删除形状。当我在项目中尝试这个时,它实际上从舞台上删除了图层。

So am I doing something wrong or is there some other way to remove a shape from a layer?

那么我做错了什么还是有其他方法可以从图层中删除形状?

回答by BenMQ

There are two functions that may be helpful.

有两个功能可能会有所帮助。

  • childContainer.remove()removes the childContainer from it's parent.
  • parentContainer.removeChildren()removes all the children from this container.
  • childContainer.remove()从其父级中删除 childContainer。
  • parentContainer.removeChildren()从此容器中删除所有子项。

Edit: This can also apply to shapes. Simply redraw the layer.

编辑:这也适用于形状。只需重新绘制图层。

myShape.remove();
myLayer.draw();

回答by Adam Augusta

Somewhere between Kinetic 4.0 and the latest version, remove(child) stopped working. removeChild(child) doesn't work either.

在 Kinetic 4.0 和最新版本之间的某个地方,remove(child) 停止工作。removeChild(child) 也不起作用。

I resolved this problem by using child.remove();

我通过使用 child.remove() 解决了这个问题;

回答by Manjunath Raddi

U can use the prototype of kinetic function

你可以使用动力学函数的原型

Kinetic.Node.prototype.remove.call(removed_object);

Kinetic.Node.prototype.remove.call(removed_object);

baselayer.draw();

baselayer.draw();