一、简单填充
使用白色背景色简单地填充整个画布,可以清除当前内容。
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);

本文介绍了如何使用HTML5canvasAPI中的fillRect方法简单填充画布,重置画布区域以清除原有内容,以及利用clearRect函数精确清除指定区域。这些技巧是前端开发者在进行图形绘制时的基础操作。
3487

被折叠的 条评论
为什么被折叠?



