Diorama

Made for a big screen.

This site is one 3D scene, built for a computer: a wide window and a mouse. Open dioramajs.dev on a desktop or a laptop to see it.This site is one 3D scene, built for a wide window. Make this one wider, or open dioramajs.dev on a larger screen.

GitHub

DioramaDocs

confetti

Celebration and particles

@dioramajs/particles/confettiSource

A burst of confetti from an element, a shower or fireworks to celebrate a sign-up, an order or a donation.

View as Markdown
On this page

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.

count pieces fly out of the element’s box at angle with spread and speed, slow down in the air, fall with gravity and tumble in 3D, face and back in different shades; ribbons bend, and every piece fades over the last fifth of its life. The rain motion lets the pieces fall from above the viewport over its whole width instead, and fireworks bursts five shells one after another at random points of its upper half. The pieces draw over everything and the element stays as it is.

Or click Place order. Switches and sliders apply to the next burst.

Params of confetti
motion

burst fires the pieces out of the element, for a button or a form.

origin

shapes

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.

Terminal window
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.

ParamTypeDefaultDescription
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'
  • burst fires the pieces out of the element, for a button or a form.
  • rain lets them fall from above the whole viewport for a few seconds, for a thank-you or results page.
  • fireworks bursts shells in every direction over the upper viewport, for a launch or a big win. Rain and fireworks ignore origin, angle and spread.
eventrebuildsdata-dio-eventstring''Name of the DOM event on the element for the event trigger.
firedata-dio-firenumber0 to 100Every increase fires a burst with the manual trigger.
oncedata-dio-oncebooleanfalseAt most one burst per page.
originrebuildsdata-dio-origin'center' | 'edges' | 'pointer''center'center of the box, its edges, or the pointer.
countdata-dio-countnumber1 to 1200200Pieces per burst.At most low 80, medium 140 by quality tier.
angledata-dio-anglenumber0 to 360°90Direction of the burst; 90 is up.
spreaddata-dio-spreadnumber0 to 360°70Spread around the direction.
speeddata-dio-speednumber100 to 4000 px/s1400Initial speed of the pieces.
gravitydata-dio-gravitynumber0 to 50001600Gravity in px per second squared.
decaydata-dio-decaynumber0 to 51.2Air drag per second, as in canvas-confetti.
durationdata-dio-durationnumber0.5 to 10 s3Life of a piece.
scaledata-dio-scalenumber0.2 to 41Size of the pieces over a base of 10 px.
colorsrebuildsdata-dio-colorsstring'#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-seednumber1 to 1001Seed 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.

TierWhat changes
lowAt most 80 pieces.
mediumAt 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>
src/signup.ts
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.