How to use it
1. Add the script
<script src="confetti.js" defer></script>
2. Add one attribute to a button
<button data-confetti>Sign up</button>
The confetti shoots out of the button when it is clicked. It works on links and any other element too.
Three effects
| Attribute | What happens |
|---|---|
data-confetti | A burst that starts at the element. |
data-confetti="sides" | Two cannons shoot from the left and right edge of the window. |
data-confetti="rain" | Confetti falls from the top for three seconds. |
Confetti on a thank-you page
Put this on the body tag of the page that visitors see after an order or a sign-up. It fires once when the page opens.
<body data-confetti-onload="sides">
Options
<button data-confetti
data-confetti-count="200"
data-confetti-colors="#ffd700,#ffb300,#ffffff"
data-confetti-shapes="star,circle">
Claim your prize
</button>| Attribute | Default | What it does |
|---|---|---|
data-confetti-count | 120 | Number of pieces. |
data-confetti-colors | six bright colours | Your colours, separated by commas. |
data-confetti-shapes | rect, circle, strip | Also star, or any emoji. |
data-confetti-spread | 70 | Width of the fan in degrees. 360 goes in all directions. |
data-confetti-power | 1 | How hard it shoots. 0.5 is gentle, 1.5 is strong. |
Emoji instead of paper
<button data-confetti data-confetti-shapes="🎉,⭐,🎈" data-confetti-count="60">Party</button>
Use it from JavaScript
This is what you need after a form was sent or a payment went through.
Confetti.burst(); // from the middle of the window
Confetti.burst(document.querySelector('#buy')); // from an element
Confetti.burst({ x: 0.2, y: 0.5, count: 200, spread: 120 });
Confetti.sides(); // two cannons
Confetti.rain({ seconds: 5 }); // falling confetti
Confetti.clear(); // remove everything at once| Option | Default | What it does |
|---|---|---|
x, y | 0.5, 0.6 | Start point, from 0 to 1 of the window. 0.5 and 0.5 is the middle. |
count | 120 | Number of pieces. |
angle | 90 | Direction in degrees. 90 is up, 0 is right, 180 is left. |
spread | 70 | Width of the fan in degrees. |
power | 1 | How hard it shoots. |
gravity | 1 | How fast it falls. 0.5 floats, 2 drops quickly. |
size | 1 | Size of the pieces. |
duration | 3200 | Life of a piece in milliseconds. |
colors | six bright colours | A list of colours. |
shapes | rect, circle, strip | A list of shapes: rect, circle, strip, star or emoji. |
After a form was sent
form.addEventListener('submit', function (event) {
event.preventDefault();
sendForm().then(function () {
Confetti.sides({ colors: ['#7c5cff', '#22d3ee', '#ffffff'] });
});
});What it takes care of
- It never blocks a click. The confetti layer lets every click and tap through to your page.
- It cleans up. The canvas is created for the burst and removed when the last piece is gone. Between bursts the script uses nothing.
- Reduced motion. Visitors who switched off animations on their device get no confetti. Your button still works as normal.
- Phones. Bursts are a little gentler on small screens, so the pieces stay on screen.
- A limit. There are never more than 1,500 pieces at once, also when someone clicks the button twenty times.
When confetti helps, and when not
- Good: order confirmed, account created, goal reached, discount unlocked. One moment that the visitor worked for.
- Not good: every "add to cart" click, page loads of normal pages, error messages. Confetti that fires all the time stops meaning anything.
Common questions
Does it work with WooCommerce or Shopify?
Yes. Load the script on the order confirmation page and add data-confetti-onload="sides" to that page. In Shopify this goes into the order status page scripts, in WooCommerce into the thank-you template.
The confetti appears behind my popup.
The layer uses a very high z-index, so this is rare. It happens when the popup is inside an iframe. Load the script inside that iframe then.
Can I use my logo as a piece?
Shapes are drawn shapes or text. An emoji or a single letter works. A picture does not.
What is in the download
confetti.jsthe script, about 9 KB with comments. No stylesheet needed.index.htmlanddemo.cssthe demo page you see aboveREADME.txtandLICENSE.txtthe short guide and the MIT licence
Licence
MIT licence. Use it in personal and commercial projects, change it as you like. A link back is welcome and not required.