1.0.2 • Published 2 years ago

use-confetti-svg v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

use-confetti-svg

React hook to display a confetti animation with a custom SVG particle image

This hook makes it easy to add a simple confetti animation with custom SVG particles to any page. It is designed with transitions in mind as it supports fading out during the animation. There are several props that allow you to control the speed, duration, and size of the particles. All of these are explored in the included demo.

The confetti animation in this library is based on the excellent confetti-js library.

Installation

npm install --save use-confetti-svg

Using the demo

  1. Checkout this repository

  2. Run the below command to start the server on port 3010.

npm run example

Edit use-confetti-svg example

Reference

Inputs

This hook accepts the following options:

OptionTypeDefaultDefinition
imagesConfettiImage[]REQUIREDAn array of particle images to use for the confetti animation. See below for ConfettiImage properties.
durationnumber (ms)2000How long to run the confetti animation for
fadeOutnumber (ms) | false1000When to begin fading out the confetti animation. The fade-out will begin at the specified time and complete at the specified duration value. Specify false to disable the fade-out.
particleCountnumber50Total number of particles to render at one time
speednumber50How fast the confetti particles should "fall"
rotatebooleanfalseWhether to rotate the particle images while they are falling

Confetti image object

OptionTypeDefaultDefinition
srcstringREQUIREDFull path to the SVG image
sizenumber24Size of the SVG image in px
weightnumber1Numeric value that controls how often to display this image relative to others. For instance, if there are two images with weights of 50 and 100, the second image will be rendered twice as often.

Outputs

This hook returns the following properties:

runAnimation: () => Promise<void>

runAnimation async function

This function runs the confetti animation and returns a promise that resolves when the animation completes.

Example

This is just a simple example that runs the confetti animation with two different particle images. The animation runs for 3 seconds total, with a fade-out beginning at 2 seconds.

import React from "react";
import { runAnimation } from "use-confetti-svg";

export default function App() {
    const [animating, setAnimating] = React.useState(false);

    const { runAnimation } = useConfetti({
        images: [
            {
                src: '/img/snowflake.svg',
                size: 32,
                weight: 5,
            },
            {
                src: '/img/bolt.svg',
                size: 40,
                weight: 3,
            },
        ],
        duration: 3000,
        fadeOut: 2000,
    });
    
    const handleRunAnimation = () => {
        setAnimating(true);
        runAnimation().then(() => {
            setAnimating(false);
        })
    }

    return (
        <div>
            <button
                onClick={handleRunAnimation}
                disabled={animating}
            >
                Run animation
            </button>
        </div>
    );
}

Note

Feel free to submit issues/PR's and I will do my best to respond.

License

This project is licensed under the terms of the MIT license.