HTML canvas clearRect()Method
Example
Clear 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);
var ctx=c.getContext("2d");
ctx.fillStyle="red";
ctx.fillRect(0,0,300,150);
ctx.clearRect(20,20,100,50);
Try it Yourself »
Browser Support
![]()
![]()
![]()
![]()
![]()
Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the clearRect() method.
Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.
Definition and Usage
The clearRect() method clears the specified pixels within a given rectangle.
| JavaScript Syntax: | context.clearRect(x,y,width,height); |
|---|
Parameter Values
| Parameter | Description |
|---|---|
| x | The x-coordinate of the upper-left corner of the rectangle to clear. |
| y | The y-coordinate of the upper-left corner of the rectangle to clear. |
| width | The width of the rectangle to clear, in pixels. |
| height | The height of the rectangle to clear, in pixels. |
Other Extensions
Canvas Object