js-confetti icon indicating copy to clipboard operation
js-confetti copied to clipboard

Better cleanup

Open danmindru opened this issue 2 years ago • 1 comments

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

danmindru avatar May 22 '23 08:05 danmindru

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,
      });
    }
  }

ghost avatar Jul 15 '23 10:07 ghost