canvas clearRect()
clearRect() 方法可以清除指定矩形區域內的圖案。
更準確的說是清除指定矩形區域內的畫素,此區域完全透明。
語法結構:
[JavaScript] 純文字檢視 複製程式碼context.clearRect(x,y,width,height);
引數解析:
(1).x:必需,要清除矩形區域左上角的x軸座標。
(2).y:必需,要清楚矩形區域的左上角的y軸座標。
(3).width:必需,要清除矩形區域的寬度。
(4).height:必需,要清除矩形區域的高度。
瀏覽器相容:
(1).IE9+瀏覽器支援此屬性。
(2).edge瀏覽器支援此屬性。
(3).谷歌瀏覽器支援此屬性。
(4).火狐瀏覽器支援此屬性。
(5).Opera瀏覽器支援此屬性。
(6).Safari瀏覽器支援此屬性。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="https://www.softwhy.com/" /> <title>螞蟻部落</title> <style> canvas { border:2px dotted #ccc; } </style> <script> window.onload = ()=> { let cvs = document.getElementById('canvas'); let ctx = cvs.getContext("2d"); ctx.fillStyle = "#ccc"; ctx.fillRect(0, 0, 300, 150); ctx.clearRect(20, 20, 100, 50); } </script> </head> <body> <canvas id="canvas"></canvas> </body> </html>
上述程式碼通過clearRect()方法將指定區域內的畫素完全擦除。
程式碼執行效果截圖如下:
再強調一下,方法的功能是將指定區域的畫素擦除,所以指定區域完全透明。
相關文章
- canvas clearRect()清除指定區域內容Canvas
- 使用雙快取解決 Canvas clearRect 引起的閃屏問題快取Canvas
- canvasCanvas
- WPF C# create canvas and draw ellipse in canvasC#Canvas
- canvas rect()Canvas
- canvas strokeStyleCanvas
- canvas lineTo()Canvas
- canvas strokeRect()Canvas
- canvas createRadialGradient()Canvas
- canvas createLinearGradient()Canvas
- canvas arc()Canvas
- canvas stroke()Canvas
- canvas fill()Canvas
- canvas fillStyleCanvas
- canvas setTransform()CanvasORM
- canvas transform()CanvasORM
- 初探canvasCanvas
- canvas closePath()Canvas
- canvas moveTo()Canvas
- canvas beginPath()Canvas
- canvas getContext()CanvasContext
- canvas getImageData()Canvas
- canvas isPointInPath()Canvas
- canvas putImageData()Canvas
- canvas drawImage()Canvas
- canvas clip()Canvas
- canvas arcTo()Canvas
- canvas createPattern()Canvas
- canvas createImageData()Canvas
- canvas restore()CanvasREST
- canvas translate()Canvas
- canvas rotate()Canvas
- canvas scale()Canvas
- canvas measureText()Canvas
- canvas fillText()Canvas
- canvas save()Canvas
- canvas fillRect()Canvas
- canvas(三)Canvas