Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Canvas & KineticJS layer clearing

I wondered if anyone could shed some light on the way in which layers are managed in Canvas and KineticJS. I'm struggling understand why when i clear(); a layer, then use the draw(); function on that layer again, it comes back with the shapes etc that I originally add to that layer.

for example:

http://jsfiddle.net/vPGbz/1/

I assumed clearing a layer completely removes it, and in order to redraw it i would have to set up new shapes and construct a new layer.

If anyone could explain this too me I would be very thankful.

Cheers, Caius

like image 234
Caius Eugene Avatar asked Mar 22 '12 14:03

Caius Eugene


2 Answers

One has to be honest that this documentation is not be considered uber-verbose, but as you can see here: http://jsfiddle.net/vPGbz/2/ clear will only remove the drawn representation of a layer from the Stage object. What you are looking for is the remove method that is used to remove certain elements from the layer.

Like:

circleLayer.remove(circle);
like image 167
m90 Avatar answered Oct 02 '22 14:10

m90


For now, you could do layer.children = []; but I don't know if this will have any side effects that it shouldn't!

Edit: Don't do this. Instead, do this: layer.removeChildren() found at http://kineticjs.com/api-docs.php under Container.

like image 26
Adam K Dean Avatar answered Oct 02 '22 12:10

Adam K Dean