Canvas清除画布的3种方法

2020-11-04  本文已影响0人  不负好时光_9c46

一、通过简单填充

使用一个新的背景色简单地填充整个画布,这样就可以清除当前内容

context.fillStyle = '#ffffff';

context.fillRect(0, 0, theCanvas.width, theCanvas.height);

二、重置画布高度

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

 var w = theCanvas.width;

 var h = theCanvas.height;

 theCanvas.width = w;

 theCanvas.height = h;

三、使用clearRect函数

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

var w = theCanvas.width;

var h = theCanvas.height;

context.clearRect(0, 0, w, h);

上一篇 下一篇

猜你喜欢

热点阅读