Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

HTML Canvas clearRect() 方法


clearRect() 方法

clearRect() 方法用于清除画布上的矩形区域。清除的矩形是透明的。

clearRect() 方法具有以下参数

参数 描述
x 必需。矩形左上角的 x 坐标
y 必需。矩形左上角的 y 坐标
宽度 必需。矩形的宽度(以像素为单位)
高度 必需。矩形的高度(以像素为单位)

示例

使用 fillRect() 绘制一个填充的 150*100 像素矩形,从位置 (10,10) 开始。然后使用 clearRect() 清除画布上的矩形区域

抱歉,您的浏览器不支持 canvas。
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.fillStyle = "pink";
ctx.fillRect(10,10, 150,100);

ctx.clearRect(60,35, 50,50);
</script>
自己试试 »


示例

这里清除的矩形超出了粉色矩形

抱歉,您的浏览器不支持 canvas。
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.fillStyle = "pink";
ctx.fillRect(10,10, 150,100);

ctx.clearRect(60,35, 150,50);
</script>
自己试试 »

示例

这里我们使用 clearRect() 方法清除描边矩形中的矩形区域

抱歉,您的浏览器不支持 canvas。
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.strokeStyle = "blue";
ctx.strokeRect(10,10, 150,100);

ctx.clearRect(60,35, 150,50);
</script>
自己试试 »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.