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

scrub

VideoAlso inScroll scenes3D and product

@dioramajs/media/scrubSource

A video scrubbed by scroll: its time follows the sticky section it plays in, forward and back.

View as Markdown
On this page

Live preview needs WebGPU or WebGL 2. A short film of a gold pocket watch whose crystal and bow lift away while the frame stays pinned and you scroll.

This example failed to draw.

Scroll the page. The frame holds still while the film plays forward, and scrolling up plays it back.
Params of scrub
by

Usage

Put it on video, muted, with preload="auto" and a poster of its first frame. start() registers scrub, 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 scrub
<div class="teardown">
<video
class="watch"
src="/media/film/watch.webm"
poster="/media/film/watch.webp"
width="720"
height="720"
muted
playsinline
preload="auto"
aria-label="A gold pocket watch whose crystal and bow lift away from the case"
data-dio="scrub"
></video>
</div>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.watch', 'scrub', {
by: 'pin',
start: 0,
end: 1,
position: 0,
from: 0,
ease: 0.1,
blend: 0.12,
})
import { start } from '@dioramajs/diorama'
import scrub from '@dioramajs/media/scrub'
const app = start()
app.attach('.watch', scrub({
by: 'pin',
start: 0,
end: 1,
position: 0,
from: 0,
ease: 0.1,
blend: 0.12,
}))

Page transitions

Fades with the page transition. How a flight flies.

Params

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

ParamTypeDefaultDescription
byrebuildsdata-dio-by'pin' | 'progress' | 'manual''pin'What drives the time: pin of the sticky section, viewport progress, or position alone.
startdata-dio-startnumber0 to 10Progress at which the scrubbed span begins.
enddata-dio-endnumber0 to 11Progress at which the scrubbed span ends.
positiondata-dio-positionnumber0 to 10Place in the span shown when by is manual, for a tween or a slider.
fromdata-dio-fromnumber0 to 60 s0Time in the video the span starts at.
torebuildsdata-dio-tonumber | 'auto'0 to 60 s'auto'Time in the video the span ends at, in seconds; auto is the end of the video.
easedata-dio-easenumber0 to 1 s0.1Smoothing of the time behind scroll; 0 follows scroll at once (Lenis already smooths it).
blenddata-dio-blendnumber0 to 0.5 s0.12Longest crossfade from one decoded frame to the next; frames that come quicker fade quicker. 0 cuts.
resolutionrebuildsdata-dio-resolutionnumber320 to 3840 px1920Longest side in px a frame is copied at; larger videos shrink on the GPU.At most low 960, medium 1440 by quality tier.

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 and width . Draws only while something on it moves, then stops. Scroll draws its own frames; a decoded frame asks for one, and ease and blend keep drawing until the time and the crossfade settle. None at rest, and no seeks while the video is off screen. How a frame is decided.

Reduced motion and touch

With reduced motion it is off and the element shows as plain HTML. Off: the video shows its poster and is never seeked. People with reduced motion and without WebGL see the poster, so choose it on purpose.

Runs on every device. Works as on desktop: scroll drives it.

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

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

Cost and quality

Download
2.6 KB min+gzip the first time a page uses it
GPU
Two texture reads per pixel of the box, one frame copy and upload per decoded frame.
Memory
The video as the browser buffers it, and two copied frames on the GPU. A 4 second 1080p clip with a keyframe every 5 frames is about 2 to 4 MB as H.264 or VP9.
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
lowFrames copied at up to 960 px.
mediumFrames copied at up to 1440 px.

Recipes

Turn a product on scroll

The usual page is a tall section with a sticky stage the height of the screen, and the video in the stage. scrub follows the pin of that section, so 400vh gives the film three screens of scroll. An end below 1 reaches the last frame early and holds it while the reader takes in the heading.

<section
class="turn"
data-dio-section
style="height: 400vh"
>
<div
class="stage"
style="position: sticky; top: 0; height: 100vh"
>
<video
src="/film/turn.mp4"
poster="/film/turn.webp"
muted
playsinline
preload="auto"
aria-label="The headphones, turning to show the hinge"
data-dio="scrub"
data-dio-end="0.85"
></video>
</div>
</section>

Encode the file for scrubbing

A browser seeks by decoding from the keyframe before the wanted time. Files from a phone or an editor have a keyframe every few seconds, so every step back decodes dozens of frames and the picture stops while you scroll. Encode the film with a keyframe every few frames, without sound, at most 1080p and with the index at the start of the file:

Terminal window
ffmpeg -i turn.mov -an -c:v libx264 -g 5 -crf 20 -pix_fmt yuv420p -movflags +faststart turn.mp4
ffmpeg -i turn.mov -an -c:v libvpx-vp9 -g 5 -crf 34 -b:v 0 turn.webm

With -g 1 every frame is a keyframe: the smoothest scrub at about twice the size. Keep the film short, 3 to 6 seconds is plenty for a turn or a teardown. The film in the example is 4 seconds at 30 frames a second, 720 px, with a keyframe every 5 frames: 660 KB as WebM. A few frames stretched over a long scroll step however they blend, so give the scroll as many frames as it has screens times 30 or so.

The effect asks for the next time only when the last seek is done, so seeks never pile up, and each frame the browser decodes crossfades from the one before for as long as the frames take to come, at most blend. A slow seek then shows as a short dissolve instead of a stop, and quick seeks stay sharp.

Part of a longer film

from and to pick the seconds that scroll plays through, so one film can serve several sections.

<video
src="/film/assembly.mp4"
muted
playsinline
preload="auto"
data-dio="scrub"
data-dio-from="2"
data-dio-to="6"
></video>

Driven by your own code

With by set to manual, position from 0 to 1 picks the moment, for a slider or a GSAP tween.

<video
class="film"
src="/film/assembly.mp4"
muted
playsinline
preload="auto"
data-dio="scrub"
data-dio-by="manual"
></video>
<input
class="at"
type="range"
min="0"
max="1"
step="0.001"
value="0"
aria-label="Assembly step"
/>
const film = app.effects.get(document.querySelector('.film'), 'scrub')
document.querySelector('.at').addEventListener('input', (e) => {
film?.set({ position: Number(e.target.value) })
})

Notes

scrub or sequence: a video is one file that the browser streams and decodes on the GPU, so it is smaller and quicker to start than a hundred images. A sequence of images steps without any seek and suits short loops and atlases. Pick scrub when the film is longer than a few dozen frames.

The video never plays: the effect pauses it whenever something starts it. It needs muted and playsinline like any inline video, and preload="auto", which the effect sets if it is missing.

The poster is what people with reduced motion and without WebGL see, so make it the first frame or the one that tells the most.

A video from another origin needs crossorigin="anonymous" and a server that sends Access-Control-Allow-Origin, or the effect stops with EFFECT_CORS.

resolution rebuilds the effect when it changes. Set it in the markup.