React Canvas Confetti

2.0.7 · active · verified Tue Apr 21

react-canvas-confetti provides a React-friendly interface for the popular canvas-confetti library, enabling developers to easily integrate various confetti, fireworks, and particle animations into their applications. As of version 2.0.7, it offers two primary usage patterns: a simplified preset system (e.g., `Fireworks`) for common animation templates with customizable settings, and a lower-level API that grants direct access to the `canvas-confetti` instance for creating bespoke animation algorithms. The library ships with TypeScript types, promoting robust development. While no explicit release cadence is documented, its active GitHub repository and npm download statistics suggest consistent maintenance and updates, typically aligning with the underlying `canvas-confetti` library's evolution and React ecosystem best practices. It differentiates itself by abstracting away direct DOM manipulation for canvas elements, providing a declarative component-based approach.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate the `Fireworks` preset component and manually control its animation using the `ConductorInstance` obtained from the `onInit` callback. It showcases single bursts and continuous firing patterns.

import React, { useRef, useCallback, useState } from 'react';
import Fireworks from 'react-canvas-confetti/dist/presets/fireworks';
import type { TConductorInstance } from 'react-canvas-confetti';

function ConfettiDemo() {
  const conductorRef = useRef<TConductorInstance | null>(null);
  const [isRunning, setIsRunning] = useState(false);

  const onInit = useCallback(({ conductor }: { conductor: TConductorInstance }) => {
    conductorRef.current = conductor;
    // You could autorun here as well, but we'll use a button for demonstration
    // conductor.run({ speed: 2 });
    // setIsRunning(true);
  }, []);

  const handleShootConfetti = useCallback(() => {
    if (conductorRef.current) {
      conductorRef.current.shoot();
    }
  }, []);

  const handleRunAnimation = useCallback(() => {
    if (conductorRef.current) {
      conductorRef.current.run({ speed: 1.5, duration: 2000 });
      setIsRunning(true);
    }
  }, []);

  const handleStopAnimation = useCallback(() => {
    if (conductorRef.current) {
      conductorRef.current.stop();
      setIsRunning(false);
    }
  }, []);

  return (
    <div>
      <h1>Confetti Show!</h1>
      <p>Click buttons to control the fireworks animation.</p>
      <button onClick={handleShootConfetti} style={{ margin: '10px', padding: '10px 20px' }}>
        Single Burst
      </button>
      <button onClick={handleRunAnimation} disabled={isRunning} style={{ margin: '10px', padding: '10px 20px' }}>
        Start Continuous
      </button>
      <button onClick={handleStopAnimation} disabled={!isRunning} style={{ margin: '10px', padding: '10px 20px' }}>
        Stop Continuous
      </button>
      {/* The Fireworks component will render the canvas */}
      <Fireworks onInit={onInit} autorun={false} />
      <p style={{ marginTop: '20px' }}>
        This example demonstrates how to control the confetti animation manually using the `ConductorInstance`
        obtained via the `onInit` callback of the `Fireworks` preset component.
        It shows single bursts and continuous animation, offering full control over the visual effects.
      </p>
    </div>
  );
}

export default ConfettiDemo;

view raw JSON →