HTML Canvas clearRect() Method: Clearing Rectangular Areas
The clearRect() method is a crucial component of the HTML Canvas API, allowing you to erase or clear specific rectangular portions of the canvas. This method is essential for creating animations, updating dynamic drawings, and managing complex canvas content by selectively removing elements. This article will provide a detailed exploration of the clearRect() method, including its syntax, examples, and practical uses.
What is clearRect()?
The clearRect() method erases a rectangular area of the canvas, effectively making it transparent. It is a key tool in any canvas-based application where content needs to be updated dynamically. Unlike other canvas methods that draw or modify existing pixels, clearRect() removes pixels, making the area ready for new content.
Purpose of clearRect()
The main purpose of the clearRect() method is to:
- Remove existing content in a specified rectangular area of the canvas.
- Prepare the canvas for redrawing, essential in animation loops.
- Update specific parts of the canvas without redrawing the entire canvas.
- Create dynamic effects by clearing and redrawing content.
Syntax of clearRect()
The clearRect() method takes four parameters:
ctx.clearRect(x, y, width, height);
Where:
x: The x-coordinate of the top-left corner of the rectangle to clear.y: The y-coordinate of the top-left corner of the rectangle to clear.width: The width of the rectangle to clear.height: The height of the rectangle to clear.
All parameters are numbers representing pixel values on the canvas.
| Parameter | Type | Description |
|---|---|---|
x |
Number | The x-coordinate of the top-left corner of the rectangle. |
y |
Number | The y-coordinate of the top-left corner of the rectangle. |
width |
Number | The width of the rectangle to clear. |
height |
Number | The height of the rectangle to clear. |
Note: The clearRect() method operates within the current transformation matrix. 📝
Basic Examples of clearRect()
Let's explore some practical examples to understand how to use the clearRect() method effectively.
Clearing a Basic Rectangle
This example demonstrates how to clear a rectangle from a canvas that initially contains a filled square:
<canvas
id="clearCanvasBasic"
width="200"
height="150"
style="border: 1px solid black;"
></canvas>
<script>
//<![CDATA[
const canvas_basic = document.getElementById("clearCanvasBasic");
const ctx_basic = canvas_basic.getContext("2d");
// Draw a filled rectangle
ctx_basic.fillStyle = "lightblue";
ctx_basic.fillRect(20, 20, 100, 100);
// Clear a portion of the rectangle
ctx_basic.clearRect(40, 40, 60, 60);
//]]]]><![CDATA[>
</script>
Clearing Multiple Rectangles
This example shows how to clear multiple separate rectangular areas on the canvas:
<canvas
id="clearCanvasMultiple"
width="200"
height="150"
style="border: 1px solid black;"
></canvas>
<script>
//<![CDATA[
const canvas_multiple = document.getElementById("clearCanvasMultiple");
const ctx_multiple = canvas_multiple.getContext("2d");
ctx_multiple.fillStyle = "lightgreen";
ctx_multiple.fillRect(10, 10, 180, 130); // Filled background
ctx_multiple.clearRect(30, 30, 40, 40); // Clear first rectangle
ctx_multiple.clearRect(100, 80, 50, 30); // Clear second rectangle
//]]]]><![CDATA[>
</script>
Creating a Simple Animation with clearRect()
The clearRect() method is essential for animations. This example demonstrates how to create a basic animation using clearRect() by clearing and redrawing a circle at different positions:
<canvas
id="clearCanvasAnimation"
width="200"
height="150"
style="border: 1px solid black;"
></canvas>
<script>
//<![CDATA[
const canvas_animation = document.getElementById("clearCanvasAnimation");
const ctx_animation = canvas_animation.getContext("2d");
let x = 20;
const radius = 20;
function animate() {
ctx_animation.clearRect(
0,
0,
canvas_animation.width,
canvas_animation.height
); // Clear the entire canvas
ctx_animation.beginPath();
ctx_animation.arc(x, 75, radius, 0, 2 * Math.PI);
ctx_animation.fillStyle = "coral";
ctx_animation.fill();
x += 2;
if (x > canvas_animation.width + radius) {
x = -radius; // Reset x if it goes off the right edge
}
requestAnimationFrame(animate);
}
animate();
//]]]]><







