After experimenting with composite operations and drawing images on the canvas I\'m now trying to remove images and compositing. How do I do this?
I need to clear th
This is 2018 and still there is no native method to completely clear canvas for redrawing. clearRect()
does not clear the canvas completely. Non-fill type drawings are not cleared out (eg. rect()
)
1.To completely clear canvas irrespective of how you draw:
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
context.beginPath();
Pros: Preserves strokeStyle, fillStyle etc.; No lag;
Cons: Unnecessary if you are already using beginPath before drawing anything
2.Using the width/height hack:
context.canvas.width = context.canvas.width;
OR
context.canvas.height = context.canvas.height;
Pros: Works with IE Cons: Resets strokeStyle, fillStyle to black; Laggy;
I was wondering why a native solution does not exist. Actually, clearRect()
is considered as the single line solution because most users do beginPath()
before drawing any new path. Though beginPath is only to be used while drawing lines and not closed path like rect().
This is the reason why the accepted answer did not solve my problem and I ended up wasting hours trying different hacks. Curse you mozilla