What's the difference between `destroy` and `remove` in konvajs
Asked Answered
J

1

5

I have create a shape object(like konva.Rect) and want to reuse it in some cases.

In the first place, add it to layer, and everything is fine, like drag, move .etc. And then destroy it from layer by some trigger event, and at some point in time, add it back to layer with layer.add(rectObj), the shape lost some properties like drag, but use remove will not.

I check the source code, but have no clue about this situation. The only difference is that delete id and name in destroy(),

It's seems too vague in documents.

destroy() remove and destroy self

remove() remove self from parent, but don't destroy

So want to know if use remove and shape object id and name will remain in layer, Is there any significant impact about the render or others?

If use destroy, how to make it works like first add to layer?

Jaquelin answered 2/7, 2018 at 9:2 Comment(0)
V
8

I updated the docs. I hope it is more clear now:

https://konvajs.org/api/Konva.Node.html#destroy

destroy: remove and destroy a node. Kill it forever! You should not reuse node after destroy().

https://konvajs.org/api/Konva.Node.html#remove

remove: remove self from a parent, but don't destroy. You can reuse node later.

Volcanism answered 2/7, 2018 at 13:34 Comment(1)
Could you add code to illustrate how to maintain a handle to the node and add it back in to the parent? Is it as easy as var node = new shape({}); layer.add(node); node.remove(); layer.add(node); or is there more to it ?Parrish

© 2022 - 2024 — McMap. All rights reserved.