From df98fc51015f47e9b0b5717ddaecaa70a73ae7b7 Mon Sep 17 00:00:00 2001 From: Alex Mendes Date: Sat, 5 Sep 2020 17:49:20 +0100 Subject: [PATCH] fix: clear canvas entirely before starting another run --- src/confetti.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/confetti.js b/src/confetti.js index bd9e152..537e09e 100644 --- a/src/confetti.js +++ b/src/confetti.js @@ -92,19 +92,14 @@ export default class Confetti { * The particle options. */ start(opts = {}) { - const canvasEl = this.getCanvasElementFromOptions(opts); + this.remove(); // clear any previous settings - if (!this.canvas || canvasEl !== this.canvasEl) { - this.canvas = new Canvas(canvasEl); - this.canvasEl = canvasEl; - } + const canvasEl = this.getCanvasElementFromOptions(opts); - if (this.animationId) { - cancelAnimationFrame(this.animationId); // Cancel any previous loop - } + this.canvas = new Canvas(canvasEl); + this.canvasEl = canvasEl; this.createParticles(opts); - this.canvas.updateDimensions(); this.setParticlesPerFrame(opts); this.animationId = requestAnimationFrame(this.mainLoop.bind(this)); } @@ -130,10 +125,10 @@ export default class Confetti { update(opts) { const canvasEl = this.getCanvasElementFromOptions(opts); + // Restart if a different canvas is given if (this.canvas && canvasEl !== this.canvasEl) { - this.stop(); - this.canvas.clear(); this.start(opts); + return; } this.setParticlesPerFrame(opts); @@ -149,11 +144,15 @@ export default class Confetti { */ remove() { this.stop(); + if (this.animationId) { cancelAnimationFrame(this.animationId); } - this.canvas.clear(); + if (this.canvas) { + this.canvas.clear(); + } + this.setDefaults(); }