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

spin

3D and productAlso inPhoto

@dioramajs/media/spinSource

A product turns by drag through a series of photos taken around it.

View as Markdown
On this page
Norrvik RF-2 rangefinder camera, 360° view. Drag or use the arrow keys to turn it.

Live preview needs WebGPU or WebGL 2. A rangefinder camera in 36 photos taken around it, turned by a drag, the pointer or the arrow keys.

This example failed to draw.

Drag the camera, or focus it and press the arrow keys. Switch by to hover and move the pointer across it instead.
Params of spin
by

drag turns it while pressed.

Usage

Put it on img (the poster). start() registers spin, 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 spin
<img
class="camera"
src="/media/shop/spin/01.avif"
width="960"
height="640"
data-dio="spin"
data-dio-frames="/media/shop/spin/{01..36}.avif"
tabindex="0"
style="touch-action: pan-y"
/>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.camera', 'spin', {
by: 'drag',
sensitivity: 1,
spin: 0,
inertia: 0,
reverse: false,
blend: false,
hint: true,
})
import { start } from '@dioramajs/diorama'
import spin from '@dioramajs/media/spin'
const app = start()
app.attach('.camera', spin({
by: 'drag',
sensitivity: 1,
spin: 0,
inertia: 0,
reverse: false,
blend: false,
hint: true,
}))

Page transitions

Fades with the page transition; a flight shows the current frame. How a flight flies.

Params

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

ParamTypeDefaultDescription
framesrebuildsdata-dio-framesstring''Frame URLs as a {01..72} range or a comma list, in turning order.
atlasrebuildsdata-dio-atlasstring''Atlas page URLs as a range or a list, read cell by cell.
gridrebuildsdata-dio-grid[number, number]1 to 16[1, 1]Columns and rows of cells on an atlas page.
countrebuildsdata-dio-countnumber | 'auto'1 to 360'auto'Number of frames; auto takes all.
densityrebuildsdata-dio-densitynumber0.1 to 11Share of the frames to load; 0.5 takes every second.At most low 0.5 by quality tier.
resolutionrebuildsdata-dio-resolutionnumber256 to 4096 px1600Cap on the long side of a frame when decoding.At most low 960, medium 1280 by quality tier.
rowsrebuildsdata-dio-rowsnumber1 to 81Rows of viewing angles; frames run row after row.
framedata-dio-framenumber0 to 720Current frame in its row, fractional with blend; the effect writes it as the visitor turns it.
rowdata-dio-rownumber0 to 80Current row of viewing angles.
byrebuildsdata-dio-by'drag' | 'hover' | 'manual''drag'
  • drag turns it while pressed.
  • hover turns it as the mouse moves over it, without a press (touch still drags).
  • manual leaves the pointer to the page: keys, frame and spin turn it, for a product that turns by itself.
sensitivitydata-dio-sensitivitynumber0.1 to 41How far a drag across the full width of the box turns it, as a factor: 1 is one whole turn.
spindata-dio-spinnumber0 to 360 deg/s0Turning by itself until the first touch, in degrees a second; 0 for none.
inertiadata-dio-inertianumber0 to 2 s0Coast after a flick: it goes as far as the flick carries it in this time and eases out onto a whole frame over three times as long at most. 0, the default, stops on the frame where the pointer lets go.
loopdata-dio-loopbooleantrueTurns full circle; off stops at the end frames.
reversedata-dio-reversebooleanfalseReverses the order of the frames.
blenddata-dio-blendbooleanfalseCross-fades neighbor frames by the fraction of the turn. Off by default: 36 or more frames turn smoothly as whole photos, and a mix of two reads as ghosting. Turn it on for sparse sets, about 24 frames or fewer, where whole frames step visibly.
hintdata-dio-hintbooleantrueTurns a twelfth back and forth when it first shows.
cachedata-dio-cachenumber1 to 328Decoded frames kept around the current one.At most low 4, medium 6 by quality tier.
fpsdata-dio-fpsnumber1 to 6030Frame cap while turning by itself.

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, opacity, reducedMotion and size . Draws at a steady rate while it is on screen and stops when it leaves. None at rest; full rate while dragging or coasting, at most fps while turning by itself or hinting. 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. No spin, inertia or hint; dragging and keys still work.

Runs on every device. Dragging needs touch-action: pan-y; rows need touch-action: none.

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

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

Cost and quality

Download
5.9 KB min+gzip the first time a page uses it
GPU
One or two texture reads per pixel.
Memory
Compressed bytes of every frame and a window of decoded ones; an atlas keeps none decoded.
Frames
Draws while on screen

It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.

TierWhat changes
lowHalf the frames, four decoded at a time, at most 960 px.
mediumSix decoded frames at a time, at most 1280 px.

Recipes

A product shot that turns

Shoot or render the product in a circle, every 5° to 10°, and name the files in turning order. The first frame is the <img>: it shows before the frames load, without WebGL and with reduced motion. tabindex="0" lets the arrow keys turn it, and touch-action: pan-y lets a finger turn it sideways while a vertical swipe still scrolls the page.

<img
src="/spin/camera/01.avif"
data-dio="spin"
data-dio-frames="/spin/camera/{01..36}.avif"
tabindex="0"
style="touch-action: pan-y"
/>

Say how to turn it in the alt or next to the picture. Nothing on the poster shows that it turns until the hint plays.

Buttons for the views that sell it

The effect writes frame as the visitor turns the product and goes to any frame the page writes. Buttons for front, side and back step the handle there the short way round, and a drag carries on from wherever they leave it.

Norrvik RF-2 rangefinder camera, 360° view. Drag, use the arrow keys or pick a view below.
Views of the camera

Live preview needs WebGPU or WebGL 2. The rangefinder camera with buttons for the three-quarter, front, side and back views, which turn it there frame by frame.

This example failed to draw.

<div class="product">
<img
class="camera"
src="/media/shop/spin/01.avif"
width="960"
height="640"
data-dio="spin"
data-dio-frames="/media/shop/spin/{01..36}.avif"
tabindex="0"
style="touch-action: pan-y"
/>
<fieldset class="views">
<legend>Views of the camera</legend>
<button
type="button"
data-frame="0"
>Three-quarter</button>
<button type="button" data-frame="33">Front</button>
<button type="button" data-frame="6">Side</button>
<button type="button" data-frame="15">Back</button>
</fieldset>
</div>
import { app } from '../../../../docs/diorama.ts'
// Each button turns the camera to its frame the short way round, one frame at a time.
export default function mount(example: HTMLElement): () => void {
const camera = example.querySelector('.camera')
const views = example.querySelector('.views')
const count = 36
let raf = 0
const turnTo = (target: number) => {
const spin = camera && app.effects.get(camera, 'spin')
if (!spin) return
cancelAnimationFrame(raf)
const from = spin.params.frame
// The shorter way round the circle.
const delta =
((((target - from) % count) + count * 1.5) % count) - count / 2
const start = performance.now()
const duration = 120 + Math.abs(delta) * 30
const step = (now: number) => {
const t = Math.min(1, (now - start) / duration)
const eased = 1 - (1 - t) ** 3
spin.params.frame =
((Math.round(from + delta * eased) % count) + count) % count
if (t < 1) raf = requestAnimationFrame(step)
}
raf = requestAnimationFrame(step)
}
const onClick = (e: Event) => {
const button = (e.target as Element).closest<HTMLButtonElement>(
'button[data-frame]',
)
if (button) turnTo(Number(button.dataset.frame))
}
views?.addEventListener('click', onClick)
return () => {
cancelAnimationFrame(raf)
views?.removeEventListener('click', onClick)
}
}

Fewer requests with an atlas

Thirty-six files mean thirty-six requests. Put the frames into atlas pages instead, read cell by cell, left to right and top to bottom. Two pages of 6 × 6 hold 72 frames, and .ktx2 pages stay compressed on the GPU, so an atlas keeps no frames decoded on the CPU.

<img
src="/spin/camera/01.avif"
data-dio="spin"
data-dio-atlas="/spin/camera-{0..1}.ktx2"
data-dio-grid="6 6"
tabindex="0"
style="touch-action: pan-y"
/>

Sparse sets and products that turn by themselves

With 24 frames or fewer, each step jumps visibly. blend cross-fades two neighbors by the fraction of the turn, which smooths a sparse set but reads as ghosting on a dense one, so it is off by default. spin turns it, in degrees a second, until the first touch, for a hero shot or a card that should catch the eye.

<img
src="/spin/vase/01.avif"
data-dio="spin"
data-dio-frames="/spin/vase/{01..18}.avif"
data-dio-blend="true"
data-dio-spin="36"
tabindex="0"
style="touch-action: pan-y"
/>

Notes

Frames decode off the main thread, and only cache of them stay decoded around the current one. A 36-frame set of 960 × 640 AVIF files weighs about 630 KB here; the low tier loads half the frames at up to 960 px. Decide on density before you shoot more frames: 0.5 loads every second one.

The product stops on the frame where the pointer lets go. inertia adds a coast after a flick: the product turns on as far as the flick carries it in that many seconds and eases out onto a whole photo, cross-fading neighbor frames on the way so it slows down without stepping. Try 0.3 to 0.5; reduced motion turns the coast off.

by set to hover turns the product as the mouse moves over it, without a press. Touch screens have no hover, so a finger still drags it. manual leaves the pointer to the page entirely: keys, frame and spin turn it.

Rows of viewing angles work the same way: shoot one circle per height, set rows, and a vertical drag moves between them. Rows need touch-action: none, since a vertical swipe turns the product instead of scrolling.