The Canvas clearRect(x, y, width, height) method erases pixels inside a rectangular area. The cleared pixels become transparent, revealing the canvas background or anything behind the canvas element.
Use it to erase part of a drawing, clear the whole canvas before redrawing, or remove a rectangular region during an animation or interactive update.
<canvas id="my_canvas" width="400" height="180"></canvas>
<script>
const canvas = document.getElementById('my_canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#0d6efd';
ctx.fillRect(20, 20, 220, 100);
// Erase a 70 x 50 area from the filled rectangle
ctx.clearRect(60, 45, 70, 50);
</script>
ctx.clearRect(x, y, width, height);
| Parameter | Meaning |
|---|---|
x | Horizontal coordinate of the rectangle's starting point. |
y | Vertical coordinate of the rectangle's starting point. |
width | Width of the area to clear. Positive values extend right; negative values extend left. |
height | Height of the area to clear. Positive values extend down; negative values extend up. |
clearRect() paints nothing visible. It makes the selected pixels transparent.
Move the controls to change the X position, Y position, width and height of the erased area. Use Fill Again to restore the dark background before experimenting again.
Use the canvas element's drawing-buffer dimensions as the width and height:
ctx.clearRect(0, 0, canvas.width, canvas.height);
This is common before drawing the next frame of an animation or when rebuilding a scene from scratch.
clearRect() erases pixels directly; you do not need beginPath(), stroke() or fill() to make it work.
// No beginPath() or stroke() is required for the erase itself
ctx.clearRect(20, 20, 80, 50);
If you start drawing a new path after clearing, call beginPath() when you want that new path separated from previously defined subpaths.
Clearing pixels does not restore drawing settings such as fillStyle, strokeStyle, lineWidth, shadows, clipping, or transformations. Those settings remain in the 2D context until you change or restore them.
ctx.fillStyle = 'red';
ctx.clearRect(0, 0, canvas.width, canvas.height);
// fillStyle is still red
ctx.fillRect(20, 20, 100, 60);
If your goal is to clear the bitmap and return the 2D context to its default drawing state, modern browsers also provide ctx.reset(). For ordinary erasing, clearRect() remains the focused method.
Negative dimensions are valid. A negative width clears to the left of x, and a negative height clears upward from y.
ctx.clearRect(220, 130, -100, -60);
| Method | Use |
|---|---|
clearRect() | Erases pixels in a rectangular area by making them transparent. |
fillRect() | Immediately draws a filled rectangle. |
strokeRect() | Immediately draws a rectangle outline. |
rect() | Adds a rectangle to the current path for later filling or stroking. |
Canvas pixels are not automatically exposed as structured content. If clearing or revealing part of a drawing changes information that users need, provide the same state or result through accessible HTML, text, controls, or another suitable alternative.
stroke() after clearRect() and expecting it to perform the erase.beginPath() as though it were required by clearRect().width and height when clearing the whole canvas.clearRect() to reset colors, line settings, shadows, transforms or clipping.The syntax is ctx.clearRect(x, y, width, height).
Use ctx.clearRect(0, 0, canvas.width, canvas.height) when the context is using its normal coordinate system.
No. It makes the pixels transparent. A white result appears only when the canvas or page background behind those pixels is white.
No. It clears pixels without resetting the rest of the Canvas 2D drawing state.
No. The erase itself is immediate. Use beginPath() when starting a new path that should be independent of previously defined path segments.
Canvas tutorial | fillRect() | strokeRect() | rect() | moveTo() | lineTo()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.