Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset added but not working, or i'm stupid ;P #63

Closed
rickerd-treehouse opened this issue Jul 16, 2019 · 2 comments
Closed

Reset added but not working, or i'm stupid ;P #63

rickerd-treehouse opened this issue Jul 16, 2019 · 2 comments
Labels
question Further information is requested

Comments

@rickerd-treehouse
Copy link

rickerd-treehouse commented Jul 16, 2019

Hi, thanks for added the reset!

I know it uses an instance to reset the confetti but what about the example below?
I use that one to create a confetti canon like animation.

How to stop those?

// do this for 30 seconds
var duration = 30 * 1000;
var end = Date.now() + duration;

(function frame() {
  // launch a few confetti from the left edge
  confetti({
    particleCount: 7,
    angle: 60,
    spread: 55,
    origin: { x: 0 }
  });
  // and launch a few from the right edge
  confetti({
    particleCount: 7,
    angle: 120,
    spread: 55,
    origin: { x: 1 }
  });

  // keep going until we are out of time
  if (Date.now() < end) {
    requestAnimationFrame(frame);
  }
}());
@catdad catdad added the question Further information is requested label Jul 16, 2019
@catdad
Copy link
Owner

catdad commented Jul 16, 2019

The example (which is from the demo page) uses code to continue launching more and more confetti. It does so by launching just 7 particles every 16 milliseconds. I assume that in this case, you tried calling confetti.reset() and saw that all the existing confetti went away but immediately, you got 7 more.

In this case, launching more and more confetti is done in your code (the example code in this case) and not in the confetti library, so you need to make sure that your code stops adding more confetti when you want to stop.

You could do something like this:

// do this for 30 seconds
var duration = 30 * 1000;
var end = Date.now() + duration;
var running = true;

(function frame() {
  // launch a few confetti from the left edge
  confetti({
    particleCount: 7,
    angle: 60,
    spread: 55,
    origin: { x: 0 }
  });
  // and launch a few from the right edge
  confetti({
    particleCount: 7,
    angle: 120,
    spread: 55,
    origin: { x: 1 }
  });

  // keep going until we are out of time
  if (Date.now() < end && running) {
    requestAnimationFrame(frame);
  }
}());

// some time later
setTimeout(function () {
  running = false;
  confetti.reset();
}, 2000);

@catdad catdad closed this as completed Jul 22, 2019
@deepansh96
Copy link

For someone who wants to have control on how to stop the confetti creation instantly, you can use this.

var req = null;

(function frame() {
  // launch a few confetti from the left edge
  confetti({
    particleCount: 7,
    angle: 60,
    spread: 55,
    origin: { x: 0 }
  });
  // and launch a few from the right edge
  confetti({
    particleCount: 7,
    angle: 120,
    spread: 55,
    origin: { x: 1 }
  });

  // keep going until we are out of time
  if (Date.now() < end && running) {
    req = requestAnimationFrame(frame);
  }
}());

// now when you want to stop, you can just call this
cancelAnimationFrame(req);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants