Top Related Projects
🎉 performant confetti animation in the browser
Confetti without the cleanup.
Quick Overview
JS Confetti is a lightweight JavaScript library for creating confetti animations on web pages. It provides an easy-to-use API for adding colorful, customizable confetti effects to enhance user interactions and celebrations in web applications.
Pros
- Simple and intuitive API for quick implementation
- Customizable confetti appearance (colors, shapes, size)
- Lightweight with no dependencies
- Supports both one-time and continuous confetti animations
Cons
- Limited animation options compared to more complex libraries
- May impact performance on low-end devices if overused
- Lacks advanced features like custom particle shapes or physics simulations
Code Examples
Creating a basic confetti burst:
import JSConfetti from 'js-confetti'
const jsConfetti = new JSConfetti()
jsConfetti.addConfetti()
Customizing confetti colors:
const jsConfetti = new JSConfetti()
jsConfetti.addConfetti({
confettiColors: [
'#ff0a54', '#ff477e', '#ff7096', '#ff85a1', '#fbb1bd', '#f9bec7',
],
})
Creating a confetti explosion with emojis:
const jsConfetti = new JSConfetti()
jsConfetti.addConfetti({
emojis: ['🌈', '⚡️', '💥', '✨', '💫', '🌸'],
})
Getting Started
-
Install the library using npm:
npm install js-confetti
-
Import and initialize JSConfetti in your JavaScript file:
import JSConfetti from 'js-confetti' const jsConfetti = new JSConfetti()
-
Trigger confetti animation:
jsConfetti.addConfetti()
-
Customize the confetti as needed using the available options:
jsConfetti.addConfetti({ confettiNumber: 500, confettiColors: ['#ff0000', '#00ff00', '#0000ff'], })
Competitor Comparisons
🎉 performant confetti animation in the browser
Pros of canvas-confetti
- More customizable with a wider range of options for particle behavior and appearance
- Supports multiple confetti sources and custom shapes
- Better performance for large numbers of particles
Cons of canvas-confetti
- Slightly larger file size
- More complex API, which may be overkill for simple use cases
- Requires more setup code for basic confetti effects
Code Comparison
js-confetti:
import JSConfetti from 'js-confetti'
const jsConfetti = new JSConfetti()
jsConfetti.addConfetti()
canvas-confetti:
import confetti from 'canvas-confetti'
confetti({
particleCount: 100,
spread: 70,
origin: { y: 0.6 }
})
Both libraries offer easy-to-use APIs for adding confetti effects to web applications. js-confetti provides a simpler interface for basic confetti animations, while canvas-confetti offers more advanced customization options.
js-confetti is ideal for quick implementations and straightforward confetti effects. canvas-confetti is better suited for projects requiring fine-tuned control over particle behavior, multiple confetti sources, or custom shapes.
When choosing between the two, consider your project's specific needs, desired level of customization, and performance requirements.
Confetti without the cleanup.
Pros of react-confetti
- Specifically designed for React applications, offering seamless integration
- Provides more customization options and props for fine-tuning confetti behavior
- Supports both canvas and DOM rendering modes
Cons of react-confetti
- Larger package size due to React dependency and additional features
- May have a steeper learning curve for developers not familiar with React
- Performance might be slightly lower compared to vanilla JavaScript solutions
Code Comparison
react-confetti:
import Confetti from 'react-confetti'
<Confetti
width={window.width}
height={window.height}
numberOfPieces={200}
recycle={false}
/>
js-confetti:
import JSConfetti from 'js-confetti'
const jsConfetti = new JSConfetti()
jsConfetti.addConfetti({
confettiNumber: 200,
})
Both libraries offer easy-to-use APIs for adding confetti effects to web applications. react-confetti is more suitable for React-based projects and provides extensive customization options, while js-confetti is a lightweight, framework-agnostic solution that can be easily integrated into any JavaScript project. The choice between the two depends on the specific project requirements, such as the development framework, desired customization level, and performance considerations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ð JavaScript Confetti library
ð¥ Supports emojis as confetti
â¡ï¸ Zero dependencies used
ð¦ Works without any config, yet configurable
ð Has TypeScript typings
𧩠Confetti speed adapts to user screen width
Install
You can install library from NPM using yarn or npm
yarn add js-confetti
Alternatively you can download script from CDN
<script src="https://cdn.jsdelivr.net/npm/js-confetti@latest/dist/js-confetti.browser.js"></script>
and then access JSConfetti
global variable
Usage
Initialize instance of JSConfetti class and call addConfetti method
import JSConfetti from 'js-confetti'
const jsConfetti = new JSConfetti()
jsConfetti.addConfetti()
NOTE new JSConfetti()
creates HTML Canvas element and adds it to page, so call it only once!
If need to use custom canvas element, you can pass canvas
arg to JSConfetti constructor (example)
const canvas = document.getElementById('your_custom_canvas_id')
const jsConfetti = new JSConfetti({ canvas })
Customise confetti
Use emojis as confetti:
jsConfetti.addConfetti({
emojis: ['ð', 'â¡ï¸', 'ð¥', 'â¨', 'ð«', 'ð¸'],
})
Customize confetti colors:
jsConfetti.addConfetti({
confettiColors: [
'#ff0a54', '#ff477e', '#ff7096', '#ff85a1', '#fbb1bd', '#f9bec7',
],
})
Customize confetti radius:
jsConfetti.addConfetti({
confettiRadius: 6,
})
Customize confetti number:
jsConfetti.addConfetti({
confettiRadius: 6,
confettiNumber: 500,
})
Combine different properties:
jsConfetti.addConfetti({
emojis: ['ð¦'],
emojiSize: 100,
confettiNumber: 30,
})
clearCanvas()
Call clearCanvas
method to clear canvas
Example:
const jsConfetti = new JSConfetti()
jsConfetti.addConfetti()
// ...
jsConfetti.clearCanvas()
addConfetti Promise
addConfetti
method returns Promise, which is resolved when added confetti dissapears from the user screen due to the gravity physics of confetti
Example:
// async/await
await jsConfetti.addConfetti()
console.log('Confetti animation completed!')
// Promise.then
jsConfetti.addConfetti()
.then(() => console.log('Confetti animation completed!'))
How to run locally
Install dependencies by Yarn or NPM
yarn install
Run dev
script with website build
yarn run dev
License
MIT
Top Related Projects
🎉 performant confetti animation in the browser
Confetti without the cleanup.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot