javascript - 使用jquery,如何清除 Canvas 中的项目?

标签 javascript jquery canvas

我是using this jquery code页面上掉落五彩纸屑。这里is the demo page

它运行良好,我发现我可以启动和停止函数来控制更新的发生,但我没有看到“clear()”方法可以从 Canvas 上删除所有五彩纸屑。

下面是我的 html,我的 Canvas 上有一个背景,我不希望将其删除,但我只是想要一种方法来清除 Canvas 上的所有五彩纸屑,这样我只能看到背景图像。

 <canvas style="background-repeat:no-repeat;background-image: url('/Content/Images/myImage.png');" id="confetti" width="1" height="1"></canvas>

最佳答案

您可以自己添加一个clear():

// Somewhere inside this
confetti.Context = function (id) {
  ...
  this.clear = function () {
    this.stop();
    context.clearRect(0, 0, canvas.width, canvas.height);
  }
  ...
}

然后可以在代码中的其他任何地方调用它,例如:

confetti.clear();

关于javascript - 使用jquery,如何清除 Canvas 中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33150278/

相关文章:

javascript - 为什么我的程序没有绘制任何东西?

javascript - 仅在 IE9 中不支持方法 compareDocumentPosition

javascript - 使用 $this 和 .find 在另一个 div 中查找一个 div

javascript - JSON - 在 Internet Explorer 8 中不起作用

使用循环的javascript对象操作

jquery - 如何更改文本区域的占位符?

javascript - 当前创建 Google map 自定义信息窗口的方式

iphone - 等效于 html5 canvas 的 iphone 和 android 的 native 应用程序 API 是什么?

animation - 在 JavaFX Canvas 中移动形状

java - WindowBuilder 设计模式未显示我的 Widget 的视觉特征