Canvas清除画布

一、简单填充

使用白色背景色简单地填充整个画布,可以清除当前内容。

复制代码
var context= getContext("2d");
context.fillStyle = 'white';
context.fillRect(0, 0, theCanvas.width, theCanvas.height);

二、重置画布区域

当画布的宽或高被重置时,当前画布内容就会被移除。

复制代码
 var width = theCanvas.width;
 var height = theCanvas.height;
 theCanvas.width = width;
 theCanvas.height = height;

三、使用clearRect函数

clearRect() 函数指定起始点的x, y 位置以及宽度和高度来清除画布。

复制代码
var context= getContext("2d");
var width = theCanvas.width;
var height = theCanvas.height;
context.clearRect(0, 0, width, height);
相关推荐
万少4 小时前
小龙虾(openclaw),轻松玩转自动发帖
前端·人工智能·后端
Jagger_6 小时前
抱怨到躺床关灯的一次 DIY 记录
前端
陈随易9 小时前
前端大咖mizchi不满Rust、TypeScript却爱上MoonBit
前端·后端·程序员
whinc10 小时前
🚀 两年小程序开发,我把踩过的坑做成了开源 Skills
前端·微信小程序·ai编程
兆子龙11 小时前
ahooks useRequest 深度解析:一个 Hook 搞定所有请求
java·javascript
兆子龙11 小时前
React Suspense 从入门到实战:让异步加载更优雅
java·javascript
sure28211 小时前
React Native中创建自定义渐变色
前端·react native
KKKK12 小时前
SSE(Server-Sent Events)流式传输原理和XStream实践
前端·javascript
子兮曰12 小时前
Humanizer-zh 实战:把 AI 初稿改成“能发布”的技术文章
前端·javascript·后端