Removing objects from a layer using KineticJS

2019-03-18 08:33发布

I am currently working a project involving 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?

3条回答
可以哭但决不认输i
2楼-- · 2019-03-18 09:08

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.

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

myShape.remove();
myLayer.draw();
查看更多
Melony?
3楼-- · 2019-03-18 09:24

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

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

查看更多
我想做一个坏孩纸
4楼-- · 2019-03-18 09:29

U can use the prototype of kinetic function

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

baselayer.draw();

查看更多
登录 后发表回答