Skip to content

Commit

Permalink
Merge pull request #54 from alexandermendes/fix-36
Browse files Browse the repository at this point in the history
fix: clear canvas entirely before starting another run
  • Loading branch information
alexandermendes committed Sep 5, 2020
2 parents da83ade + df98fc5 commit 65bb5da
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/confetti.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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);
Expand All @@ -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();
}

Expand Down

0 comments on commit 65bb5da

Please sign in to comment.