admin管理员组

文章数量:1416050

Im trying to clear the layer from every circle and line (remove it pletely) but keep the image in the background.

If I use layer.clear() it removes the image also.

Question: How to clear the layer but avoid removing the image?

Im trying to clear the layer from every circle and line (remove it pletely) but keep the image in the background.

If I use layer.clear() it removes the image also.

Question: How to clear the layer but avoid removing the image?

https://codesandbox.io/s/sharp-night-t4vtt

Share Improve this question asked Sep 3, 2019 at 10:18 PatrickkxPatrickkx 1,8809 gold badges38 silver badges66 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

layer.clear() do not remove objects from the scene. It is just clear canvas element and on the next layer.draw() all objects will be drawn again.

Instead of layer.clear() you need to remove objects, that you don't need anymore. From the demo, I see that you need to remove lines and circles. You can use layer.find(selector) to find that nodes and destroy them.

document.getElementById("btn").addEventListener("click", () => {
  layer.find('Line').destroy();
  layer.find('Circle').destroy();
  layer.draw();
});

本文标签: javascriptKonvaclear layer but keep the backgroundStack Overflow