Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML canvas clearRect() Method

HTML canvas clearRect() Method

HTML canvas Reference HTML canvas Reference Manual

Example

Clears a rectangle within a given rectangle:

YourbrowserdoesnotsupporttheHTML5canvastag.

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 ExplorerFirefoxOperaGoogle ChromeSafari

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 HTML canvas Reference Manual