views:

380

answers:

4

I can't seem to find the function to remove a shape or path from the canvas after it has been created.

So I'm creating a bezier curve between 2 points with

beginPath();
bezierCurveTo();
stroke();
closePath();

. How can I remove this from the canvas once it's been created? I need to be able to call the remove function via toggle() and blur(). I'm sure something exists for this...

Thanks in advance for any help!

+1  A: 

As far as I remember the specification you must call context.save() before drawing, then draw something, and then call context.restore() to return to the previous state.

ILog
Thanks for the suggestion - I tried this (unsuccessfully) and then found this resource (http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#the-canvas-state) stating:"The current path and the current bitmap are not part of the drawing state. The current path is persistent, and can only be reset using the beginPath() method. The current bitmap is a property of the canvas, not the context."Thanks anyway though, much appreciated!
bobsoap
+1  A: 

You can't remove a path/shape from the canvas. You can draw something else over it or clear the canvas.

nxt
+1  A: 

You might try using SVG instead of canvas. There's a fantastic library called Raphaël that makes working with SVG a breeze. It works in all browsers too, including IE6.

With SVG each shape is its own element that can be moved, transformed, or removed.

bcherry
This looks fantastic, I'm already looking into it. Thanks!
bobsoap
A: 

Thanks for the great input to all of you - it helped me find the solution:

context.clearRect(x,y,w,h);

(link)

This will clear anything within that rectangle.

I found the method on the page I found while digging for ILog's answer to save/restore the context, and it's all on there. Thanks again.

bobsoap