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

shatter

TextAlso in3D and productScroll scenes

@dioramajs/text/shatterSource

A heading breaks into lit 3D shards that fly apart and tumble, then fly back together: on a click, on scroll, under the pointer or once in view.

View as Markdown
On this page

Harbor Glass Studio · Open furnace weekend

Fragile by design

Two days of open furnaces, glassblowing demos and a seconds sale of studio pieces. Saturday and Sunday, 10 to 6, free entry.

Live preview needs WebGPU or WebGL 2. A glass studio headline on a dark page that breaks into lit glass shards where it is clicked, which tumble away and fly back together.

This example failed to draw.

Click the heading where you want it to break, or press Break it. Switch the style for stone or paper.

Or click the heading where you want it to break. The pieces fly back after hold seconds.

Params of shatter
style

glass is thin clear panes.

Usage

Put it on a large heading or short line of display text. 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 shatter, 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 shatter
<div class="sh-hero">
<p class="sh-kicker">Harbor Glass Studio · Open furnace weekend</p>
<h2
class="sh-headline"
data-dio="shatter"
>Fragile by design</h2>
<p class="sh-lede">Two days of open furnaces, glassblowing demos and a seconds sale of studio pieces. Saturday and Sunday, 10 to 6, free entry.</p>
</div>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.sh-headline', 'shatter', {
style: 'glass',
strength: 1,
spin: 1,
gravity: 1,
depth: 1,
hold: 1.5,
})
import { start } from '@dioramajs/diorama'
import shatter from '@dioramajs/text/shatter'
const app = start()
app.attach('.sh-headline', shatter({
style: 'glass',
strength: 1,
spin: 1,
gravity: 1,
depth: 1,
hold: 1.5,
}))

Markup it reads

  • data-dio-shatter: Set by the click trigger on its element: broken while the pieces fly, gone once the text is gone for good.

Events

It dispatches these on its element; they bubble.

  • dio:shatter, detail: null. The click trigger with hold 0 has broken the text for good; the element keeps its space, so remove or collapse it here.

Page transitions

The pieces fade 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'glass' | 'stone' | 'paper''glass'
  • glass is thin clear panes.
  • stone is thick rough chunks with dust.
  • paper is torn scraps.
triggerrebuildsdata-dio-trigger'click' | 'scroll' | 'hover' | 'view''click'
  • click breaks where clicked.
  • scroll as it leaves.
  • hover lifts pieces.
  • view flies it in.
piecesrebuildsdata-dio-piecesnumber6 to 14040About how many pieces the text box is cut into; pieces with no ink are left out.At most low 24, medium 64 by quality tier.
strengthdata-dio-strengthnumber0 to 31Speed the pieces leave with, and how high they lift with the hover trigger.
spindata-dio-spinnumber0 to 31How fast the pieces tumble; 0 keeps them flat to the page as they fly.
gravitydata-dio-gravitynumber0 to 31Pull that bends the paths down; 0 lets the pieces drift off in a straight line.
depthdata-dio-depthnumber0 to 31Thickness of a piece, times the thickness of its style; 0 makes flat pieces.
holddata-dio-holdnumber0 to 10 s1.5Time the click trigger waits before the pieces fly back; 0 leaves the text gone.
seedrebuildsdata-dio-seednumber1 to 1001Picks the fracture lines, paths and spins; the same seed breaks 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 presence, reducedMotion and size . Draws only while something on it moves, then stops. While the pieces fly, fly back or settle, and while scrolling through the break with the scroll trigger; 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 click trigger fades the text out instead, and back in after hold; the other triggers keep the text as it is.

Runs on every device. A tap breaks it with the click trigger; with the hover trigger a finger on the text lifts the pieces under it.

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

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

Cost and quality

Download
16.0 KB min+gzip the first time a page uses it
GPU
One draw of every piece as a prism of a few dozen triangles, back to front, with a short march through stone; one draw of their shadows and one of up to 420 dust motes.
Memory
A canvas of the text at twice its CSS size and two small float textures of the pieces: a few MB for a large heading.
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 24 pieces, a shorter march through stone, no dust, and the text rastered at 1 device pixel per CSS pixel instead of 2.
mediumAt most 64 pieces and a shorter march through stone.

Recipes

Cracks under the pointer

With data-dio-trigger="hover" the heading stays whole, but the pieces near the pointer lift toward the viewer and tilt, and a ripple of cracks runs out across the line from where the pointer came in. When the pointer leaves, the pieces settle back and the heading is plain text again. strength sets how high they lift and spin how far they tilt. On a touch screen a finger on the heading lifts the pieces under it. Move the pointer over the heading below.

Hand-thrown stoneware · ships in recycled crates

Handle with care

Every bowl is packed by the potter who made it, so it reaches you in one piece.

Live preview needs WebGPU or WebGL 2. A stoneware shop banner on a cream page whose heading cracks into stone pieces that lift under the pointer.

This example failed to draw.

Params of shatter
<div class="sh-shop">
<p class="sh-shop-tag">Hand-thrown stoneware · ships in recycled crates</p>
<h2
class="sh-shop-title"
data-dio="shatter"
data-dio-trigger="hover"
data-dio-style="stone"
>Handle with care</h2>
<p class="sh-shop-note">Every bowl is packed by the potter who made it, so it reaches you in one piece.</p>
</div>

A notice that breaks for good

With data-dio-hold="0" the click trigger never brings the text back: the pieces fall away, the element is left empty with data-dio-shatter="gone", and a dio:shatter event fires. The element still takes its space, so remove or collapse it then. Give a clickable element role="button" and tabindex="0", so Enter breaks it too.

<p
class="notice"
role="button"
tabindex="0"
data-dio="shatter"
data-dio-style="stone"
data-dio-hold="0"
>Offer expired. Click to dismiss.</p>
document.querySelector('.notice').addEventListener('dio:shatter', (e) => e.target.remove())

A chapter heading that crumbles as it scrolls out

With data-dio-trigger="scroll" the heading is whole while it sits in the viewport, and breaks apart as it rises past the top, scrubbed by the scroll in both directions. A heading that scrolls in from the bottom flies together on its way up. Stone suits a heavy, solid typeface.

<h2
data-dio="shatter"
data-dio-style="stone"
data-dio-trigger="scroll"
data-dio-pieces="30"
>Built to last</h2>

data-dio-trigger="view" plays the opposite once: the pieces fly in from around the page and lock together as the heading comes into view, a good opener for a launch or a portfolio.

Notes

The pieces are cut from the heading’s own text, drawn the way the browser laid it out, so every piece carries its part of the letters in their font, color and weight. The cuts are seeded Voronoi cells that crowd onto the letters; cells with no ink are left out. A click cuts the text again around the point of impact, with small pieces in rings close to it and larger ones farther out, like a struck pane, and the crack runs out from there so the far pieces let go a moment later.

Each piece is a real slab seen through the page camera: glass is a thin clear pane with the text printed on its face, green along its edges where the light travels through more glass, with reflections and bright glints on the cut edges as it turns. Stone is a thick chunk whose walls follow the letters, rough and matte, and it breaks with a puff of dust. Paper is a sheet with torn, fibrous edges that bends and rocks as it flutters down and shows its blank back. Lifted pieces cast soft shadows on the page. depth scales the thickness; 0 gives flat pieces.

pieces sets about how many pieces the text box is cut into: 40 suits a heading of two or three words, more makes finer shards. The drawing order follows the distance to the camera every frame, so pieces that fly toward the viewer pass in front of the others. Nothing is drawn at rest: the heading is plain DOM text before the break and once the pieces are back, and with the hover trigger once they have settled.

Under reduced motion the click trigger fades the text out, and back in after hold; the other triggers keep the text as it is. 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, pieces and seed rebuild the effect when they change.