Giter VIP home page Giter VIP logo

js-confetti's Introduction

npm version NPM Downloads npm bundle size

๐ŸŽ‰ JavaScript Confetti library

๐Ÿ’ฅ Supports emojis as confetti
โšก๏ธ Zero dependencies used
๐Ÿฆ„ Works without any config, yet configurable
๐Ÿ›  Has TypeScript typings
๐Ÿงฉ Confetti speed adapts to user screen width

Links: GitHub | NPM | Demo

Install

You can install library from NPM using yarn or npm

yarn add js-confetti

Alternatively you can download script from CDN

<script src="https://cdn.jsdelivr.net/npm/js-confetti@latest/dist/js-confetti.browser.js"></script>

and then access JSConfetti global variable

Usage

Initialize instance of JSConfetti class and call addConfetti method

import JSConfetti from 'js-confetti'

const jsConfetti = new JSConfetti()

jsConfetti.addConfetti()

NOTE new JSConfetti() creates HTML Canvas element and adds it to page, so call it only once!

If need to use custom canvas element, you can pass canvas arg to JSConfetti constructor (example)

const canvas = document.getElementById('your_custom_canvas_id')

const jsConfetti = new JSConfetti({ canvas })

Customise confetti

Use emojis as confetti:

jsConfetti.addConfetti({
   emojis: ['๐ŸŒˆ', 'โšก๏ธ', '๐Ÿ’ฅ', 'โœจ', '๐Ÿ’ซ', '๐ŸŒธ'],
})

Customize confetti colors:

jsConfetti.addConfetti({
  confettiColors: [
    '#ff0a54', '#ff477e', '#ff7096', '#ff85a1', '#fbb1bd', '#f9bec7',
  ],
})

Customize confetti radius:

jsConfetti.addConfetti({
  confettiRadius: 6,
})

Customize confetti number:

jsConfetti.addConfetti({
  confettiRadius: 6,
  confettiNumber: 500,
})

Combine different properties:

jsConfetti.addConfetti({
  emojis: ['๐Ÿฆ„'],
  emojiSize: 100,
  confettiNumber: 30,
})

clearCanvas()

Call clearCanvas method to clear canvas

Example:

const jsConfetti = new JSConfetti()

jsConfetti.addConfetti()

// ... 
jsConfetti.clearCanvas()

addConfetti Promise

addConfetti method returns Promise, which is resolved when added confetti dissapears from the user screen due to the gravity physics of confetti

Example:

// async/await
await jsConfetti.addConfetti()
console.log('Confetti animation completed!')

// Promise.then
jsConfetti.addConfetti()
   .then(() => console.log('Confetti animation completed!'))

How to run locally

Install dependencies by Yarn or NPM

yarn install

Run dev script with website build

yarn run dev

License

MIT

js-confetti's People

Contributors

a-trost avatar grork avatar halilb avatar jameshfisher avatar loonywizard avatar rubenwerdmuller 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.