Canvas clearRect() 方法

定義和用法

clearRect() 方法清空給定矩形內的指定像素。

實例

在給定矩形內清空一個矩形:

Your browser does not support the HTML5 canvas tag.

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);

親自試一試

語法

context.clearRect(x,y,width,height);

參數值

參數 描述
x 要清除的矩形左上角的 x 坐標
y 要清除的矩形左上角的 y 坐標
width 要清除的矩形的寬度,以像素計
height 要清除的矩形的高度,以像素計

瀏覽器支持

表中的數字注明了首個完全支持該屬性的瀏覽器版本。

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
4.0 9.0 3.6 4.0 10.1

注釋:Internet Explorer 8 以及更早的版本不支持 <canvas> 元素。