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 4 of 7

Frames and time

Animate an effect with the time input, count the frames it draws and make it run only while the pointer is over the image.

View as Markdown
On this page

In this step you write shimmer, a band of light that sweeps across an image, and count the frames the page draws. You see the difference between u.time, which runs while the image is on screen, and u.hoverTime, which runs only while the pointer is over it.

Count the frames

Diorama draws a frame only when something on the canvas changes. To watch that, add a counter to the page and show app.loop.stats.frames, the number of frames run since the start.

index.html
<p>Frames: <output class="frames">0</output></p>
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
()
const
const meter: Element | null
meter
=
var document: Document

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

MDN Reference

document
.
ParentNode.querySelector<Element>(selectors: string): Element
| null (+4 overloads)

Returns the first element that is a descendant of node that matches selectors.

MDN Reference

querySelector
('.frames')
function setInterval(
handler: TimerHandler,
timeout?: number,
...arguments: any[]
): number
setInterval
(() => {
if (
const meter: Element | null
meter
)
const meter: Element
meter
.
Element.textContent: string | null
textContent
=
var String: StringConstructor
(value?: any) => string

Allows manipulation and formatting of text strings and determination and location of substrings within strings.

String
(
const app: App
app
.
AppCore<Services & Empty, string>.loop: Loop

Live state of the frame loop.

loop
.
Loop.stats: FrameStats

Loop counters.

stats
.
FrameStats.frames: number

Frames run so far.

frames
)
}, 250)

Leave the pointer still and the counter stops. Scroll, or move the pointer, and it grows by a few frames for each move: every pointer move and scroll step can change what an effect sees, so each one runs a frame.

Animate with time

u.time is the number of seconds the element has been on screen. Use it to move a light band across the image once every four seconds.

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>,
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 },
({
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
}) => {
// Where the band is, from -0.5 to 1.5, so it enters and leaves fully.
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
)
})
},
)
index.html
<img
src="/cover.jpg"
width="800"
height="450"
data-dio="shimmer"
/>
Paper ridges at dusk

Live preview needs WebGPU or WebGL 2. A photo with a light band that sweeps across it all the time, next to a counter of the frames the page draws.

This example failed to draw.

The counter counts every frame of this page, so another example on screen adds to it.

Frames: 0

Last second: 0

Params of shimmer
style

chrome a mirror metal.

trigger

hover sweeps on pointer, focus or tap.

<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="shimmer"
/>
import { effect } from '@dioramajs/diorama'
import { fract, smoothstep, vec4 } from 'three/tsl'
export default effect(
{ speed: 0.25, width: 0.15 },
({ surface, u }) => {
surface.color((color, { box }) => {
// Where the band is, from -0.5 to 1.5, so it enters and leaves fully.
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 { app } from '../../docs/diorama.ts'
// The counter of src/main.ts, plus the frames of the last second.
export default (example: HTMLElement) => {
const meter = example.querySelector('.frames')
const rate = example.querySelector('.rate')
let last = app.loop.stats.frames
let ticks = 0
const timer = setInterval(() => {
const frames = app.loop.stats.frames
if (meter) meter.textContent = String(frames)
if (++ticks % 4 === 0) {
if (rate) rate.textContent = String(frames - last)
last = frames
}
}, 250)
return () => clearInterval(timer)
}

The band sweeps diagonally across the image, and the counter grows by about 60 a second, one frame per refresh of the display. Scroll the image off screen: the counter stops, and the band waits where it was. Switch to another tab: it stops too.

Run only on hover

An image that shimmers all the time costs a frame on every refresh while it is visible. Replace u.time with u.hoverTime, which runs only while the pointer is over the box, and multiply the band by u.hover so it fades out when the pointer leaves.

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>,
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 },
({
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.hoverTime: UniformNode<"float">

Seconds that run while hovered or while hover decays, frozen at rest and under reduced motion. Reading it implies animate: 'hover'.

hoverTime
.
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
(
u: Uniforms<
ColorsIn<{ readonly speed: 0.25; readonly width: 0.15; }>
>

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
, 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
)
})
},
)
Paper ridges at dusk

Live preview needs WebGPU or WebGL 2. The photo with a band that sweeps only while the pointer is over it, next to the frame counter that stands still at rest.

This example failed to draw.

The counter counts every frame of this page, so another example on screen adds to it.

Frames: 0

Last second: 0

Params of shimmer
style

chrome a mirror metal.

trigger

hover sweeps on pointer, focus or tap.

<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="shimmer"
/>
import { effect } from '@dioramajs/diorama'
import { fract, smoothstep, vec4 } from 'three/tsl'
export default effect(
{ speed: 0.25, width: 0.15 },
({ surface, u }) => {
surface.color((color, { box }) => {
const at = fract(u.hoverTime.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(u.hover, 0.35)
return vec4(color.rgb.add(band), color.a)
})
},
)
import { app } from '../../docs/diorama.ts'
// The counter of src/main.ts, plus the frames of the last second.
export default (example: HTMLElement) => {
const meter = example.querySelector('.frames')
const rate = example.querySelector('.rate')
let last = app.loop.stats.frames
let ticks = 0
const timer = setInterval(() => {
const frames = app.loop.stats.frames
if (meter) meter.textContent = String(frames)
if (++ticks % 4 === 0) {
if (rate) rate.textContent = String(frames - last)
last = frames
}
}, 250)
return () => clearInterval(timer)
}

Now the counter stands still while the pointer rests outside the image. Over the image it grows by about 60 a second even when the pointer stops, and it stops a moment after the pointer leaves, once the band has faded out.

Choose the frame policy yourself

Diorama picks the frame policy from the inputs the effect reads: u.time means 'visible', u.hoverTime means 'hover', and neither means 'input'. An update function that returns true or false decides instead, as in Your own effect in 10 minutes. To set it yourself, pass the animate option between the params and the setup function. A shimmer does not need 60 frames a second; 30 is enough.

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 },
{
animate?:
| AnimatePolicy<
ColorsIn<{ readonly speed: 0.25; readonly width: 0.15; }>
>
| undefined

Defaults to the update's answer when it returns a boolean, else to what the effect reads: time animates every visible frame, hoverTime while hovered or hover decays, neither means 'input'.

animate
: {
fps:
| number
| (
(
ctx: EffectContext<
ColorsIn<
{ readonly speed: 0.25; readonly width: 0.15; }
>
>
) => number
)
fps
: 30 } },
({
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
)
})
},
)

The band moves as before, and the counter grows by about 30 a second while the image is on screen. Frames caused by scrolling or the pointer still run at the full rate.

Why it works

Diorama runs frames on demand. A frame runs when something calls for it: an input event, a param write, a fading input, or an effect whose policy asks for frames. When nothing asks, the loop sleeps and the GPU does no work. Each effect on screen asks according to its policy, and an effect off screen or in a hidden tab asks for nothing.

u.time and u.hoverTime count only the seconds the effect runs, so an animation continues where it stopped instead of jumping ahead after a pause.