confetti
A burst of confetti from an element, a shower or fireworks to celebrate a sign-up, an order or a donation.
Your order
- Field notebook, 3-pack$24.00
- Brass pencil$18.00
- ShippingFree
Total$42.00
Live preview needs WebGPU or WebGL 2. A checkout card whose Place order button bursts confetti over the page when clicked.
This example failed to draw.
Or click Place order. Switches and sliders apply to the next burst.
Usage
Put it on any element: a button, a section, a form. start() registers confetti, so the attribute alone puts it on an element. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add confetti<div class="cf-shop"> <section class="cf-order" aria-label="Order summary"> <p class="cf-kicker">Your order</p> <ul class="cf-lines"> <li> <span>Field notebook, 3-pack</span> <span>$24.00</span> </li> <li><span>Brass pencil</span><span>$18.00</span></li> <li><span>Shipping</span><span>Free</span></li> </ul> <p class="cf-total"> <span>Total</span> <span>$42.00</span> </p> <button type="button" class="cf-pay" data-dio="confetti" >Place order</button> <p class="cf-status" role="status"></p> </section></div>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.cf-pay', 'confetti', { motion: 'burst', origin: 'center', shapes: 'mix', count: 200, angle: 90, spread: 70, speed: 1400, gravity: 1600, decay: 1.2, duration: 3, scale: 1,})import { start } from '@dioramajs/diorama'import confetti from '@dioramajs/particles/confetti'
const app = start()app.attach('.cf-pay', confetti({ motion: 'burst', origin: 'center', shapes: 'mix', count: 200, angle: 90, spread: 70, speed: 1400, gravity: 1600, decay: 1.2, duration: 3, scale: 1,}))Page transitions
A burst fades with the page as it leaves. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
triggerrebuildsdata-dio-trigger | 'click' | 'view' | 'event' | 'manual' | 'click' | click, view once on screen, a DOM event, or manual through fire. |
motionrebuildsdata-dio-motion | 'burst' | 'rain' | 'fireworks' | 'burst' |
|
eventrebuildsdata-dio-event | string | '' | Name of the DOM event on the element for the event trigger. |
firedata-dio-fire | number0 to 10 | 0 | Every increase fires a burst with the manual trigger. |
oncedata-dio-once | boolean | false | At most one burst per page. |
originrebuildsdata-dio-origin | 'center' | 'edges' | 'pointer' | 'center' | center of the box, its edges, or the pointer. |
countdata-dio-count | number1 to 1200 | 200 | Pieces per burst.At most low 80, medium 140 by quality tier. |
angledata-dio-angle | number0 to 360° | 90 | Direction of the burst; 90 is up. |
spreaddata-dio-spread | number0 to 360° | 70 | Spread around the direction. |
speeddata-dio-speed | number100 to 4000 px/s | 1400 | Initial speed of the pieces. |
gravitydata-dio-gravity | number0 to 5000 | 1600 | Gravity in px per second squared. |
decaydata-dio-decay | number0 to 5 | 1.2 | Air drag per second, as in canvas-confetti. |
durationdata-dio-duration | number0.5 to 10 s | 3 | Life of a piece. |
scaledata-dio-scale | number0.2 to 4 | 1 | Size of the pieces over a base of 10 px. |
colorsrebuildsdata-dio-colors | string | '#ff5a36 #ffd23f #3ec1d3 #7b5cff #ffffff' | Colors separated by spaces, any CSS colors. |
shapesrebuildsdata-dio-shapes | 'mix' | 'rect' | 'circle' | 'ribbon' | 'mix' | mix, rect, circle or ribbon. |
seedrebuildsdata-dio-seed | number1 to 100 | 1 | Seed of the randomness. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads presence
. Draws only while something on it moves, then stops. Only while pieces fly; none at rest. How a frame is decided.
Reduced motion and touch
With reduced motion it is off and the element shows as plain HTML. Off: neither the module nor the engine loads for it, and the page is unchanged.
Runs on every device. A tap fires like a click.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { confetti: { reducedMotion: 'static' } } })Cost and quality
- Download
- 4.2 KB min+gzip the first time a page uses it
- GPU
- One instanced draw for a pool of 1200 pieces.
- Memory
- Instance attributes of the pool, under 100 KB.
- Frames
- Idle at rest
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
low | At most 80 pieces. |
medium | At most 140 pieces. |
Recipes
Celebrate a sign-up when the server says yes
A click is too early for a form: the request can still fail. Use the event trigger and dispatch the event once your server answers, so the burst marks a sign-up that happened.
Live preview needs WebGPU or WebGL 2. A free trial form that bursts confetti once the sign-up request succeeds.
This example failed to draw.
Press Start free trial. The burst waits for the stand-in server, 0.6 s.
<form class="cf-signup" data-dio="confetti" data-dio-trigger="event" data-dio-event="signup:done"> <label for="cf-email">Work email</label> <div class="cf-row"> <input id="cf-email" type="email" name="email" value="you@example.com" required /> <button type="submit">Start free trial</button> </div> <p class="cf-note" role="status" >14 days free, no card needed.</p></form>const form = document.querySelector('form.signup') as HTMLFormElement
form.addEventListener('submit', async (event) => { event.preventDefault() const response = await fetch('/api/signup', { method: 'POST', body: new FormData(form), }) if (response.ok) form.dispatchEvent(new Event('signup:done'))})Rain on a donation thank-you page
The view trigger fires the first time half the section is in view, and rain lets the pieces fall from above the whole viewport for a few seconds. With once it rains one time on the page, however often the visitor scrolls back.
<section class="thanks" data-dio="confetti" data-dio-trigger="view" data-dio-motion="rain" data-dio-count="300" data-dio-once="true"> <h1>Thank you. Your gift plants 40 trees.</h1> <p>A receipt for your tax records is on its way to your inbox.</p></section>Fireworks when a course is complete
The manual trigger leaves the moment to your code: every increase of fire is one burst. Fire it when the last lesson is marked done, and let fireworks burst shells over the upper half of the viewport.
Lesson 12 of 12
Final project: a week of dinners for under $40
Live preview needs WebGPU or WebGL 2. The last lesson of a course marked complete, with fireworks bursting over the page.
This example failed to draw.
Mark the lesson complete to fire the fireworks.
<div class="cf-course" data-dio="confetti" data-dio-trigger="manual" data-dio-motion="fireworks" data-dio-shapes="circle"> <p class="cf-step">Lesson 12 of 12</p> <p class="cf-name">Final project: a week of dinners for under $40</p> <div class="cf-bar"><span></span></div> <button type="button">Mark as complete</button> <p class="cf-said" role="status"></p></div>import { app } from './diorama'
const course = document.querySelector('.cf-course')const confetti = course && app.effects.get(course, 'confetti')if (confetti) confetti.params.fire++Notes
Under reduced motion neither the module nor the engine loads for it, and the page stays as it is. Treat the burst as decoration: say what happened in the page itself, as the examples do in a status line.
The effect never cancels the click. A form still submits and a link still opens; when the page changes right away, the pieces fade out with it during a page transition.
The pieces are drawn in one instanced draw from a pool of 1200, and frames run only while pieces fly. The quality tier caps count at 80 on the low tier and 140 on the medium one, so markup that asks for 400 pieces gets 140 on a device at the medium tier.
trigger, motion, origin, shapes, colors and seed rebuild the effect when they change. Counts, speeds and angles change live and apply to the next burst.