Write Code Get Confetti

A complete guide to implementing celebratory animations in JavaScript. From production libraries to custom implementations, with code examples you can use today.

Introduction to Confetti Animations in Web Development

Confetti animations have become a staple of modern web interfaces. When a user completes a purchase, submits a form, or reaches a milestone, a burst of colorful particles creates a moment of delight that users remember long after leaving your site.

Why Confetti Matters for User Experience

Visual feedback goes beyond functional confirmation. When users see confetti after completing an action, they receive both confirmation (the action succeeded) and celebration (the action was worth doing). This combination reinforces desired behaviors and creates positive associations with your brand. Confetti effects are a powerful tool in front-end development that can transform mundane interactions into memorable experiences.

Implementing celebratory micro-interactions is a hallmark of thoughtful UX design that sets your website apart from competitors.

Popular JavaScript Confetti Libraries

Choose the right tool for your project

canvas-confetti

The industry standard library using HTML Canvas API. Highly configurable physics, automatic cleanup, and excellent performance with hundreds of particles.

js-confetti

Zero-dependency alternative with native emoji support. Promise-based API enables easy chaining of celebratory actions.

CSS Confetti

Alternative approach using CSS keyframes for simple, predictable effects without Canvas overhead.

canvas-confetti: The Industry Standard

The canvas-confetti library has become the de facto standard for confetti animations in web applications. It renders confetti using the HTML Canvas API, which provides excellent performance even with hundreds of particles.

Key Configuration Options:

  • particleCount - Number of confetti particles
  • spread - Arc angle in degrees where confetti spreads
  • origin - Launch position using x and y coordinates (0-1 scale)
  • colors - Array of hex color strings
  • disableForReducedMotion - Auto-respect motion preferences
  • useWorker - Offload rendering to a web worker

This library is ideal for complex animations requiring precise control over particle physics and origin positioning in your JavaScript applications. For dynamic user interfaces, canvas-confetti provides the flexibility needed for polished experiences.

Basic canvas-confetti usage
1// Basic usage2import confetti from 'canvas-confetti';3 4// Fire a basic burst5confetti({6 particleCount: 100,7 spread: 70,8 origin: { y: 0.6 }9});10 11// With custom colors12confetti({13 particleCount: 75,14 spread: 70,15 colors: ['#ff0a54', '#ff477e', '#ff7096'],16 origin: { x: 0.5, y: 0.6 }17});

js-confetti: Emoji Support and Zero Dependencies

The js-confetti package offers a simpler alternative with native emoji support and a Promise-based API that makes chaining actions straightforward. This library is perfect for projects prioritizing simplicity over physics customization.

The Promise-based API is particularly useful when you need to sequence multiple animations or trigger actions after the confetti effect completes. Whether you're building e-commerce completion celebrations or gamification features, js-confetti provides an elegant solution. The emoji support adds a playful touch that resonates with users across different cultures and age groups.

For teams looking to quickly add celebratory moments without complex configuration, js-confetti delivers immediate results with minimal code.

js-confetti with emoji support
1import JSConfetti from 'js-confetti';2 3const jsConfetti = new JSConfetti();4 5// Add colorful confetti6jsConfetti.addConfetti({7 confettiColors: ['#ff0a54', '#ff477e', '#ff7096'],8 confettiNumber: 1009});10 11// Or use emojis12jsConfetti.addConfetti({13 emojis: ['🎉', '✨', '💫', '🎊'],14 emojiSize: 50,15 confettiNumber: 3016});17 18// Promise-based API for chaining19await jsConfetti.addConfetti();20console.log('Celebration complete!');

Implementation Patterns and Code Examples

Button Click Celebrations

Trigger confetti on button clicks after successful form submissions or purchases. This pattern is essential for interactive user experiences that celebrate user achievements.

const celebrateButton = document.getElementById('celebrate-btn');

celebrateButton.addEventListener('click', async () => {
 // Process the action first
 const success = await processAction();

 if (success) {
 // Trigger confetti on success
 confetti({
 particleCount: 75,
 spread: 70,
 colors: ['#00ff00', '#ffffff'],
 disableForReducedMotion: true
 });
 }
});
Celebration on successful action
1const celebrateButton = document.getElementById('celebrate-btn');2 3celebrateButton.addEventListener('click', async () => {4 // Process the action first5 const success = await processAction();6 7 if (success) {8 // Trigger confetti on success9 confetti({10 particleCount: 75,11 spread: 70,12 colors: ['#00ff00', '#ffffff'],13 disableForReducedMotion: true14 });15 }16});

Performance Optimization Strategies

Keeping confetti performant across all devices is crucial for user experience. Poorly optimized animations can cause jank and drain battery life on mobile devices.

Particle Count Guidelines

Device TypeRecommended Particle CountNotes
Desktop (high-end)100-150Smooth 60fps typical
Desktop (average)75-100Good balance
Mobile (flagship)50-75Test on target devices
Mobile (budget)25-50Prioritize responsiveness

These guidelines ensure your web applications remain responsive while still delivering delightful experiences across all device types. Pairing optimized confetti with proper frontend architecture creates seamless interactions that users love.

Adaptive particle count for devices
1const getOptimizedParticleCount = () => {2 const isMobile = window.innerWidth < 768;3 const isLowPower = navigator.hardwareConcurrency <= 2;4 5 if (isMobile && isLowPower) return 30;6 if (isMobile) return 50;7 if (isLowPower) return 75;8 return 100;9};10 11confetti({12 particleCount: getOptimizedParticleCount(),13 spread: 7014});

Accessibility and User Preferences

Respecting prefers-reduced-motion

Always check for motion sensitivity before triggering confetti animations. The Window.matchMedia API allows you to detect user preferences.

Accessibility should never be an afterthought in modern web development. By respecting motion preferences, you ensure your celebratory moments don't cause discomfort for users with vestibular disorders. Inclusive design benefits everyone, not just users with specific accessibility needs.

const shouldShowConfetti = () => {
 const prefersReducedMotion = window.matchMedia(
 '(prefers-reduced-motion: reduce)'
 ).matches;

 return !prefersReducedMotion;
};

// Usage with automatic fallback
if (shouldShowConfetti()) {
 confetti({ particleCount: 100 });
} else {
 // Provide alternative feedback (static message, success indicator)
 showSuccessMessage();
}
Motion preference detection
1const shouldShowConfetti = () => {2 const prefersReducedMotion = window.matchMedia(3 '(prefers-reduced-motion: reduce)'4 ).matches;5 6 return !prefersReducedMotion;7};8 9// Usage with automatic fallback10if (shouldShowConfetti()) {11 confetti({ particleCount: 100 });12} else {13 // Provide alternative feedback (static message, success indicator)14 showSuccessMessage();15}

Best Practices for Production Deployments

Strategic Usage Patterns

Confetti effects work best when reserved for genuinely celebratory moments. Overuse leads to user fatigue and diminishes the impact of your celebrations.

Use confetti for:

  • Purchase completion
  • Account creation success
  • Achievement unlocked
  • Milestone reached
  • Form submission confirmation

Avoid confetti for:

  • Every button click
  • Page loads (unless celebrating something specific)
  • Minor interactions
  • Repeated actions within short timeframes

By following these guidelines, you create meaningful moments that enhance user engagement without overwhelming your audience. This strategic approach is essential for building polished digital experiences that users appreciate. When implemented thoughtfully alongside AI-powered automation, confetti becomes part of a cohesive user engagement strategy.

Frequently Asked Questions

Ready to Add Celebratory Moments to Your Web App?

Our team builds engaging, performant web experiences with thoughtful micro-interactions that delight users.

Sources

  1. canvas-confetti GitHub Repository - Industry-standard library documentation and API reference
  2. js-confetti NPM Package - Zero-dependency confetti library with emoji support
  3. OpenReplay: Adding Confetti Effects with JavaScript - Production best practices and implementation patterns
  4. MDN: Window.matchMedia() - API documentation for motion preference detection