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

box

A product box built from pictures of its faces: turn it, tilt it, open its lid.

View as Markdown
On this page
Hillside Tea, sencha green tea, 20 bags

Live preview needs WebGPU or WebGL 2. A green tea carton with rolling hills on its front, turned a little to show its printed side; under the pointer it turns further and leans toward the pointer.

This example failed to draw.

Move the pointer over the box, drag it to see the back and the top, or focus it and press the arrow keys. Pick a motion to open it.
Params of box
motion

Usage

Put it on img: the front of the box; its aspect is the front of the box, and without WebGL the plain front image shows. start() registers box, 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 box
<img
class="tea"
src="/media/boxes/tea-front.webp"
width="600"
height="800"
data-dio="box"
data-dio-right="/media/boxes/tea-side.webp"
data-dio-top="/media/boxes/tea-top.webp"
data-dio-back="/media/boxes/tea-back.webp"
data-dio-yaw="20"
tabindex="0"
style="touch-action: pan-y"
/>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.tea', 'box', {
motion: 'none',
depth: 'auto',
yaw: 20,
pitch: 0,
swing: 30,
open: 0.6,
tilt: 5.7,
spin: 0,
shadow: 0.5,
})
import { start } from '@dioramajs/diorama'
import box from '@dioramajs/model/box'
const app = start()
app.attach('.tea', box({
motion: 'none',
depth: 'auto',
yaw: 20,
pitch: 0,
swing: 30,
open: 0.6,
tilt: 5.7,
spin: 0,
shadow: 0.5,
}))

Page transitions

Leaves by dithering its pixels away while shrinking to 96%, and its shadow fades with it. 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'none' | 'flap' | 'lift' | 'drawer''none'
  • flap swings the top back on a hinge, as on a folding carton of tea or cosmetics.
  • lift raises a lid over the top fifth, as on a rigid gift box.
  • drawer slides a tray out of a sleeve to the right, as on a box of chocolates or a phone.
backrebuildsdata-dio-backstring''Back face image URL or color; empty prints the front on the back too.
leftrebuildsdata-dio-leftstring''Left side image URL or color; empty takes the right side picture, else the color of the left edge of the front.
rightrebuildsdata-dio-rightstring''Right side image URL or color; empty takes the left side picture, else the color of the right edge of the front.
toprebuildsdata-dio-topstring''Top face image URL or color, its top edge toward the back; empty takes the bottom picture, else the color of the top edge of the front.
bottomrebuildsdata-dio-bottomstring''Bottom face image URL or color; empty takes the top picture, else the color of the bottom edge of the front.
insiderebuildsdata-dio-insidestring''Color of the bare board inside an opening box; empty is an off-white board.
depthrebuildsdata-dio-depthnumber | 'auto'0 to 2'auto'Depth as a share of the front width, so an 18 × 24 × 6 cm box has 0.33, and 0 a flat card; auto takes it from the side picture, else the top picture, else 0.3.
yawdata-dio-yawnumber-180 to 180°0Rest turn; positive shows the right side.
pitchdata-dio-pitchnumber-80 to 80°0Rest tilt; positive shows the top.
swingdata-dio-swingnumber-90 to 90°30Turn under the pointer or keyboard focus: 0 keeps it facing the viewer.
opendata-dio-opennumber0 to 10.6Share of the full opening shown on hover; a click or Enter opens it all the way. 0 keeps it shut.
tiltdata-dio-tiltnumber0 to 25°5.7How far it leans toward the pointer: 0 turns the lean off.
spindata-dio-spinnumber-180 to 180 deg/s0Idle turn while nobody touches the box, pausing under the pointer and for 1.5 s after: 0 turns it off.
dragdata-dio-dragbooleantrueTurns and tips it under a drag, and with the arrow keys when it has a tabindex.
shadowdata-dio-shadownumber0 to 10.5Density of the shadow on the page.

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, opacity, presence, reducedMotion and width . Draws only while something on it moves, then stops. Only while it turns, leans, opens, coasts after a drag or eases back, and while spin turns it; none at rest, and pointer moves elsewhere draw nothing. 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. It rests as posed: no swing, lean or spin, and hover does not open it. A click or Enter still opens it, at once; dragging and arrow keys still turn it, without coasting.

Runs on every device. Nothing hovers. A tap opens and closes a box with a motion, unless the image sits in a link or a button. A finger turns the box only when the element has `touch-action: pan-y` or `none`, and tips it only with `none`; otherwise the gesture scrolls the page.

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

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

Cost and quality

Download
4.9 KB min+gzip the first time a page uses it
GPU
Six to twenty flat faces with a simple light, no environment map.
Memory
The front image, and the pictures of the other faces when given.
Frames
Idle at rest

It runs from the low tier up. Every tier draws it the same way. The quality plugin picks the tier for each device.

Recipes

A carton from its front and one side

Most packaging is printed from a flat dieline, so the pictures already exist: export each face as its own image. The front image is the front of the box and sets its width and height. A side picture sets the depth by its own proportions: a side 240 px wide next to an 800 px tall front makes a box 0.3 of its height deep. Faces you leave out borrow from their pair, or take the color of the matching edge of the front.

<img
src="/shop/tea-front.webp"
width="600"
height="800"
data-dio="box"
data-dio-right="/shop/tea-side.webp"
data-dio-top="/shop/tea-top.webp"
data-dio-back="/shop/tea-back.webp"
data-dio-yaw="20"
/>

A gift box that lifts its lid

motion="lift" raises a lid that covers the top fifth of the box. Hover opens it by open, a click or Enter all the way. inside colors the bare board inside, which shows once the lid is off.

The Winter Gift Set: candles, soap and tea

Live preview needs WebGPU or WebGL 2. A burgundy gift box with a gold ribbon; under the pointer its lid lifts and tilts back, showing a cream board inside.

This example failed to draw.

Params of box
<img
class="gift"
src="/media/boxes/gift-front.webp"
width="900"
height="600"
data-dio="box"
data-dio-motion="lift"
data-dio-top="/media/boxes/gift-top.webp"
data-dio-inside="#f3e3c3"
data-dio-depth="0.6"
tabindex="0"
style="touch-action: pan-y"
/>

A sleeve with a tray

motion="drawer" slides a tray out of its sleeve to the right, as on a box of chocolates or a phone. motion="flap" swings back the top flap of a folding carton instead.

Nocturne, twelve dark pralines

Live preview needs WebGPU or WebGL 2. A navy sleeve of pralines; under the pointer a brown tray slides out of its right end.

This example failed to draw.

Params of box
<img
class="pralines"
src="/media/boxes/choc-front.webp"
width="900"
height="450"
data-dio="box"
data-dio-motion="drawer"
data-dio-right="/media/boxes/choc-side.webp"
data-dio-inside="#5b3a26"
data-dio-yaw="15"
tabindex="0"
style="touch-action: pan-y"
/>

Real proportions from the size

Without a side picture depth is auto and comes out at 0.3 of the width; give it as a share of the front width instead: a box 18 × 24 × 6 cm has a front 18 cm wide, so depth is 6 / 18, about 0.33.

<img
src="/games/harbor-front.webp"
width="800"
height="800"
data-dio="box"
data-dio-depth="0.25"
data-dio-spin="18"
/>

Notes

At rest the front is drawn exactly where the image is, so nothing jumps when the engine takes over, and the plain image with its alt text stays for screen readers and without WebGL.

The box draws only while it moves: a hover, a drag, the lean, the lid or the idle spin. The faces are flat pictures with a simple light, so a box costs its pictures and nothing else to download.

A box inside a link keeps the link’s click; it still opens on hover and turns under a drag. On touch screens a tap opens and closes it, and a finger turns it only when the image has touch-action: pan-y or none.

motion and the face pictures rebuild the effect; every other param changes live.