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

dust

A heading crumbles into fine sand that lifts off the page and drifts away in depth as it scrolls out, or on a click.

View as Markdown
On this page

Wadi Rum · Night walks

Where the desert ends

Four hours on foot from the last road, with a Bedouin guide, mint tea in the dunes and the sky to yourself. Nightly from October to April.

Live preview needs WebGPU or WebGL 2. A desert night walk headline that crumbles into lit sand, lifts off the page towards the viewer and drifts away.

This example failed to draw.

Press Blow away, or scroll until the heading leaves the top of the window. Scrolling back gathers the sand into the heading again.

Scrolling the heading out of the top of the window blows it away too; scrolling back gathers it.

Params of dust
motion

wind blows the grains along angle in swirling streams, for most headings.

Usage

Put it on a large heading, a short line of display text or a dismissible message. Not img, video, canvas, svg or form fields. Text with text-shadow, a text stroke, background-clip: text or vertical writing stays as it is. start() registers dust, 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 dust
<div class="du-hero">
<p class="du-kicker">Wadi Rum · Night walks</p>
<h2
class="du-headline"
data-dio="dust"
>Where the desert ends</h2>
<p class="du-lede">Four hours on foot from the last road, with a Bedouin guide, mint tea in the dunes and the sky to yourself. Nightly from October to April.</p>
</div>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.du-headline', 'dust', {
motion: 'wind',
dot: 1.6,
duration: 2.2,
angle: 15,
turbulence: 1,
depth: 1,
shadow: 0.5,
})
import { start } from '@dioramajs/diorama'
import dust from '@dioramajs/text/dust'
const app = start()
app.attach('.du-headline', dust({
motion: 'wind',
dot: 1.6,
duration: 2.2,
angle: 15,
turbulence: 1,
depth: 1,
shadow: 0.5,
}))

Markup it reads

  • data-dio-dust: Set by the click trigger on its element: playing while the sand flies, then done for good.

Events

It dispatches these on its element; they bubble.

  • dio:dust, detail: null. The click trigger has blown the text away; the element keeps its space, so remove or collapse it here.

Page transitions

Leaving a page, the heading blows away as sand; entering, the sand gathers back into the text. How a flight flies.

Params

Every param is also an attribute, and both app.attach() and the handle take it by name.

ParamTypeDefaultDescription
motionrebuildsdata-dio-motion'wind' | 'rise' | 'explode''wind'
  • wind blows the grains along angle in swirling streams, for most headings.
  • rise lets them float up like ash.
  • explode bursts the text into tiles that fly out of the page, front and back.
triggerrebuildsdata-dio-trigger'scroll' | 'click' | 'view''scroll'
  • scroll ties it to the heading leaving the top of the viewport, both ways.
  • click plays it on a click or Enter, then leaves the element empty.
  • view plays it once the heading scrolls out and gathers it back on return.
densityrebuildsdata-dio-densitynumber0.3 to 31Grains per area of the glyphs; 2 doubles them, up to the tier limit.At most low 0.6 by quality tier.
dotdata-dio-dotnumber0.6 to 6 px1.6Size of a grain in flight on the page plane; closer grains look larger.
durationdata-dio-durationnumber0.5 to 6 s2.2Time the click and view triggers take to blow the whole text away.
angledata-dio-anglenumber-180 to 180°15Where the wind blows, counterclockwise from the right; 90 blows straight up.
turbulencedata-dio-turbulencenumber0 to 31Strength of the swirls the grains ride; 0 lets them fly straight.
depthdata-dio-depthnumber0 to 21How far the grains travel towards the viewer and back; 0 keeps them flat.
shadowdata-dio-shadownumber0 to 10.5Darkness of the shadows lifted grains cast on the page; 0 turns them off.At most low 0 by quality tier.
seedrebuildsdata-dio-seednumber1 to 1001Picks the ragged edge, paths and sizes; the same seed blows the same way.

Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.

Inputs and frames

It reads height, presence, reducedMotion, size and y . Draws only while something on it moves, then stops. While scrolling through the sweep with the scroll trigger, and while the sand flies with the click and view triggers; none at rest. How a frame is decided.

Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. The scroll and view triggers show the text as it is; the click trigger fades the text out in 0.4 s instead.

Runs on every device. The same as with a mouse; a tap plays the click trigger.

To change what reduced motion does, patch it in start().

import { start } from '@dioramajs/diorama'
start({ effects: { dust: { reducedMotion: 'off' } } })

Cost and quality

Download
11.4 KB min+gzip the first time a page uses it
GPU
Two instanced draws of up to 20,000 small quads, each tracing its path through the flow in five steps, and one instanced draw of a quad per line for the text that is left.
Memory
A canvas of the text at twice its CSS size, a small map of when each grain comes loose, and 48 bytes per grain: about 1 MB at the high tier.
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
lowA quarter of the grains, density at most 0.6, no shadows, and the text rastered at 1 device pixel per CSS pixel instead of 2.
mediumAbout half of the grains.

Recipes

A notice the visitor dismisses

With data-dio-trigger="click" the text blows away once on a click, a tap or Enter, and the element is left empty, with data-dio-dust="done". It still takes its space, so remove or collapse it on the dio:dust event. Put the close button inside the element, so the click on it plays the effect too. The dust carries only the text: fade the box around it with CSS while data-dio-dust is set. Click the notice below.

Quarterly report · Draft 4

Draft saved. 3 changes synced to your team.

Live preview needs WebGPU or WebGL 2. A saved draft notice that rises away as ash when it is clicked, then leaves the page.

This example failed to draw.

<div class="du-app">
<p class="du-doc">Quarterly report · Draft 4</p>
<div class="du-slot">
<div class="du-toast">
<div
class="du-body"
data-dio="dust"
data-dio-trigger="click"
data-dio-motion="rise"
>
<p>
<span aria-hidden="true">✓</span>
Draft saved. 3 changes synced to your team.
</p>
<button
type="button"
aria-label="Dismiss"
>×</button>
</div>
</div>
</div>
<button
type="button"
class="du-again"
hidden
>Show the notice again</button>
</div>
// When the notice has blown away the page removes it; a button brings a fresh one back.
export default function mount(example: HTMLElement): () => void {
const slot = example.querySelector('.du-slot')
const again = example.querySelector<HTMLButtonElement>('.du-again')
const fresh = slot?.querySelector('.du-toast')?.cloneNode(true) as
| Element
| undefined
if (!slot || !again || !fresh) return () => {}
const gone = (e: Event) => {
;(e.target as Element).closest('.du-toast')?.remove()
again.hidden = false
again.focus()
}
const restore = () => {
const toast = fresh.cloneNode(true) as Element
for (const el of toast.querySelectorAll(
'[data-dio-state], [data-dio-dust]',
)) {
el.removeAttribute('data-dio-state')
el.removeAttribute('data-dio-dust')
}
slot.append(toast)
again.hidden = true
}
slot.addEventListener('dio:dust', gone)
again.addEventListener('click', restore)
return () => {
slot.removeEventListener('dio:dust', gone)
again.removeEventListener('click', restore)
}
}
.toast:has([data-dio-dust]) {
background-color: transparent;
box-shadow: none;
transition: background-color 0.6s 0.3s, box-shadow 0.6s 0.3s;
}

A heading that shatters once it scrolls out

With data-dio-trigger="view" the heading plays on its own clock once it has moved into the top of the window, whatever the scroll speed, and gathers back when it returns. explode breaks it into tiles that tumble out of the page, towards the viewer and behind it.

<h2
data-dio="dust"
data-dio-trigger="view"
data-dio-motion="explode"
data-dio-duration="1.6"
>Sold out</h2>

Ash rising off a closing chapter

rise lets the grains float straight up like ash, and an angle of 90 starts the edge at the top of the text, so the grains never cross the lines still standing. Without shadows the ash reads lighter on a dark page.

<h2
data-dio="dust"
data-dio-motion="rise"
data-dio-angle="90"
data-dio-shadow="0"
>The last summer</h2>

Notes

The grains are pieces of the heading’s own text, drawn the way the browser laid it out: each one carries its cell of the glyph pixels, so the edge where the text comes loose looks like the ink it leaves, in every color and weight the heading has. A grain that comes loose first tumbles as a flake, then rounds off into sand of the dot size, lifts off the page towards the viewer and rides a swirling flow in the direction of the wind. Grains in front of the page are seen through the page camera, larger and slightly out of focus, those behind it smaller and dimmer, and a soft light from the upper left shades and glints them as they turn. Lifted grains cast faint shadows on the page below.

The edge sweeps from the side the wind blows to, so the sand always flies over the empty page, never across the text that is left. With the scroll trigger the sweep starts when the heading’s top rises past 30% of the window height, or with the first scroll for a heading that starts above that line, and ends when its middle reaches the top; it runs both ways.

The number of grains follows the area of the glyphs: about 10,000 for a 120 px heading of four words, at most 20,000 on fast devices, fewer on slower ones. density raises or lowers it within that limit. Under reduced motion the scroll and view triggers keep the text as it is, and the click trigger fades it out instead. Text with text-shadow, -webkit-text-stroke, background-clip: text or a vertical writing mode stays as it is, with a warning in development. motion, trigger, density and seed rebuild the effect when they change.