Coder Social home page Coder Social logo

mighty-peach / react-confetti-explosion Goto Github PK

View Code? Open in Web Editor NEW

This project forked from herrethan/react-confetti-explosion

1.0 0.0 0.0 505 KB

A lightweight css-animation based confetti exploder for React

License: MIT License

JavaScript 5.20% TypeScript 84.90% CSS 5.84% HTML 4.06%

react-confetti-explosion's Introduction

React Confetti Explosion

npm version

This is inspired by this beautiful and oft-used confetti which uses canvas, but equally inspired by how many bad looking CSS examples there are out there. The goal was to create a super lightweight confetti component that would not require canvas, use only CSS for animation, and could also be controlled as an explosion (rather than raining confetti), without the need to write a full-blown particle generator.

Install:

$ yarn add react-confetti-explosion

Usage

import ConfettiExplosion from 'react-confetti-explosion';

function App() {
  const [isExploding, setIsExploding] = React.useState(false);
  return <>{isExploding && <ConfettiExplosion />}</>;
}

Updates in v2.x

  • Remove dependency on @material-ui/styles in favor of react-jss
  • Update peer dependencies to react 18.x
  • Props floorWidth floorHeight -> width height
  • Export ConfettiProps type
  • Extend ConfettiProps with HTMLDivElement attributes (except ref)
  • Support number or string for height, defaulting to 120vh
  • Fix rotation in Safari (thanks to roydigerhund)
  • Use Portal for dom placement
  • Use create-react-app for dev and example app
  • Use rollup for package bundling (down to 31kb, woot!)

Optional Props

Name Type Default Description
particleCount number 100 Total number of particles used. Generally try to stay under 400 for optimal performance.
particleSize number 12 Size of particles in pixels. This means width for squares, diameter for circles. Note there is also a bit of randomness added to these.
duration number 2200 Duration of explosion in ms. This is the time it takes particles to travel from explosion point to the floor, as defined by height.
onComplete function undefined Function that is called at end of duration
zIndex number undefined zIndex that will be applied to the explosion, in case higher Portal stacking order is required.
colors string[] [
'#FFC700',
'#FF0000',
'#2E3191',
'#41BBC7'
]
An array of any css-readable colors, which are evenly distributed across the number of total particles.
force number 0.5 Between 0-1, roughly the vertical force at which particles initially explode. Straying too far away from 0.5 may start looking...interesting.
height number string '120vh' Pixel distance the particles will vertically spread from initial explosion point.
width number 1000 Pixel distance the particles will horizontally spread from initial explosion point.

Although the above properties of the explosion is controlled, mounting/unmounting is entirely left to the consumer.

Potential gotchas

  • The height is defaulted to 120vh in an attempt to guarantee particles land past the bottom of the viewport, which may not be ideal in some scenarios.
  • If using a string for relative height (vh or % for example), the relative height changes between mobile/desktop screens, which means different looking explosions.
  • If your height is too small, particles may visibly land on the floor instead of disappearing off-screen.
  • The ConfettiExplosion is appended to the document body via ReactDOM.createPortal() to ensure it is positioned correctly and layered above all page content. Make sure to remember to unmount it when your explosion is finished!

To keep the library as little as possible much of the physics have been estimated, cheapened, and downright mutilated. There are certainly prop combinations that will not look realistic, due to the limitations of CSS animations. But there should be enough options to fit most needs.

Examples

See src/example.tsx as an example app, bootstrapped with Create React App.

This can be run locally with yarn start.

Large explosion

{
  force: 0.8,
  duration: 3000,
  particleCount: 250,
  width: 1600,
}

confetti-large-edit

Medium explosion

{
  force: 0.6,
  duration: 2500,
  particleCount: 80,
  width: 1000,
}

confetti-small-edit

Small explosion

{
  force: 0.4,
  duration: 2200,
  particleCount: 30,
  width: 400,
}

confetti-tiny

Author

herrethan

License

MIT

react-confetti-explosion's People

Contributors

herrethan avatar mighty-peach avatar

Stargazers

Magoz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.