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

physics

TextAlso in3D and productBlocks and cards

@dioramajs/text/physicsSource

Playful gravity type: the letters of a heading become thick lit 3D tiles that fall, bounce, tumble and pile up, can be thrown with the pointer, and stand back up.

View as Markdown
On this page

Toy Fair 2027 · Hall 4 · Stand B12

Play it again

Wooden blocks, marble runs and stacking games, made in Porto and tested by the toughest critics we know.

Live preview needs WebGPU or WebGL 2. A toy fair banner whose red heading drops into a pile of thick 3D letter tiles at the bottom of the banner, ready to be thrown about.

This example failed to draw.

Click the heading to drop it, drag a letter to throw it, then press Stand it up. Switch the style for scatter or stack.
Params of physics
style

drop collapses the heading into a pile.

Usage

Put it on a large heading or a short line of display text, in a container with room below it. Not img, video, canvas, svg or form fields. start() registers physics, 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 physics
<div class="ph-stage">
<p class="ph-kicker">Toy Fair 2027 · Hall 4 · Stand B12</p>
<h2
class="ph-headline"
data-dio="physics"
data-dio-trigger="click"
data-dio-container=".ph-stage"
tabindex="0"
>Play it again</h2>
<p class="ph-lede">Wooden blocks, marble runs and stacking games, made in Porto and tested by the toughest critics we know.</p>
<button
class="ph-reset"
type="button"
data-dio-reset=""
>Stand it up</button>
</div>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.ph-headline', 'physics', {
style: 'drop',
gravity: 1,
bounce: 0.35,
friction: 0.5,
depth: 0.25,
throw: 1,
})
import { start } from '@dioramajs/diorama'
import physics from '@dioramajs/text/physics'
const app = start()
app.attach('.ph-headline', physics({
style: 'drop',
gravity: 1,
bounce: 0.35,
friction: 0.5,
depth: 0.25,
throw: 1,
}))

Markup it reads

  • data-dio-physics: Set on the heading while letters are off their places: moving while any moves, piled once all are still.
  • data-dio-reset: A click on any element with it stands the letters back up; a selector as its value limits it to matching headings.

Events

It dispatches these on its element; they bubble.

  • dio:physics, detail: { state: 'moving' | 'piled' | 'upright' }. The letters changed state: moving while they fall or fly, piled once still, upright once back as text.

Page transitions

Fades with the page. How a flight flies.

Params

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

ParamTypeDefaultDescription
stylerebuildsdata-dio-style'drop' | 'scatter' | 'stack''drop'
  • drop collapses the heading into a pile.
  • scatter bursts the letters apart.
  • stack drops them in one by one.
triggerrebuildsdata-dio-trigger'view' | 'click' | 'scroll' | 'hover''view'
  • view falls once in view.
  • click toggles on a click.
  • scroll falls when scrolled past.
  • hover knocks letters.
gravitydata-dio-gravitynumber0 to 31Pull of gravity, 1 is about earth for the page; 0 lets the letters float.
bouncedata-dio-bouncenumber0 to 10.35How much of its speed a letter keeps when it hits the floor or another letter.
frictiondata-dio-frictionnumber0 to 1.50.5Grip between letters and the floor; 0 lets them slide like ice.
depthdata-dio-depthnumber0 to 1 em0.25Thickness of a tile in em of its font size; 0 draws flat letters.
throwdata-dio-thrownumber0 to 31Strength of a throw with the pointer; 0 turns grabbing and knocking off.
containerrebuildsdata-dio-containerstring''Selector of an ancestor whose box the letters fall in; empty keeps the heading’s own box.

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

Inputs and frames

It reads delta, height, presence, size, width, x and y . Draws only while something on it moves, then stops. While letters fall, fly, hop or are dragged; none once the pile has settled or the heading stands upright. How a frame is decided.

Reduced motion and touch

With reduced motion it is off and the element shows as plain HTML. Off: the heading stays upright as plain text, and the engine does not load for it.

Runs on every device. A finger drags and throws a loose letter without scrolling the page; in the hover trigger a tap knocks a letter off.

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

import { start } from '@dioramajs/diorama'
start({ effects: { physics: { reducedMotion: 'static' } } })

Cost and quality

Download
18.1 KB min+gzip the first time a page uses it
GPU
One instanced draw of a box per letter, marched to the glyph in up to 24 texture reads per pixel, and two instanced draws of shadows.
Memory
An atlas of the letters at twice their CSS size with room for the shadows, and a relief map of the same size.
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
lowThe letters are rastered at 1 device pixel per CSS pixel instead of 2 and the march through each tile takes 8 steps instead of 24.
mediumThe march through each tile takes 14 steps instead of 24.

Recipes

A hero heading that falls once in view

The default view trigger lets the heading fall a moment after it comes into view. Point data-dio-container at the hero, so the letters pile up at its bottom edge rather than inside the heading’s own line, and give the visitor a way back with a button that carries data-dio-reset.

<section class="hero">
<h1
data-dio="physics"
data-dio-container=".hero"
>Gravity wins</h1>
<button data-dio-reset="">Reset</button>
</section>

While letters are off their places the heading carries data-dio-physics="moving" or "piled", so CSS can show the button only then: .hero:has([data-dio-physics]) button { visibility: visible }.

Letters that stack in one by one

data-dio-style="stack" drops each letter in from above into its own place, one after another, with a bounce and a wobble. On the first screen the heading stays hidden until the letters land, so it never shows as flat text first.

<h2
data-dio="physics"
data-dio-style="stack"
>Built to stack</h2>

A word mark you can knock apart

With data-dio-trigger="hover" the letters the pointer touches fall off, and the rest stay in place. When the pointer leaves the container and the pile is still, the letters fly back. On a touch screen a tap knocks a letter off.

<header>
<p
class="mark"
data-dio="physics"
data-dio-trigger="hover"
data-dio-container="header"
>loose</p>
</header>

data-dio-trigger="scroll" lets the heading fall as it scrolls past the upper third of the viewport and stands it back up when it comes back, and from code element.dispatchEvent(new Event('dio:reset')) stands it up at any time. A dio:physics event reports each change with detail.state set to moving, piled or upright.

Notes

Each letter is a real 3D tile cut in the shape of its glyph from the heading’s own text, as the browser laid it out, so it keeps its font, color and weight. The tiles are boxes in a small rigid-body simulation written for this effect: gravity, the walls of the container, contacts between tiles with friction and restitution, all on a fixed step, and each tile goes to sleep once it lies still. In the drawing every tile is marched from its box to the glyph, so its walls are the letter’s own outline seen from any angle, lit by their normals, with a bevel round the face, and each casts a soft shadow on the page behind it and on the floor below.

The container is the heading’s own box unless data-dio-container names an ancestor; the room also reaches a little toward the viewer, so letters can lie flat and lean on each other in depth. A loose letter can be grabbed and thrown: throw scales the speed it leaves the pointer with, and 0 turns the pointer off. While a letter is held the page does not scroll under a finger.

Nothing is drawn once everything is still: the pile stays where it lies without frames, and once the letters are back in their places the heading is plain DOM text again. Under reduced motion the effect is off and the heading stays upright as text. Text with text-shadow, -webkit-text-stroke, background-clip: text or a vertical writing mode stays as it is, with a warning in development. style, trigger and container rebuild the effect when they change.