clearRect method

The clearRect method works like an eraser, clearing the rectangular area, making the content completely transparent.

Syntax

javascriptcontext.clearRect(x, y, width, height);

Example

Let's draw a square with fillRect and erase part of it with clearRect:

html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
javascriptlet canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');

ctx.fillRect(50, 50, 100, 100);
ctx.clearRect(75, 75, 50, 50);

Результат:

See also

  • the fillRect method
    that draws a filled rectangle