clearRect() 是 Canvas 2D API 設(shè)置指定矩形區(qū)域內(nèi)(以 點(diǎn) (x, y) 為起點(diǎn),范圍是(width, height) )所有像素變成透明,并擦除之前繪制的所有內(nèi)容的方法。
清除給定矩形內(nèi)的矩形:
<!DOCTYPE html>
<html>
<head>
<title>HTML canvas clearRect() 方法的使用(菜鳥(niǎo)教程 cainiaoplus.com)</title>
</head>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
您的瀏覽器不支持 HTML5 canvas 標(biāo)簽。
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="blue";
ctx.fillRect(0,0,300,150);
ctx.clearRect(20,20,100,50);
</script>
</body>
</html>測(cè)試看看 ?/?IEFirefoxOperaChromeSafari
Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持 clearRect() 方法。
注意:Internet Explorer 8 及之前的版本不支持 <canvas> 元素。
clearRect() 方法清除給定矩形內(nèi)的指定像素。
| JavaScript 語(yǔ)法: | context.clearRect(x,y,width,height); |
|---|
| 參數(shù) | 描述 |
|---|---|
| x | 要清除的矩形左上角的 x 坐標(biāo)。 |
| y | 要清除的矩形左上角的 y 坐標(biāo)。 |
| width | 要清除的矩形的寬度,以像素計(jì)。 |
| height | 要清除的矩形的高度,以像素計(jì)。 |