neoconfetti
repository·main·Indexed 43 days ago
https://github.com/puruvj/neoconfettiA multi-framework confetti explosion library for high-performance visual effects. It provides dedicated packages for React (@neoconfetti/react), Svelte (@neoconfetti/svelte), and SolidJS (@neoconfetti/solid), as well as the svelte-confetti-explosion package. Features include customizable particle count, size, shape, colors, and physics, with built-in SSR support and memory management via the destroyAfterDone option.
What's inside neoconfetti
- neoconfetti is a library that provides confetti explosion effects compatible with multiple frontend frameworks. It is distributed as a monorepo with framework-specific packages to ensure seamless integration.
Understand Confetti reactivity and performance
mainReactivity
Changing options affects the animation in different ways:
- Changing
particleCount: Destroys existing confetti mid-flight and creates a new explosion with the new options. - Changing
colorsorparticleShape: IfparticleCountremains unchanged, particles will change their colors or shapes mid-flight without restarting the explosion.
Performance
- DOM Nodes: The library creates 2 DOM nodes for every single confetti. For example, a
particleCountof 150 creates 300 nodes. High counts (e.g., 400 or 500) may impact performance on some devices. - Memory Management: By default,
destroyAfterDoneistrue, meaning nodes are removed after the duration to free memory. Set it tofalseif you need to keep them.
- Changing
Manage Confetti lifecycle and options
mainDestroying confetti
You must call
confetti.destroy()when the confetti is no longer needed to clean up resources.Updating options
You can update configuration dynamically:
- Merge options: Assign to
confetti.options.colors(or other specific properties) to merge changes with existing settings. Note that changingparticleCountwill destroy existing particles and restart the animation with new settings. - Override options: Assign a new object to
confetti.optionsto completely replace the current configuration.
Waiting for completion
Since
explode()returns a Promise, you can useawaitto perform actions only after the animation finishes.import { Confetti } from '@neoconfetti/vanilla'; const confetti = new Confetti(document.querySelector('#confetti'), { color: ['#ff0000', '#00ff00', '#0000ff'], force: 0.9, }); // Update specific options (merged) confetti.options.colors = ['white', 'black']; // Completely override existing options confetti.options = { colors: ['white', 'black'] }; // Cleanup confetti.destroy(); // Wait for finish await confetti.explode();- Merge options: Assign to
Performance considerations for @neoconfetti/svelte
mainDOM Node Count
This library creates 2 DOM nodes for every single confetti particle. For the default
particleCountof 150, this results in 300 DOM nodes. While manageable for most devices, be cautious when using high counts (e.g., 400 or 500) on lower-end hardware.Memory Management
By default,
destroyAfterDoneis set totrue, meaning all confetti DOM nodes are removed after thedurationexpires to prevent memory leaks. If you need the particles to persist, setdestroyAfterDone: false.Performance and Memory Management with @neoconfetti/vue
mainDOM Node Count
This library creates 2 DOM nodes for every single confetti particle. For the default
particleCountof 150, this results in 300 DOM nodes. If you increaseparticleCountto high values (e.g., 400 or 500), monitor the performance on target devices.Memory Cleanup
To prevent memory leaks, the library destroys all confetti DOM nodes after the
durationhas passed. If you need to keep the nodes in the DOM, setdestroyAfterDone: falsein your options object.Performance considerations for svelte-confetti-explosion
mainDOM Node Count
This library creates 2 DOM nodes for every single confetti particle. For example, a
particleCountof 150 results in 300 DOM nodes. While modern devices handle this well, be cautious when using high counts (e.g., 400 or 500) on lower-end target devices.Memory Management
By default,
destroyAfterDoneis set totrue, meaning all confetti DOM nodes are removed after thedurationexpires to free up memory. If you need the particles to remain on screen, setdestroyAfterDone={false}.Install dependencies for the Solid demo
mainTo set up the Solid demo project, install the dependencies using your preferred package manager. While the template includes a
pnpm-lock.yamlfile (maintained viapnpm up -Lri), you can usenpm,pnpm, oryarnto install.$ npm install # or pnpm install or yarn installInstall @neoconfetti/vue
mainYou can install the Vue integration for neoconfetti using your preferred package manager.
# pnpm pnpm add @neoconfetti/vue # npm npm install @neoconfetti/vue # yarn yarn add @neoconfetti/vueInstall @neoconfetti/solid
mainInstall the SolidJS confetti package using your preferred package manager.
# pnpm pnpm add @neoconfetti/solid # npm npm install @neoconfetti/solid # yarn yarn add @neoconfetti/solidUse the confetti Svelte action
mainTo trigger a confetti explosion, use the
confettiaction on a Svelte element via theuse:confettidirective. This works seamlessly in SSR environments like SvelteKit.Basic Usage
<script> import { confetti } from '@neoconfetti/svelte'; </script> <div use:confetti />Customizing Behavior
Pass an options object to the action to customize the explosion:
<div use:confetti={{ particleCount: 200, force: 0.3 }} />Use @neoconfetti/vanilla for confetti explosions
mainTo use the library, instantiate the
Confetticlass by passing a DOM element as the first argument. You can optionally pass a configuration object as the second argument. Call.explode()to trigger the animation. The.explode()method returns a Promise that resolves when the animation is complete.import { Confetti } from '@neoconfetti/vanilla'; // Basic usage const confetti = new Confetti(document.querySelector('#confetti')); await confetti.explode(); // With options const confetti = new Confetti(document.querySelector('#confetti'), { color: ['#ff0000', '#00ff00', '#0000ff'], force: 0.9, }); confetti.explode();Use the v-confetti directive in Vue
mainThe simplest way to trigger a confetti explosion is by using the
v-confettidirective on an element. ImportvConfettifrom@neoconfetti/vueand apply it to a template element.Basic Usage
<script> import { vConfetti } from '@neoconfetti/vue'; </script> <div v-confetti />Customizing with Options
You can pass an options object directly to the directive to customize the explosion behavior:
<div v-confetti="{ particleCount: 200, force: 0.3 }" />