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

voxel

A heading built from small lit 3D cubes that tumble in, bounce into place and settle into the text.

View as Markdown
On this page

Blockworks · toys, kits and bricks

Build something big

Free build table every Saturday from 10 a.m. Bring a friend, leave with a spaceship.

Live preview needs WebGPU or WebGL 2. A toy store's headline in yellow cubes on a blue background that fall in from above, bounce into place and settle into the text.

This example failed to draw.

Switch the style and the trigger, or move the sliders; with the hover trigger and with pixels, move the pointer across the heading.

Every switch and slider builds it again.

Params of voxel
style

cubes are beveled blocks that settle into the text, for most headings.

trigger

view builds when the heading enters.

Usage

Put it on a large heading or a 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 voxel, 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 voxel
<section class="vx-shop">
<p class="vx-kicker">Blockworks · toys, kits and bricks</p>
<h2
class="vx-title"
data-dio="voxel"
>Build something big</h2>
<p class="vx-line">Free build table every Saturday from 10 a.m. Bring a friend, leave with a spaceship.</p>
</section>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.vx-title', 'voxel', {
style: 'cubes',
trigger: 'view',
depth: 1,
drop: 240,
duration: 0.9,
stagger: 0.1,
bounce: 1,
})
import { start } from '@dioramajs/diorama'
import voxel from '@dioramajs/text/voxel'
const app = start()
app.attach('.vx-title', voxel({
style: 'cubes',
trigger: 'view',
depth: 1,
drop: 240,
duration: 0.9,
stagger: 0.1,
bounce: 1,
}))

Page transitions

Leaving a page, the heading turns back into cubes that bounce up and fly off behind the page; entering, they build it again. 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'cubes' | 'bricks' | 'pixels''cubes'
  • cubes are beveled blocks that settle into the text, for most headings.
  • bricks are toy bricks two cells wide with mortar lines and studs.
  • pixels are flat-faced voxels with crisp edges that stay as voxels, for a retro or game look.
triggerrebuildsdata-dio-trigger'view' | 'scroll' | 'hover''view'
  • view builds when the heading enters.
  • scroll builds it toward the middle of the viewport and drops the cubes again as it leaves.
  • hover shows voxels at once for the pointer to poke.
cellrebuildsdata-dio-cellnumber3 to 32 px8Size of one cube on the grid over the letters; larger reads chunkier.
depthdata-dio-depthnumber0 to 41Depth of a cube as a share of its width; 2 makes long blocks, 0 flat tiles.
dropdata-dio-dropnumber0 to 800 px240How far above the text the cubes start; 0 lets them pop up in place.
durationdata-dio-durationnumber0.3 to 3 s0.9Time one cube takes to fall, land and settle.
staggerdata-dio-staggernumber0 to 0.5 s0.1Delay of the wave per em of text, left to right; 0 drops every column at once.
bouncedata-dio-bouncenumber0 to 21Height of the hop and the squash when a cube lands; 0 lands it dead.
radiusdata-dio-radiusnumber0 to 400 px120Reach of the pointer for voxels that stay; 0 turns the pop-out off.
delaydata-dio-delaynumber0 to 2 s0Wait after the trigger, for a cascade.
thresholddata-dio-thresholdnumber0 to 10.2Share of the element height in view, above the bottom tenth, before it plays.
studsdata-dio-studsbooleantrueRound studs on top of each brick in the bricks style.
seedrebuildsdata-dio-seednumber1 to 1001Picks the starts and turns of the cubes; the same seed builds 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, progress, reducedMotion, size and y . Draws only while something on it moves, then stops. Only while cubes fall and settle, while scrolling with the scroll trigger and while popped voxels spring back; 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. Static: cubes and bricks show the text as it is; pixels show still voxels without the build or the pop-out.

Runs on every device. The same as with a mouse; voxels that stay pop out under a finger while it presses on the heading.

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

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

Cost and quality

Download
12.1 KB min+gzip the first time a page uses it
GPU
One instanced draw of up to 9,000 lit boxes, one quad per letter for the crisp text, and for voxels that stay one quad per cube for the shadows.
Memory
A canvas of the text at twice its CSS size, and about 100 bytes per cube: under 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
lowAt most 2,250 cubes (the grid widens past that), and the text rastered at 1 device pixel per CSS pixel instead of 2.
mediumAt most about 5,000 cubes.

Recipes

Retro voxels for a game launch

style="pixels" draws flat-faced voxels with crisp edges and no bevel, and keeps them: once the heading is built it stays as voxels instead of turning back into text. A larger depth makes long blocks whose sides show in perspective, more so away from the middle of the screen. Voxels that stay pop out toward the viewer under the pointer, lean out of the bump, cast a soft shadow on the cubes around them and spring back when it moves on. Scroll down to this example to see it build, then move the pointer across it.

Night Owl Games · out Friday on every console

Press start

Live preview needs WebGPU or WebGL 2. A dark purple game teaser with the words Press start in green voxels that pop out under the pointer.

This example failed to draw.

Params of voxel
<section class="vx-arcade">
<p class="vx-arcade-kicker">Night Owl Games · out Friday on every console</p>
<h2
class="vx-arcade-title"
data-dio="voxel"
data-dio-style="pixels"
data-dio-cell="6"
data-dio-depth="2"
data-dio-radius="140"
>Press start</h2>
</section>

radius sets how far the pointer reaches, in CSS pixels; 0 turns the pop-out off. The text stays real for screen readers, search and selection, but it shows as voxels, so keep this style for a short display line.

Toy bricks for a kids’ brand

style="bricks" pairs neighboring cells into bricks two cells wide, laid in a running bond like a wall, with dark mortar lines between them and a round stud on top of each cell. Bricks read best with a larger cell, so the studs have room; studs="false" leaves plain bricks.

<h1
data-dio="voxel"
data-dio-style="bricks"
data-dio-cell="12"
data-dio-bounce="1.5"
>Toy box</h1>

A heading that builds as you scroll

With data-dio-trigger="scroll" the cubes fall into place as the heading rises toward the middle of the viewport, the text holds there as plain DOM text, and as it moves on toward the top it turns back into cubes that drop away down the page. Scrolling back plays it in reverse. A higher drop makes the cubes fall from further up.

<h2
data-dio="voxel"
data-dio-trigger="scroll"
data-dio-drop="400"
>Step by step</h2>

Blocks to poke in a navigation or a hero

data-dio-trigger="hover" skips the build and shows the heading as cubes from the start, for the pointer to poke. Under reduced motion it stays plain text, unless the style is pixels.

<h2
data-dio="voxel"
data-dio-trigger="hover"
data-dio-radius="160"
>Play with us</h2>

Notes

The cubes are real boxes in the 3D space of the page, seen through the same camera, one instanced draw for all of them. Each samples the color of the text under its cell of the grid, so mixed colors inside the heading carry over. They are lit by a fixed key light from the upper left: cubes get a beveled edge that catches the light, bricks a gloss on their studs, and the sides of a cube are darker where a neighbor sits beside it and toward the page, a cheap kind of ambient occlusion between neighbors.

Each cube starts above and behind the page, tumbles around its own axis on the way down, lands with a hop and a squash, and wobbles still. The wave runs from left to right with lower rows first, so the letters stack up. Once a column has landed the cubes flatten into the face of the text and the real text fades in over them; when the last column is in the element is plain, selectable DOM text. Text on screen when the page opens is hidden until the effect is ready and then builds, as with assemble.

cell sets the size of the grid in CSS pixels: 8 suits headings of 80 to 160 px, 5 or 6 smaller ones, and 12 or more gives chunky toy blocks. The grid widens when a heading would need more than 9,000 cubes, fewer on slower devices. Frames run only while cubes move; at rest the effect draws nothing, voxels that stay included. Under reduced motion cubes and bricks show the text as it is, and pixels show still voxels. 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, mode, cell and seed rebuild the effect when they change.