Clearing the screen
Rectangles
Section titled “Rectangles”You can use the clearRect method to clear any rectangular section of the canvas.
// Clear the entire canvasctx.clearRect(0, 0, canvas.width, canvas.height);Note: clearRect is dependent on the transformation matrix.
To deal with this, it’s possible to reset the transformation matrix before you clear the canvas.
ctx.save(); // Save the current context statectx.setTransform(1, 0, 0, 1, 0, 0); // Reset the transformation matrixctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvasctx.restore(); // Revert context state including // transformation matrixNote: ctx.save and ctx.restore are only requiered if you wish to keep the canvas 2D context state. In some situations save and restore can be be slow and generally should be avoided if not required.
Raw image data
Section titled “Raw image data”It’s possible to write directly to the rendered image data using putImageData. By creating new image data then assigning it to the canvas, you will clear the entire screen.
var imageData = ctx.createImageData(canvas.width, canvas.height);ctx.putImageData(imageData, 0, 0);Note: putImageData is not affected by any transformations applied to the context. It will write data directly to the rendered pixel region.
Complex shapes
Section titled “Complex shapes”It’s possible to clear complex shaped regions by changing the globalCompositeOperation property.
// All pixels being drawn will be transparentctx.globalCompositeOperation = 'destination-out';
// Clear a triangular sectionctx.globalAlpha = 1; // ensure alpha is 1ctx.fillStyle = '#000'; // ensure the current fillStyle does not have any transparencyctx.beginPath();ctx.moveTo(10, 0);ctx.lineTo(0, 10);ctx.lineTo(20, 10);ctx.fill();
// Begin drawing normally againctx.globalCompositeOperation = 'source-over';Clear canvas with gradient.
Section titled “Clear canvas with gradient.”Rather than use clearRect which makes all pixels transparent you may want a background.
To clear with a gradient
// create the background gradient oncevar bgGrad = ctx.createLinearGradient(0,0,0,canvas.height);bgGrad.addColorStop(0,"#0FF");bgGrad.addColorStop(1,"#08F");
// Every time you need to clear the canvasctx.fillStyle = bgGrad;ctx.fillRect(0,0,canvas.width,canvas.height);This is about half as quick 0.008ms as clearRect 0.004ms but the 4millions of a second should not negatively impact any realtime animation. (Times will vary considerably depending on device, resolution, browser, and browser configuration. Times are for comparison only)
Clear canvas using composite operation
Section titled “Clear canvas using composite operation”Clear the canvas using compositing operation. This will clear the canvas independent of transforms but is not as fast as clearRect().
ctx.globalCompositeOperation = 'copy';anything drawn next will clear previous content.
Syntax
Section titled “Syntax”- void clearRect(x, y, width, height)
- ImageData createImageData(width, height)
Remarks
Section titled “Remarks”None of these methods will produce transparent pixels if the context was created with the alpha: false parameter.