loonywizard / js-confetti

JS Confetti library that supports emojis 🦄 🎉 ⚡️
https://loonywizard.github.io/js-confetti/
MIT License
1.13k stars 48 forks source link

Better cleanup #57

Open danmindru opened 1 year ago

danmindru commented 1 year ago

hey Vladimir, Thanks for the neat little lib!

Saw the implementation of clearCanvas() based on https://github.com/loonywizard/js-confetti/issues/46, but this is not exactly what @lifenautjoe was asking for, I think.

clearCanvas() removes the confetti but the canvases keep stacking. So if you have a button that shows confetti you'll get a bunch of empty canvases.

Maybe we need a destroyCanvas() or something like that? Alternatively clearCanvas() could also remove the canvas element from the DOM. Happy to raise a PR if you want to get it in.

Screenshot 2023-05-22 at 10 47 23
ghost commented 1 year ago

I fixed this using

React.useEffect(() => {
    setJsConfetti(new JSConfetti());
  }, []);

  function fire() {
    if (jsConfetti) {
      jsConfetti.addConfetti({
        emojis: ["✨", "💫", "🌈", "🍉"],
        emojiSize: 60,
        confettiNumber: 20,
      });
      // enable vibration support
      navigator.vibrate =
        navigator.vibrate ||
        navigator.webkitVibrate ||
        navigator.mozVibrate ||
        navigator.msVibrate;

      if (navigator.vibrate) {
        // vibration API supported
        navigator.vibrate(100);
      }
    }
    if (jsConfetti) {
      jsConfetti.addConfetti({
        emojis: ["✨", "💫", "🌈", "🍉"],
        emojiSize: 60,
        confettiNumber: 20,
      });
    }
  }