npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-confetti-svg

v1.0.2

Published

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

Downloads

89

Readme

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:

| Option | Type | Default | Definition | |---------------|-------------------------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | images | ConfettiImage[] | REQUIRED | An array of particle images to use for the confetti animation. See below for ConfettiImage properties. | | duration | number (ms) | 2000 | How long to run the confetti animation for | | fadeOut | number (ms) | false | 1000 | When 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. | | particleCount | number | 50 | Total number of particles to render at one time | | speed | number | 50 | How fast the confetti particles should "fall" | | rotate | boolean | false | Whether to rotate the particle images while they are falling |

Confetti image object

| Option | Type | Default | Definition | |--------|----------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | src | string | REQUIRED | Full path to the SVG image | | size | number | 24 | Size of the SVG image in px | | weight | number | 1 | Numeric 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.