HTML canvas clearRect() Method
Example
Clear a rectangle within a given rectangle:
JavaScript:
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 300, 150);
ctx.clearRect(20, 20, 100, 50);
Try it Yourself »
Description
The clearRect() method clears the specified pixels within a given rectangle.
JavaScript syntax: | context.clearRect(x, y, width, height); |
---|
Parameter Values
Parameter | Description | Play it |
---|---|---|
x | The x-coordinate of the upper-left corner of the rectangle to clear | Play it » |
y | The y-coordinate of the upper-left corner of the rectangle to clear | Play it » |
width | The width of the rectangle to clear, in pixels | Play it » |
height | The height of the rectangle to clear, in pixels | Play it » |
Browser Support
The <canvas>
element is an HTML5 standard (2014).
clearRect()
is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
❮ Canvas API