canvas Clearing the screen

canvas Clearing the screen
Nikhil
Published on 2021-07-05 09:54:01

Rectangles


You can use the clearRect method to clear any rectangular section of the canvas.

// Clear the entire canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);

To deal with this, it's possible to reset the transformation matrix before you clear the canvas.

ctx.save(); // Save the current context state
ctx.setTransform(1, 0, 0, 1, 0, 0); // Reset the transformation matrix
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
ctx.restore(); // Revert context state including transformation matrix

 

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 once
var bgGrad = ctx.createLinearGradient(0, 0, 0, canvas.height);
bgGrad.addColorStop(0, "#0FF");
bgGrad.addColorStop(1, "#08F");
// Every time you need to clear the canvas
ctx.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


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.

 

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


It's possible to clear complex shaped regions by changing the globalCompositeOperation property.

// All pixels being drawn will be transparent
ctx.globalCompositeOperation = 'destination-out';
// Clear a triangular section
ctx.globalAlpha = 1; // ensure alpha is 1
ctx.fillStyle = '#000'; // ensure the current fillStyle does not have any transparency
ctx.beginPath();
ctx.moveTo(10, 0);
ctx.lineTo(0, 10);
ctx.lineTo(20, 10);
ctx.fill();
// Begin drawing normally again
ctx.globalCompositeOperation = 'source-over';

ATutorialHub Related Guide

Comments (8)

Leave a Comment

Your email address will not be published. Required fields are marked*

User Comments

html tutorial comments

panduranga gupta

2021-07-05 07:03:13

good website for learning and help me a lot

html tutorial comments

raju

2021-09-25 14:58:47

The awsome website i am looking like for a long time, good work atutorialhub team keep doing

html tutorial comments

Shivani

2021-09-01 15:03:56

Learning a lot from the courses present on atutorialhub. The courses are very well explained. Great experience

html tutorial comments

Harshitha

2021-09-10 15:05:45

It is very helpful to students and easy to learn the concepts

html tutorial comments

Sowmya

2021-09-14 15:06:41

Great job Tutorials are easy to understand Please make use of it

html tutorial comments

Zain Khan

2021-09-18 15:07:23

Great content and customized courses.

html tutorial comments

Rudrakshi Bhatt

2021-09-09 15:08:10

Well structured coursed and explained really well!

html tutorial comments

Pavana Somashekar

2021-09-11 15:09:08

Good platform for beginners and learn a lot on this website