What is canvas-confetti?
canvas-confetti is a lightweight JavaScript library for creating fun and customizable confetti animations on HTML canvas elements. It is easy to use and provides a variety of options to control the appearance and behavior of the confetti.
What are canvas-confetti's main functionalities?
Basic Confetti
This feature allows you to create a basic confetti animation with default settings. It is the simplest way to add confetti to your web page.
const confetti = require('canvas-confetti');
confetti();
Customizable Confetti
This feature allows you to customize the confetti animation by specifying parameters such as particle count, spread, and origin. This gives you more control over the appearance and behavior of the confetti.
const confetti = require('canvas-confetti');
confetti({
particleCount: 100,
spread: 70,
origin: { y: 0.6 }
});
Confetti with Shapes
This feature allows you to create confetti with different shapes and colors. You can specify an array of shapes and colors to create a more diverse and visually appealing confetti animation.
const confetti = require('canvas-confetti');
confetti({
particleCount: 100,
shapes: ['circle', 'square'],
colors: ['#bb0000', '#ffffff']
});
Fireworks Effect
This feature allows you to create a fireworks-like confetti effect by adjusting parameters such as particle count, spread, and start velocity. This creates a more dynamic and exciting animation.
const confetti = require('canvas-confetti');
confetti({
particleCount: 200,
spread: 160,
startVelocity: 30,
origin: { y: 0.7 }
});
Other packages similar to canvas-confetti
confetti-js
confetti-js is another JavaScript library for creating confetti animations. It is similar to canvas-confetti but offers fewer customization options. It is suitable for simpler confetti effects.
party-js
party-js is a JavaScript library for creating various particle effects, including confetti. It offers more advanced features and customization options compared to canvas-confetti, making it suitable for more complex animations.
tsparticles
tsparticles is a highly customizable JavaScript library for creating particle animations, including confetti. It provides a wide range of options and effects, making it more versatile than canvas-confetti for creating different types of particle animations.
🎊 canvas confetti 🎊

Demo
catdad.github.io/canvas-confetti
Install
You can install this module as a component from NPM:
npm install --save canvas-confetti
You can then require('canvas-confetti');
to use it in your project build. Note: this is a client component, and will not run in Node. You will need to build your project with something like webpack in order to use this.
You can also include this library in your HTML page directly from a CDN:
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.1.0/dist/confetti.browser.min.js"></script>
Note: you should use the latest version at the time that you include your project. You can see all versions on the releases page.
API
When installed from npm
, this library can be required as a client component in your project build. When using the CDN version, it is exposed as a confetti
function on window
.
confetti([options {Object}])
→ Promise|null
confetti
takes a single optional object. When window.Promise
is available, it will return a Promise to let you know when it is done. When promises are not available (like in IE), it will return null
. You can polyfill promises using any of the popular polyfills. You can also provide a promise implementation to confetti
through:
const MyPromise = require('some-promise-lib');
const confetti = require('canvas-confetti');
confetti.Promise = MyPromise;
If you call confetti
multiple times before it is done, it will return the same promise every time. Internally, the same canvas element will be reused, continuing the existing animation with the new confetti added. The promise returned by each call to confetti
will resolve once all animations are done.
options
The confetti
parameter is a single optional options
object, which has the following properties:
particleCount
Integer (default: 50): The number of confetti to launch. More is always fun... but be cool, there's a lot of math involved.
angle
Number (default: 90): The angle in which to launch the confetti, in degrees. 90 is straight up.
spread
Number (default: 45): How far off center the confetti can go, in degrees. 45 means the confetti will launch at the defined angle
plus or minus 22.5 degrees.
startVelocity
Number (default: 45): How fast the confetti will start going, in pixels.
decay
Number (default: 0.9): How quickly the confetti will lose speed. Keep this number between 0 and 1, otherwise the confetti will gain speed. Better yet, just never change it.
gravity
Number (default: 1): How quickly the particles are pulled down. 1 is full gravity, 0.5 is half gravity, etc., but there are no limits. You can even make particles go up if you'd like.
ticks
Number (default: 200): How many times the confetti will move. This is abstract... but play with it if the confetti disappear too quickly for you.
origin
Object: Where to start firing confetti from. Feel free to launch off-screen if you'd like.
origin.x
Number (default: 0.5): The x
position on the page, with 0
being the left edge and 1
being the right edge.
origin.y
Number (default: 0.5): The y
position on the page, with 0
being the top edge and 1
being the bottom edge.
colors
Array<String>: An array of color strings, in the HEX format... you know, like #bada55
.
shapes
Array<String>: An array of shapes for the confetti. The possible values are square
and circle
. The default is to use both shapes in an even mix. You can even change the mix by providing a value such as ['circle', 'circle', 'square']
to use two third circles and one third squares.
zIndex
Integer (default: 100): The confetti should be on top, after all. But if you have a crazy high page, you can set it even higher.
confetti.create(canvas, [globalOptions])
→ function
This method creates an instance of the confetti
function that uses a custom canvas. This is useful if you want to limit the area on your page in which confetti appear. By default, this method will not modify the canvas in any way (other than drawing to it).
Canvas can be misunderstood a bit though, so let me explain why you might want to let the module modify the canvas just a bit. By default, a canvas
is a relatively small image -- somewhere around 300x150, depending on the browser. When you resize it using CSS, this sets the display size of the canvas, but not the image being represented on that canvas. Think of it as loading a 300x150 jpeg image in an img
tag and then setting the CSS for that tag to 1500x600
-- your image will end up stretched and blurry. In the case of a canvas, you need to also set the width of height of the canvas image itself. If you don't want to do that, you can allow confetti
to set it for you.
The following global options are available:
resize
Boolean (default: false): Whether to allow setting the canvas image size, as well as keep it correctly sized if the window changes size (e.g. resiging the window, rotating a mobile device, etc.). By default, the canvas size will not be modified.
useWorker
Boolean (default: false): Whether to use an asynchronous web worker to render the confetti animation, whenever possible. This is turned off by default, meaning that the animation will always execute on the main thread. If turned on and the browser supports it, the animation will execute off of the main thread so that it is not blocking any other work your page needs to do. If it is not supported by the browser, this value will be ignored.
var myCanvas = document.createElement('canvas');
document.appendChild(myCanvas);
var myConfetti = confetti.create(myCanvas, {
resize: true,
useWorker: true
});
myConfetti({
particleCount: 100,
spread: 160
});
confetti.reset()
Stops the animation and clears all confetti, as well as immediately resolves any outstanding promises. In the case of a separate confetti instance created with confetti.create
, that instance will have its own reset
method.
confetti();
setTimeout(() => {
confetti.reset();
}, 100);
var myCanvas = document.createElement('canvas');
document.appendChild(myCanvas);
var myConfetti = confetti.create(myCanvas, { resize: true });
myConfetti();
setTimeout(() => {
myConfetti.reset();
}, 100);
Examples
Launch some confetti the default way:
confetti();
Launch a bunch of confetti:
confetti({
particleCount: 150
});
Launch some confetti really wide:
confetti({
spread: 180
});
Get creative. Launch a small poof of confetti from a random part of the page:
confetti({
particleCount: 100,
startVelocity: 30,
spread: 360,
origin: {
x: Math.random(),
y: Math.random() - 0.2
}
});
I said creative... we can do better. Since it doesn't matter how many times we call confetti
(just the total number of confetti in the air), we can do some fun things, like continuously launch more and more confetti for 30 seconds, from multiple directions:
var duration = 30 * 1000;
var end = Date.now() + duration;
(function frame() {
confetti({
particleCount: 7,
angle: 60,
spread: 55,
origin: { x: 0 }
});
confetti({
particleCount: 7,
angle: 120,
spread: 55,
origin: { x: 1 }
});
if (Date.now() < end) {
requestAnimationFrame(frame);
}
}());