A useful front-end confetti animation library

(github.com)

482 points | by blini2077 9 days ago

42 comments

  • lukax 9 days ago
    So the trick for performant animation here is to draw on canvas and put the canvas in front of all other elements but disable pointer events on canvas so that you can still interact with the page.
    • ABNW 9 days ago
      Correct! Disable Pointer Events has surprising utility!
      • amelius 9 days ago
        What if you wanted to add the requirement that the user had to click on 1 specific piece of confetti?
        • nicky0 9 days ago
          Then you have a fun programming challenge.
        • MartijnHols 9 days ago
          Add a click event listener to the body and overlay the event-coordinates on the canvas.
          • amelius 9 days ago
            Will the canvas allow you to hit-test the confetti piece given the coordinate?
            • eyelidlessness 9 days ago
              Canvas draws raster images, anything resembling an object in your drawing logic is already tracked separately by necessity. So regardless, you’d presumably check against whatever data model you’re using to determine what to draw.
              • lupire 9 days ago
                By what time the user clicks, there's no reason for the program to need to remember what they drew where.
                • eyelidlessness 9 days ago
                  If that’s the case, what other object with coordinates would you reference on a canvas to determine whether it was clicked?
    • pvorb 7 days ago
      You call it a trick for performant animation, but I couldn't think of any other way to implement something like this. What would a naive implementation look like?
  • jjice 9 days ago
    This reminds me of some good times doing web development in high school in 2015. I made a small website with confetti to ask a girl to homecoming (very nerdy looking back). Back when making a website felt like a superpower to a kid. It seems like it wasn't this package based on age, but it was a really nice animation.

    I love a fun little projects like this that are purely for a good time. That's why I started programming and it's still a driving force.

    • askonomm 9 days ago
      Did it work? Did she say yes?
      • jjice 9 days ago
        She said no - but we went on to be great friends still to this day, so I'd say it did work. We joke about it still.
        • sa-code 9 days ago
          I knew someone that made a PowerPoint presentation for their partner for Valentine's Day. So you're doing great in comparison
          • Atotalnoob 9 days ago
            My online dating profile was a PowerPoint presentation before I met my partner.

            It easily quadrupled my matches.

            • xprn 9 days ago
              And now we live in a world where “powerpoint presentations as dating profiles” are basically the norm
        • oli-g 9 days ago
          That's nice :)

          I'm curious though - did you use tabs or spaces?

          • jjice 9 days ago
            It was definitely tabs - I'll ask her if she's a space kind of gal. Some things can't be compromised!
  • TeMPOraL 9 days ago
    From the demo page:

    > If you happened to get curious and changed the particle count to 400 or so, you saw something disappointing. An even "flattened cone" look to the confetti, making it look way too perfect and ruining the illusion.

    I love it! This kind of attention to detail is rare in this world, and I cherish it wherever I find it - whether it's in statistical visualization, movie props, or website confetti.

    (As a solution, I'd probably go for changing the random distribution directly. I'd check of course, but I'm guessing the real-life distribution approximates a gaussian.)

  • Zebfross 9 days ago
    I added confetti to our salespeople's admin dashboard for when they make a sale. Surprisingly enjoyable and motivating.
  • RamRodification 9 days ago
    I wish they would have called the reset function confetti.resetti()
    • jerf 9 days ago
      This being Javascript, you can at least fix this locally with a simple "confetti.resetti = confetti.reset".

      There are some software engineering costs to this approach, but, as is transparently obvious to all thoughtful observers, the benefits massively outweigh them, so I say go for it.

      • krsdcbl 9 days ago
        omw to "consologgi = console.log"
    • waldothedog 9 days ago
      Somebody give this person a job! If they already have a job, give them a cookie
    • ilaksh 8 days ago
      maybe you could make a PR
  • fromwilliam 9 days ago
    Aside from being a cool and useful library, this is a good example of what John Ousterhout calls "deep modules" in Philosophy of Software Design.

    It's very easy to use the most basic version of this library (summon confetti) but you can get a lot out of it by exploring the options presented (snow, specific colours, different confetti effects, etc.).

  • thih9 9 days ago
    This is cool and impressive.

    At the same time I don't want to see it running on any website that I use. And especially - I wouldn't want to see confetti accompany newsletter popups or adding products to basket.

    • EMM_386 9 days ago
      The strange thing is this can be used effectively. I don't know about full-screen like this, but we were visiting a client recently who used a certain project management software and when you closed an item out, the button changed to green and had this effect on it.

      It was subtle, but noticeable enough that after the meeting another developer and I both said "that was a pretty neat effect". It conveyed the sense of "yay, progress!".

      Just make it optional.

      • jihadjihad 9 days ago
        Asana does that, and it's optional. I like it whenever it happens too!
    • Andrex 9 days ago
      I think the only "legitimate" use would be something like YouTube's like button, which has a cool animation (and vibrates the device if using the mobile app). Very pleasant UX.
      • wasteduniverse 9 days ago
        Having the like button do an animation when your video mentions that liking it would be helpful is the only good UX choice I have seen on YouTube since... playlists?
    • bradly 9 days ago
      > https://developer.mozilla.org/en-US/docs/Web/CSS/@media/pref...

      You can site your browser to prefer reduced motion. Site owners and library maintainers should be respecting this when implementing things like confetti. This library in particular has a `disableForReducedMotion` option.

    • huhtenberg 9 days ago
      This has its place, e.g. on a completion of a game or some such.
      • ivanbozic 9 days ago
        Yep, on one of the products I built, I use this when a person subscribes to the paid tier. It's really nice, not too obtrusive, but it's fun! Plus, it only happens on that specific scenario, so it's not overdone.

        Pipedrive CRM also does this when you close a deal, they even previously had a person shooting a hoop or something, that was nice. It's rewarding!

    • memonkey 9 days ago
      We use this library when a person qualifies for something. It's a neat effect to our onboarding flow.
  • lovegrenoble 9 days ago
    You have also Party.js library: https://party.js.org/
    • flanbiscuit 9 days ago
      Ah, but which one is smaller?

      10.4 kB Minified, 4.2kB Minified + Gzipped https://bundlephobia.com/package/canvas-confetti@1.9.2

      28.3kB Minified, 7.4kB Minified + Gzipped https://bundlephobia.com/package/party-js@2.2.0

      caveat: I don't know how bundlephobia works, it might not give the best representation of the final size of the package. Probably doesn't take into account code-splitting or being able to import just what you need. I'm just using it as a quick and general overview.

      Looks like confetti one beats it by a few KB gzipped so either works depending on which one has the feature you need, unless you're really trying to squeeze those kb.

      • scoot 9 days ago
        >Probably doesn't take into account code-splitting or being able to import just what you need

        Correct. It doesn't.

    • taikahessu 9 days ago
      OP script feels way more performant on mobile.
      • apocalyptic0n3 9 days ago
        And on Firefox for Mac. Noticed dropped frames pretty consistently in party.js. No such problem with Canvas Confetti, even when there are significantly more particles on screen. I had to increase the count to a point where I couldn't see gaps between them before I saw any lag whatsoever.
      • baobabKoodaa 9 days ago
        OP is more performant on desktop Windows as well. The other one visibly lags when animating even a single cannon of confetti.
    • henriquecm8 9 days ago
      OP's library feels a lot more performant, in my old work computer with 3 click you can notice some lag with Party.js. With canvas-confetti, it only starts to lag when I click non-stop for a few seconds, probably invoking more than 30 instances of confetti and a lot of particles.
  • albert_e 9 days ago
    I solve crosswords on downforacross.com and solving a puzzle there results in confetti.

    Maybe they could use some of this better performing code to make it feel lighter.

    Other than "fun" sites and sparing usage ... I wouldn't want to see such animations everywhere though.

  • humanfromearth9 9 days ago
    I think there is no need to specify 'useful' in the title.
    • refset 9 days ago
      How about as a motivational aid and means of verifying that your code has compiled: https://squint-cljs.github.io/squint/
    • meiraleal 9 days ago
      True. But also the use of the word made me genuinely interested and I laughed by how not really useful it is. Upvoted.
    • fsckboy 9 days ago
      it's as useful as actual confetti irl, so: 100%
  • devsatish 9 days ago
    This is elegant. Shows the passion that goes into building some thing that looks so beautiful and easy to integrate.
  • h1fra 9 days ago
    Very nice, not a fan of this kind of animation but at least it's smooth!
  • wackget 9 days ago
    Useful for annoying your visitors?

    Why on earth would they not enable the setting which honours the `prefers-reduces-motion` media flag for users who don't want stuff flying all over their screens?!

    • atonse 9 days ago
      They have it in the Readme where they're considering making it a default in the future.
    • kieloo 9 days ago
      Agreed. I see really no good reason not to enable this by default.
  • hnben 8 days ago
    to turn it into a bookmarklet, do this:

    1. grab the minified js from the CDM, i.e. https://cdn.jsdelivr.net/npm/canvas-confetti@1.9.2/dist/conf...

    2. remove the comments at the start and end

    3. paste it into the template below

        javascript: (async () => { /* paste minified JS here */   ; confetti();})();
    
    
    4. (optional) customize the confetti()-call

    5. copy the result and paste it into bookmark

    This works on most pages (tested on some tabs I have open), but it does not seem to work on hackernews itself. Here it gives me ContentSecurityPolicy-Error when creating the worker. (at least it does in edge)

  • guappa 9 days ago
    Just fyi, confetti are candies in italy. Not bits of paper.
    • drivers99 9 days ago
      Interesting. It's also cognate with "confection". https://www.etymonline.com/search?q=confetti

      > 1815, "small pellets made of lime or soft plaster, used in Italy during carnival by the revelers for pelting one another in the streets," [...] the custom was adopted in England by early 19c. for weddings and other occasions, with symbolic tossing of little bits of paper (which are called confetti by 1846).

    • ale42 9 days ago
      Can someone code and make a pull request to properly handle display of those for Italian users? (see https://it.wikipedia.org/wiki/Confetto for what they look like)
      • TeMPOraL 9 days ago
        Ah, so basically the same thing, but they hit much harder when they land?
        • ale42 9 days ago
          I guess they also fall down much more quickly
          • guappa 9 days ago
            Not if there is no atmosphere!
            • ale42 6 days ago
              Probably not the typical use case, though ;-)
  • rockwotj 9 days ago
    Used this to build a confetti inbox for Shortwave's April fools [1] a few years back. Really run library, and easy to integrate.

    [1]: https://www.youtube.com/watch?v=_ROTg3KcoIA

  • jaffathecake 9 days ago
    I used this in a prototype recently and was impressed by how configurable it is.
  • languagehacker 9 days ago
    Cute animation, but I find it hard to say that the confetti animation is useful
    • bangaroo 9 days ago
      this says more about you than it does about the confetti animation
      • kaptainscarlet 9 days ago
        Twitter uses a similar animation on your profile when it's your birthday. That's one use case and I am sure there are more.
        • TeMPOraL 9 days ago
          I think Facebook did something similar if you typed "congratulations" in a comment.
  • kc10 9 days ago
    We built similar animation part of a product few years ago. The flow was something like - when a new user signs up and users our product for the first time and creates certain artifact, the confetti animation would be displayed. Product managers loved it and they would show it off to execs as playful, refreshing, etc. But later on after UX reviews, accessibility testing, the feature was ultimately removed from the product.

    It was fun to present it in demos, but it can also be annoying to users.

  • graypegg 9 days ago
    I love this. Lovely work! I had done something similar a few years ago for an edtech company I was working at. [0] It uses threejs and tries to "bake" the particle animation before trying to play it, but I don't think it ever made a huge difference in performance. I think I'll reach out and mention they could change to your library for a nicer experience!

    [0] https://github.com/graypegg/xello-confetti

  • __MatrixMan__ 9 days ago
    Wouldn't it be crazy if there was a snow storm that had so many flakes, the gravitational constant was reduced because of the extra time it took to render them all?
    • seabass-labrax 9 days ago
      The excellent performance characteristics of the natural world may indeed be the best rebuttal of the popular theory that we live in a computer simulation!
      • yetihehe 9 days ago
        Good simulations have constant time step and may not run in real time. From inside such simulation, you would never know.
        • __MatrixMan__ 9 days ago
          It's only meaningful to consider that our world is a simulation if it is an imperfect one, otherwise you're just using "simulation" as an awkward synonym for "reality".

          Click the "snow" button enough (https://catdad.github.io/canvas-confetti/) and you'll get a horizontal line separating the pre-slowdown flakes from the post-slowdown flakes. I suppose that's the kind of simulation imperfection that we might look for.

          Personally, I don't think we're hacking our way out of this one any time soon, so I'm happy to just call it reality.

          • OkayPhysicist 9 days ago
            What would it even mean for a simulation to be imperfect, though, from the perspective within the simulation? You can only observe the simulated phenomena. So it would be perfectly normal, say, if things become non-deterministic at the hardest to observe small scales, or if there were minor inconsistencies between the smallest scale behaviors and the largest. You'd just call it "physics".
            • __MatrixMan__ 9 days ago
              See that's hard because I do call it physics, and I do not call it a simulation. I am here in this world, and from where I stand it's as real as anything will ever be for me.

              My point is that I don't think there's any sense in entertaining counterfactuals that, if true, will be impossible to come up with evidence for, and I think the assertion that our world is a simulation is one such counterfactual.

              That is, unless the physics gets so absolutely insane that "it's just physics" fails to scratch the itch. One example would be if we discover an artifact that lets us see each particle's corresponding unique ID such that, once we have that ID, we can then type it into a console and arbitrarily set properties like its mass.

              If simulated entities gain control over the parameters that govern the simulation itself... well is it really a simulation anymore?

              • yetihehe 9 days ago
                How to detect imperfect simulation: some unexplainable missing wavelength bands, or quantisation of results where it should be continuous would hint at "cutting corners" in simulation, like steps in energy levels from very distant xray sources perhaps?

                As far as we know, our "physics" does not show any possible imperfections, or we didn't thought of all imperfections which could arise in simulation.

                • __MatrixMan__ 8 days ago
                  I think you're setting the bar far too low. An inability to explain why certain frequencies produce no EM radiation would be unsettling, but maybe reality is unsettling. It would not be enough to jump to the conclusion that nothing we've ever measured is actually real.
        • braden-lk 9 days ago
          I was introduced to this idea by the book Permutation City. Great read.
          • marcosdumay 9 days ago
            The idea on Permutation City is way crazier than a mere computer simulation.
            • __MatrixMan__ 9 days ago
              I agree, it's awesome. I wish more scifi authors were as ambitious as Egan.
  • carimura 9 days ago
    I remember putting falling snow on our ecommerce site in like 2005 and just how amazing that feeling was. Oh how far we've come! sort of!
  • eternityforest 5 days ago
    Thanks for the great library! I'm using it for a dashboard framework plugin, I think there's a lot of times when confetti is totally appropriate.
  • DowagerDave 9 days ago
    This is one of the most point-less / awesomest things I've seen on the internet this week. I can't imagine how much work went into building it, and how much time I will now be spending to incorporate into something that definitely doesn't need it.

    Thanks!

  • scoot 9 days ago
    Interesting optical illusion – if I focus on side of the Custom Canvas demo and repeatedly click "Run", some of the confetti in my peripheral vision appears to leave the canvas. It's as if my brain is filling in what it expects to happen.

    Is that a known phenomenon?

    • scoot 8 days ago
      "on one side"...
  • lpapez 9 days ago
    This reminds me of the early Internet in the 90s and 2000s, you had falling snow and star animations everywhere.

    This is a Javascript library, but it couldn't have been JS back in those days.

    Anyone have an idea how people built stuff like this without JS? I'm curious.

    • isp 9 days ago
      JavaScript has been about since 1995, and certainly by no later than the early 2000s was used for these animations.
    • Rumudiez 9 days ago
      tiled gifs, most likely
  • GrantMoyer 9 days ago
    The effect is surprisingly smooth on my outdated mobile phone. I recently implemented a similar effect with a hardware accelerated 3D rendering API (albiet with limited collision physics), and it didn't perform nearly as well targeting WebGL.
  • ericol 9 days ago
    Go multi click on the run button for the "School Pride" demo. I double dare you.
    • eddieroger 9 days ago
      Challenge accepted. I clicked until the confetti froze up. I was impressed while I couldn't trigger new confetti of any kind, the page still scrolled, and the tab could be closed without killing my browser. It was fun.
    • OkayPhysicist 9 days ago
      Hell, they let you edit the code samples. Slap the code in a for(let i = 0; i < 100; i++){

      and watch the magic. Really impressive how it doesn't slow down the rest of page.

  • SchizoDuckie 9 days ago
    I use this on unicornpoep.nl, a little multiple choice toy i built for my daughter to teach her multiplication tables.

    This library is an awesome chromebook killer. I need to find something more optimized. But it does the trick!

  • naikrovek 9 days ago
    Disclaimer: I am old.

    I have a difficult time believing that there is any utility in a confetti animation library when that confetti is shown over a page in a web browser.

    What could the utility of showing confetti over a webpage possibly be?

    • kosolam 9 days ago
      Same utility as animations in general. Such animation as this one for example fits when a user receives something or solves a challenge and similar
      • naikrovek 9 days ago
        Those don’t have utility, either.

        I don’t know why web devs feel like they need to tell users when they’ve succeeded at something like clicking a button.

        People understand when they’ve accomplished something. We don’t need rituals around this, even simple ones like this.

  • OisinMoran 9 days ago
    Neat! Would love to see a persistent option in which the confetti gathers at the bottom of the screen/page. Even better if there was something that could persist all the confetti received across all websites!
  • AndrewHart 4 days ago
    Do you track how many people clicked each confetti button?
  • 0xFazio 9 days ago
    Great job! Nice and realistic. Did you build Framework integrator like useEffect for React or Directive for Angular? It could be great to enforce developper adoption.
  • strongpigeon 9 days ago
    This is really good. That image you have on the README now makes me want to have a depth of field effect on the confettis as well as multiple layers!
  • dangoodmanUT 9 days ago
    This is great, has a ton of options and other solutions are way more laggy
  • throwaway38375 9 days ago
    Neat library. Also, brilliant docs!
  • swah 9 days ago
    Looks a lot like Raycast one :)
  • lngarner 9 days ago
    Interesting!
  • bitter69 9 days ago
    [dead]
  • colesantiago 9 days ago
    [flagged]
    • MatthiasPortzel 9 days ago
      I pressed the power button on my monitor, and now the website is all black.
      • helboi4 9 days ago
        seriously what is up with people who believe JS should not exist on the web? We are way past that now. It's not gonna happen. Reactive apps are just required for some parts of the modern web.
        • drivers99 9 days ago
          > what is up with people who believe JS should not exist on the web

          They said it: privacy

          • naikrovek 9 days ago
            Yep, privacy.

            Those of you who work writing javascript to fingerprint browsers and collect user behavior in exchange for a paycheck, you are the worst kind of person.

            There are a lot of you on here and I expect to be "punished" for this view, but my view on this will never change.

            • MatthiasPortzel 8 days ago
              The push-back is not from the tiny number of JavaScript developers writing finger-printing scripts, but rather the huge number of JavaScript developers (including me) who are offended that you’re unwilling to run JS because you’re afraid that it’s going to finger-print you.

              You have a right to take steps that make you comfortable that your privacy is being protected. But don’t complain about websites not working if you’ve decided you don’t trust them to run JavaScript.

      • justneedaname 9 days ago
        Thank you for the laugh haha
      • kaptainscarlet 9 days ago
        I threw away my laptop, now the website is completely gone.
    • cseleborg 9 days ago
      The developer has of course provided a print stylesheet (expect no less from a pro). Just print it, frame it, hang it up on the wall and voilà! A no-JS confetti demo right there in your living room.
      • graypegg 9 days ago
        Sadly, the demo does require scissors and a colour printer/coloured markers to experience fully.
    • lambaro 9 days ago
      Hard to get much more obvious than that. How else did you think this was implemented as a front-end library?
      • chedabob 9 days ago
        If someone told me it was pure CSS, I'd believe them. I'm not a frontend dev, so I'm always impressed by what you can now do without having to import a whole JS library.
    • jazzyjackson 9 days ago
      now that you mention it it would be neat to implement this as a bunch of overlapping gifs or a fanciful css animation
    • Zenzero 9 days ago
      I haven't understood this approach. If you're going as far as not using js, do you not use a mobile phone? No loyalty programs? No credit cards? How intense is your approach towards security?