HTML canvas clearRect() Method
Example
Clears a rectangle within a given rectangle:
JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(0,0,300,150);
ctx.clearRect(20,20,100,50);
Try it »
Browser support





Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the clearRect() method.
**Note:** Internet Explorer 8 and earlier versions do not support
Definition and Usage
The clearRect() method clears the specified pixels within a given rectangle.
| JavaScript syntax: | context.clearRect(x,y,width,height); |
|---|
Parameter value
| Parameters | Description |
|---|---|
| x | The x-coordinate of the upper-left corner of the rectangle to be cleared. |
| y | The y-coordinate of the upper-left corner of the rectangle to be cleared. |
| width | The width of the rectangle to be cleared, in pixels. |
| height | The height of the rectangle to be cleared, in pixels. |
HTML canvas Reference Manual