Latest web development tutorials

HTML canvas clearRect () method

Canvas Object Reference Canvas object

Examples

Within a given rectangle empty 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 clearRect () method.

Note: 8 and earlier versions of Internet Explorer do not support the <canvas> element.


Definition and Usage

clearRect () method to specify the empty set of pixels within the rectangle.

JavaScript syntax: context .clearRect (x, y, width, height);

Parameter Value

参数 描述
x 要清除的矩形左上角的 x 坐标。
y 要清除的矩形左上角的 y 坐标。
width 要清除的矩形的宽度,以像素计。
height 要清除的矩形的高度,以像素计。


Canvas Object Reference Canvas object