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

Step 7 of 7

Reduced motion and touch

See what your effects and flights do with reduced motion and on touch devices, choose it per effect and check it in DevTools.

View as Markdown
On this page

In this step you check the page from the previous steps with reduced motion and on a touch device, and decide per effect what those visitors get. You change two effects with one option each: shimmer turns off with reduced motion, and torch turns off on touch devices.

Turn on reduced motion

Open DevTools, then the Rendering panel (in the Command Menu, CtrlShiftP, type Rendering). Set “Emulate CSS media feature prefers-reduced-motion” to reduce. Diorama follows the setting at once, without a reload.

Dawn over the ridge Dusk over the paper hills Night with a crescent moon

Live preview needs WebGPU or WebGL 2. Three prints with ripple, torch and shimmer, and a checkbox that emulates reduced motion: with it on, all three show the plain image.

This example failed to draw.

The checkbox emulates the setting for this whole page, the same way the Rendering panel does.
<div class="trio">
<img
src="/media/prints/dawn.jpg"
width="300"
height="300"
data-dio="ripple"
/>
<img
src="/media/prints/dusk.jpg"
width="300"
height="300"
data-dio="torch"
/>
<img
src="/media/prints/night.jpg"
width="300"
height="300"
data-dio="shimmer"
/>
</div>
import { effect } from '@dioramajs/diorama'
import { fract, smoothstep, vec4 } from 'three/tsl'
export default effect(
{ speed: 0.25, width: 0.15 },
{ reducedMotion: 'off' },
({ surface, u }) => {
surface.color((color, { box }) => {
const at = fract(u.time.mul(u.speed)).mul(2).sub(0.5)
const d = box.x.add(box.y).mul(0.5).sub(at).abs()
const band = smoothstep(0, u.width, d).oneMinus().mul(0.35)
return vec4(color.rgb.add(band), color.a)
})
},
)
import { effect } from '@dioramajs/diorama'
import { smoothstep, vec4 } from 'three/tsl'
export default effect(
{ radius: 0.35, dim: 0.6 },
{ when: 'hover' },
({ surface, u }) => {
surface.color((color, { box }) => {
const d = box.sub(u.pointer).mul(u.size).length().div(u.size.y)
const spot = smoothstep(u.radius.mul(0.6), u.radius, d).oneMinus()
const shade = spot.oneMinus().mul(u.dim, u.hover).oneMinus()
return vec4(color.rgb.mul(shade), color.a)
})
},
)

Move the pointer over the images and scroll:

  • ripple and torch show the plain image. They read u.hover, which stays 0 with reduced motion.
  • shimmer stands still. u.time and u.hoverTime stop, and the effect asks for no frames.
  • bend does not curve. u.velocity, the scroll speed, stays 0.
  • The thumbnail from the previous step does not fly. It fades out in place in 0.2 seconds, and the cover appears in its own place.

This is the default, reducedMotion: 'static': the effect still runs, with motion inputs at 0 and time stopped. u.pointer keeps working, because the visitor moves it.

Turn an effect off with reduced motion

A still frame is not always a good picture. If the setting changes while the page is open, u.time stops where it is, and the shimmer band can stop in the middle of the image. Pass reducedMotion: 'off' and the image stays plain HTML with reduced motion.

src/effects/shimmer.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 fract: UnaryFloatVecFunction
fract
,
const smoothstep: Smoothstep
smoothstep
,
const vec4: Vec4Function
vec4
} from 'three/tsl'
export default
effect<P>(
params: P & CheckSpec<P>,
options: EffectOptions<ColorsIn<P>>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly speed: 0.25;
readonly width: 0.15;
}

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: 0.25
speed
: 0.25,
width: 0.15
width
: 0.15 },
{
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: 0.25; readonly width: 0.15; }>
>

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
, {
box: Node<"vec2">

Box UV before transforms.

box
}) => {
const
const at: Node<"float">
at
=
function fract(x: FloatOrNumber): Node<"float"> (+3 overloads)
fract
(
u: Uniforms<
ColorsIn<{ readonly speed: 0.25; readonly width: 0.15; }>
>

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: 0.25; readonly width: 0.15; }>
>

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

u
.
speed: UniformNode<"float">
speed
)).
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(2).
NumExtensions<"float">.sub: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
sub
(0.5)
const
const d: Node<"float">
d
=
box: Node<"vec2">

Box UV before transforms.

box
.
Swizzle1From1<"float">.x: Node<"float">
x
.
NumExtensions<"float">.add: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
add
(
box: Node<"vec2">

Box UV before transforms.

box
.
Swizzle1From2<"float">.y: Node<"float">
y
).
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(0.5).
NumExtensions<"float">.sub: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
sub
(
const at: Node<"float">
at
).
FloatOrVecExtensions<"float">.abs: () => Node<"float">
abs
()
const
const band: Node<"float">
band
=
function smoothstep(
low: FloatOrNumber,
high: FloatOrNumber,
x: FloatOrNumber
): Node<"float"> (+3 overloads)
smoothstep
(0,
u: Uniforms<
ColorsIn<{ readonly speed: 0.25; readonly width: 0.15; }>
>

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

u
.
width: UniformNode<"float">
width
,
const d: Node<"float">
d
).
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
().
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(0.35)
return
function vec4(
xyz:
| Node<"vec3">
| Node<"color">
| Vector3
| Color
| Node<"vec4">,
w: Scalar
): JoinNode<"vec4"> (+10 overloads)
vec4
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
.
add: (
b: Vec3OrLessOrNumberParameterForNumType<"float">,
...params: Vec3OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec3"> (+2 overloads)
add
(
const band: Node<"float">
band
),
color: Node<"vec4">
color
.
Swizzle1From4<"float">.a: Node<"float">
a
)
})
},
)

Open the Elements panel and select the image. With reduced motion it has data-dio-state="off" and shows as plain HTML; turn the emulation off and the state goes to drawn as the effect starts again.

To keep an effect but drop only its motion, multiply the moving part by u.motion, which is 1 normally and 0 with reduced motion.

Turn an effect off on touch

A touch screen has no pointer that hovers, so torch has nothing to follow. Pass when: 'hover': the effect runs only where the page matches the media query (hover: hover).

src/effects/torch.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 smoothstep: Smoothstep
smoothstep
,
const vec4: Vec4Function
vec4
} from 'three/tsl'
export default
effect<P>(
params: P & CheckSpec<P>,
options: EffectOptions<ColorsIn<P>>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>> (+2 overloads)
type P = {
readonly radius: 0.35;
readonly dim: 0.6;
}

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
(
{
radius: 0.35
radius
: 0.35,
dim: 0.6
dim
: 0.6 },
{
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 radius: 0.35; readonly dim: 0.6; }>
>

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
, {
box: Node<"vec2">

Box UV before transforms.

box
}) => {
const
const d: Node<"float">
d
=
box: Node<"vec2">

Box UV before transforms.

box
.
NumVec2Extensions<"float">.sub: AddSubMulDivNumberVecVec2Extensions
(
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+2 overloads)
sub
(
u: Uniforms<
ColorsIn<{ readonly radius: 0.35; readonly dim: 0.6; }>
>

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

u
.
InputUniforms.pointer: UniformNode<"vec2">

Pointer in element UV, y up like uv(). 0..1 inside the box, not clamped outside.

pointer
).
mul: (
b: Vec2OrLessOrNumberParameterForNumType<"float">,
...params: Vec2OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec2"> (+6 overloads)
mul
(
u: Uniforms<
ColorsIn<{ readonly radius: 0.35; readonly dim: 0.6; }>
>

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

u
.
InputUniforms.size: UniformNode<"vec2">

Box size in CSS px.

size
).
FloatOrVecExtensions<"vec2">.length: () => Node<"float">
length
().
NumExtensions<"float">.div: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
div
(
u: Uniforms<
ColorsIn<{ readonly radius: 0.35; readonly dim: 0.6; }>
>

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

u
.
InputUniforms.size: UniformNode<"vec2">

Box size in CSS px.

size
.
Swizzle1From2<"float">.y: Node<"float">
y
)
const
const spot: Node<"float">
spot
=
function smoothstep(
low: FloatOrNumber,
high: FloatOrNumber,
x: FloatOrNumber
): Node<"float"> (+3 overloads)
smoothstep
(
u: Uniforms<
ColorsIn<{ readonly radius: 0.35; readonly dim: 0.6; }>
>

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

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

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

u
.
radius: UniformNode<"float">
radius
,
const d: Node<"float">
d
).
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
()
const
const shade: Node<"float">
shade
=
const spot: Node<"float">
spot
.
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
().
mul: (
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+7 overloads)
mul
(
u: Uniforms<
ColorsIn<{ readonly radius: 0.35; readonly dim: 0.6; }>
>

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

u
.
dim: UniformNode<"float">
dim
,
u: Uniforms<
ColorsIn<{ readonly radius: 0.35; readonly dim: 0.6; }>
>

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
).
FloatOrVecExtensions<"float">.oneMinus: () => Node<"float">
oneMinus
()
return
function vec4(
xyz:
| Node<"vec3">
| Node<"color">
| Vector3
| Color
| Node<"vec4">,
w: Scalar
): JoinNode<"vec4"> (+10 overloads)
vec4
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
.
mul: (
b: Vec3OrLessOrNumberParameterForNumType<"float">,
...params: Vec3OrLessOrNumberParameterForNumType<"float">[]
) => Node<"vec3"> (+5 overloads)
mul
(
const shade: Node<"float">
shade
),
color: Node<"vec4">
color
.
Swizzle1From4<"float">.a: Node<"float">
a
)
})
},
)

ripple comes with when: 'hover' already. when takes any media query, for example '(min-width: 800px)'.

To check it, turn on the device toolbar (CtrlShiftM in DevTools), pick a phone and reload the page. The image with torch has data-dio-state="off" and shows as plain HTML. If no other element on the page needs the engine, it does not start on the phone at all.

Why it works

Every effect decides what it does with reduced motion and on touch in its definition, so a page cannot forget one of its images. An effect that is off leaves its element as plain HTML, which costs nothing: no layer, no frames, and the engine does not start for it.

The same plain HTML is what visitors see when Diorama cannot run: on a browser without WebGPU or WebGL 2, and with Save-Data on, where start() does not load the engine. Give every image its alt text and its size, and the page works for them without a canvas.