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

Reduced motion

What effects do under prefers-reduced-motion, on touch devices and without WebGL, and how to choose for your own effects.

View as Markdown
On this page

Every visitor gets a working page: Diorama only ever adds to the HTML. This page covers the three cases where it adds less or nothing: visitors who asked for reduced motion, touch devices without hover, and browsers without WebGPU or WebGL 2.

Reduced motion

When the system setting prefers-reduced-motion: reduce is on, effects keep running but stop moving on their own. Motion inputs read zero and clocks stand still.

Input or optionUnder reduced motion
hover, velocity0
motion0, otherwise 1
time, hoverTimeStand still
animate: 'hover', animate: 'visible'Ask for no frames
pointer, progress, pinUnchanged: the visitor moves them
over on inputUnchanged: it is a position, not a motion

So a hover ripple shows a still picture, a scroll-speed bend lies flat, and an effect driven by time holds one frame. The visitor still sees the effect’s picture, colors and layout.

The setting can change while the page is open. Diorama reads it live: inputs are recomputed and a frame is drawn at once.

Paper ridges at dusk

Live preview needs WebGPU or WebGL 2. A photo with ripple and bend and a checkbox that emulates reduced motion: with it on, the photo stays still under the pointer and while the page scrolls.

This example failed to draw.

The checkbox emulates the setting for this whole page, the same way the Rendering panel of DevTools does.
<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="ripple bend"
/>

Turn an effect off

reducedMotion: 'off' removes the effect for these visitors. The instance is off, the element stays plain HTML with data-dio-state="off", and the device does not start the engine for it.

src/effects/sway.ts
import {
function effect(
setup: EffectSetup<Empty>
): Effect<Empty> (+2 overloads)

Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its registry key (the file name when a build plugin finds it).

export default effect({ strength: 1 }, ({ surface, u }) => { ... })

effect
} from '@dioramajs/diorama'
import {
const vec2: Vec2Function
vec2
} from 'three/tsl'
export default
effect<{ readonly speed: 1; }>(
params:
& { readonly speed: 1; }
& CheckSpec<{ readonly speed: 1; }>,
options: EffectOptions<ColorsIn<{ readonly speed: 1; }>>,
setup: EffectSetup<ColorsIn<{ readonly speed: 1; }>>
): Effect<WidenSpec<{ readonly speed: 1; }>> (+2 overloads)

Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its registry key (the file name when a build plugin finds it).

export default effect({ strength: 1 }, ({ surface, u }) => { ... })

effect
(
{
speed: 1
speed
: 1 },
{
reducedMotion?: "off" | "static" | undefined

'static' (default) runs with motion inputs zeroed and time frozen. 'off' skips the effect.

reducedMotion
: 'off' },
({
surface: Surface

The shared GL copy of the element, created on first access.

surface
,
u: Uniforms<ColorsIn<{
readonly speed: 1;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
}) => {
surface: Surface

The shared GL copy of the element, created on first access.

surface
.
Surface.uv(transform: (uv: Node<"vec2">) => Node): Off

Bends box UV (0..1, y up) before sampling.

uv
((
uv: Node<"vec2">
uv
) =>
uv: Node<"vec2">
uv
.
add: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+3 overloads)
add
(
function vec2(
x: Scalar,
y: Scalar
): JoinNode<"vec2"> (+4 overloads)
vec2
(
u: Uniforms<ColorsIn<{
readonly speed: 1;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
.
InputUniforms.time: UniformNode<"float">

Seconds that advance only while visible. Frozen under reduced motion.

time
.
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<ColorsIn<{
readonly speed: 1;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
.
speed: UniformNode<"float">
speed
).
FloatOrVecExtensions<"float">.sin: () => Node<"float">
sin
().
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(0.01), 0)),
)
},
)

The default, 'static', keeps the effect with motion inputs at zero. Choose 'off' when the still frame makes no sense, such as particles frozen mid-flight.

Keep inputs still yourself

pointer, progress and pin keep moving, because they follow what the visitor does. When an effect turns them into large motion, scale that part by u.motion, which is 1 normally and 0 under reduced motion.

src/effects/slide.ts
import {
function effect(
setup: EffectSetup<Empty>
): Effect<Empty> (+2 overloads)

Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its registry key (the file name when a build plugin finds it).

export default effect({ strength: 1 }, ({ surface, u }) => { ... })

effect
} from '@dioramajs/diorama'
import {
const vec2: Vec2Function
vec2
} from 'three/tsl'
export default
effect<P>(
params: P & CheckSpec<P>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly depth: 0.08;
}

Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its registry key (the file name when a build plugin finds it).

export default effect({ strength: 1 }, ({ surface, u }) => { ... })

effect
({
depth: 0.08
depth
: 0.08 }, ({
surface: Surface

The shared GL copy of the element, created on first access.

surface
,
u: Uniforms<ColorsIn<{
readonly depth: 0.08;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
}) => {
// Slides the picture as the element passes through the viewport.
const
const shift: Node<"float">
shift
=
u: Uniforms<ColorsIn<{
readonly depth: 0.08;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
.
InputUniforms.progress: UniformNode<"float">

0 as the top enters at the bottom of the viewport, 1 as the bottom leaves at the top.

progress
.
NumExtensions<"float">.sub: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
sub
(0.5).
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<ColorsIn<{
readonly depth: 0.08;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
.
depth: UniformNode<"float">
depth
,
u: Uniforms<ColorsIn<{
readonly depth: 0.08;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
.
InputUniforms.motion: UniformNode<"float">

1 normally, 0 under prefers-reduced-motion. Handy as a multiplier.

motion
)
surface: Surface

The shared GL copy of the element, created on first access.

surface
.
Surface.uv(transform: (uv: Node<"vec2">) => Node): Off

Bends box UV (0..1, y up) before sampling.

uv
((
uv: Node<"vec2">
uv
) =>
uv: Node<"vec2">
uv
.
add: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+3 overloads)
add
(
function vec2(
x: Scalar,
y: Scalar
): JoinNode<"vec2"> (+4 overloads)
vec2
(0,
const shift: Node<"float">
shift
)))
})

On the CPU, input.reducedMotion is true under reduced motion. Read it in update, which asks for frames by returning true and gets them under reduced motion too, or in a function passed to animate, which decides frames on its own.

Page transitions

Under reduced motion page transitions do not fly. A keyed copy fades out in place in 0.2 seconds, and its target shows its DOM until its own copy is ready. Inside a browser view transition, copies fade with the root crossfade over 0.25 seconds. Durations and timelines you set for leaving and entering copies do not play.

Test it

  • Chrome DevTools: open the Rendering panel and set “Emulate CSS media feature prefers-reduced-motion” to reduce.
  • Playwright: await page.emulateMedia({ reducedMotion: 'reduce' }).
  • Windows: Settings, Accessibility, Visual effects, Animation effects off. macOS: System Settings, Accessibility, Display, Reduce motion.

Touch devices

An effect that moves only under the pointer shows nothing new on a phone, while the engine costs about 225 KB to download. Set when: 'hover' on such effects. It is short for the media query '(hover: hover)'; on a device where it does not match, the instance is off, the element stays plain HTML with data-dio-state="off" and the engine does not start for it.

src/effects/brighten.ts
import {
function effect(
setup: EffectSetup<Empty>
): Effect<Empty> (+2 overloads)

Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its registry key (the file name when a build plugin finds it).

export default effect({ strength: 1 }, ({ surface, u }) => { ... })

effect
} from '@dioramajs/diorama'
export default
effect<P>(
params: P & CheckSpec<P>,
options: EffectOptions<ColorsIn<P>>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly strength: 1;
}

Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its registry key (the file name when a build plugin finds it).

export default effect({ strength: 1 }, ({ surface, u }) => { ... })

effect
(
{
strength: 1
strength
: 1 },
{
when?: When | undefined

Media query the page must match or the instance is off. 'hover' means '(hover: hover)'.

when
: 'hover' },
({
surface: Surface

The shared GL copy of the element, created on first access.

surface
,
u: Uniforms<ColorsIn<{
readonly strength: 1;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
}) => {
surface: Surface

The shared GL copy of the element, created on first access.

surface
.
Surface.color(
transform: (color: Node<"vec4">, input: SurfaceInput) => Node
): Off

Changes the colour. sample(uv) reads the element's media with crop and masks applied.

color
((
color: Node<"vec4">
color
) =>
color: Node<"vec4">
color
.
mul: (
b: Vec4OrLessOrNumberParameterForNumType<"float">,
...params: Vec4OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec4"> (+4 overloads)
mul
(
u: Uniforms<ColorsIn<{
readonly strength: 1;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
.
strength: UniformNode<"float">
strength
.
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<ColorsIn<{
readonly strength: 1;
}>>

Input uniforms and one uniform per live param, for TSL graphs.

u
.
InputUniforms.hover: UniformNode<"float">

Damped 0..1 while the pointer is over the box. 0 under reduced motion.

hover
).
NumExtensions<"float">.add: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
add
(1)))
},
)

The built-in ripple declares when: 'hover' in its registry entry as well, so a touch device downloads neither its module nor the engine for it. An effect from your folder has its when in the file, which the device reads first: the module downloads, the engine does not start. when takes any media query, such as '(min-width: 768px)', and follows it live: when the query starts or stops matching, instances turn on or off.

when is never guessed from the code, since an effect can react to a tap on purpose: pointer follows touch too. To enable one of your effects on touch without editing its file, register it in start() with when: 'all'. An entry’s when beats the one in the definition, and an entry with when: 'hover' saves the module download on touch devices.

src/main.ts
import {
function start(options?: StartOptions): App

Starts Diorama on the page and returns the app at once. It finishes starting in the background. A second call returns the same app, so a re-run module doesn't make another.

import { start } from '@dioramajs/diorama'
export const app = start()

start
} from '@dioramajs/diorama'
export const
const app: App
app
=
function start(options?: StartOptions): App

Starts Diorama on the page and returns the app at once. It finishes starting in the background. A second call returns the same app, so a re-run module doesn't make another.

import { start } from '@dioramajs/diorama'
export const app = start()

start
({
StartOptions.effects?:
| { readonly [name: string]: EffectSource | EntryPatch; }
| undefined

Effects besides the folder's and the catalogue's: a new entry replaces the one of its name, a patch ({ ripple: { when: 'all' } }) changes when or reducedMotion of a known one. Replacing a built-in warns EFFECT_OVERRIDE in development unless the entry has override: true.

effects
: {
brighten: {
load: () => Promise<typeof import("./effects/brighten")>;
when: "all";
}
brighten
: {
EffectEntry.load: EffectLoader

Imports the effect module.

load
: () => import('./effects/brighten'),
when: "all"
when
: 'all' },
},
})

To keep the engine off every device without hover, pass the query to enabled.

src/main.ts
import {
function start(options?: StartOptions): App

Starts Diorama on the page and returns the app at once. It finishes starting in the background. A second call returns the same app, so a re-run module doesn't make another.

import { start } from '@dioramajs/diorama'
export const app = start()

start
} from '@dioramajs/diorama'
export const
const app: App
app
=
function start(options?: StartOptions): App

Starts Diorama on the page and returns the app at once. It finishes starting in the background. A second call returns the same app, so a re-run module doesn't make another.

import { start } from '@dioramajs/diorama'
export const app = start()

start
({
StartOptions.enabled?: boolean | "auto" | undefined

'auto' (default) is off under Save-Data and unavailable without WebGPU or WebGL 2. true ignores Save-Data but not a missing GPU: the page stays plain, as with 'auto'. false is off.

enabled
:
function matchMedia(query: string): MediaQueryList

The Window interface's matchMedia() method returns a new MediaQueryList object that can then be used to determine if the document matches the media query string, as well as to monitor the document to detect when it matches (or stops matching) that media query.

MDN Reference

matchMedia
('(hover: hover)').
MediaQueryList.matches: boolean

The matches read-only property of the MediaQueryList interface is a boolean value that returns true if the document currently matches the media query list, or false if not.

MDN Reference

matches
,
})

Effects driven by scroll speed, such as bend, trail native touch scrolling by one frame, because the browser scrolls on its compositor. Lenis with syncTouch: true scrolls touch on the main thread in step with the canvas; see GSAP and Lenis. To test touch, turn on device emulation in DevTools: it reports (hover: none).

Without WebGL

Before it downloads three.js or any effect module, Diorama checks the device once per app: Save-Data first, then the GPU, a WebGPU adapter or a WebGL 2 context. The check runs when the first element with an effect nears the viewport, or earlier for the idle-time prefetch after the window load event. The table also lists the cases from the sections above, since they leave the element the same way.

Caseapp.effects.statusElements
Neither WebGPU nor WebGL 2'unavailable'Plain HTML, data-dio-state="off"
Save-Data is on, with enabled: 'auto''off'Plain HTML, data-dio-state="off"
enabled: false'off'Plain HTML, data-dio-state="off"
when does not match, for example on touchUnchangedPlain HTML, data-dio-state="off"
Reduced motion and reducedMotion: 'off'UnchangedPlain HTML, data-dio-state="off"
The GPU device is lost or the renderer stops'unavailable'Plain HTML in the same frame, off

In the first three cases nothing of three.js or the effects is downloaded, not even by the idle-time prefetch, and the page works as written. Page transitions still swap the page, without flights. A browser without a GPU prints the RENDER_UNAVAILABLE warning once, in development builds only; production stays silent, since such visitors are expected. A lost device is a real failure: RENDER_LOST is reported once as an error.

enabled sets the check: 'auto' applies both rules, true ignores Save-Data but still checks the GPU, so a browser without one stays plain as with 'auto', and false turns the effects off.

Style the fallback with the attribute. A hint that only makes sense with the effect, for example, can hide without it.

figure:has([data-dio-state="off"]) .hover-hint {
display: none;
}

To react in code, wait until the app runs and watch the status.

src/main.ts
import {
function ready(app?: App): Promise<App>

Resolves with the app once it runs: the page's app of start() by default, waiting for the call if it has not happened yet. Rejects if the app fails or is destroyed first.

const app = await ready()
app.effects.get(el, 'ripple')

ready
,
function start(options?: StartOptions): App

Starts Diorama on the page and returns the app at once. It finishes starting in the background. A second call returns the same app, so a re-run module doesn't make another.

import { start } from '@dioramajs/diorama'
export const app = start()

start
} from '@dioramajs/diorama'
export const
const app: App
app
=
function start(options?: StartOptions): App

Starts Diorama on the page and returns the app at once. It finishes starting in the background. A second call returns the same app, so a re-run module doesn't make another.

import { start } from '@dioramajs/diorama'
export const app = start()

start
()
void
function ready(app?: App): Promise<App>

Resolves with the app once it runs: the page's app of start() by default, waiting for the call if it has not happened yet. Rejects if the app fails or is destroyed first.

const app = await ready()
app.effects.get(el, 'ripple')

ready
(
const app: App
app
).
Promise<App>.then<void, never>(
onfulfilled?:
| ((value: App) => void | PromiseLike<void>)
| null
| undefined,
onrejected?:
| ((reason: any) => PromiseLike<never>)
| null
| undefined
): Promise<void>

Attaches callbacks for the resolution and/or rejection of the Promise.

@param ― onfulfilled The callback to execute when the Promise is resolved.

@param ― onrejected The callback to execute when the Promise is rejected.

@returns ― A Promise for the completion of which ever callback is executed.

then
((
app: App
app
) => {
app: App
app
.
Services.effects: EffectsApi
effects
.
EffectsApi.status: ReadonlySignal<EffectsStatus>

The state of the engine.

status
.
ReadonlySignal<EffectsStatus>.subscribe(
listener: (value: EffectsStatus) => void
): Off

Calls the listener now and after every change (Svelte store contract).

subscribe
((
status: EffectsStatus
status
) => {
const
const flat: boolean
flat
=
status: EffectsStatus
status
=== 'unavailable' ||
status: "idle" | "loading" | "ready" | "off"
status
=== 'off'
var document: Document

window.document returns a reference to the document contained in the window.

MDN Reference

document
.
Document.documentElement: HTMLElement

The documentElement read-only property of the Document interface returns the Element that is the root element of the document (for example, the element for HTML documents).

MDN Reference

documentElement
.
Element.classList: DOMTokenList

The read-only classList property of the Element interface contains a live DOMTokenList collection representing the class attribute of the element. This can then be used to manipulate the class list.

MDN Reference

classList
.
DOMTokenList.toggle(
token: string,
force?: boolean
): boolean

The toggle() method of the DOMTokenList interface removes an existing token from the list and returns false. If the token doesn't exist it's added and the function returns true.

MDN Reference

toggle
('no-3d',
const flat: boolean
flat
)
})
})

To try the WebGL 2 path on a machine with WebGPU, pass render: { forceWebGL: true } to start().