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

Drive params from code

Attach an effect from code, keep its handle and write its params from a slider, then reach an instance created by markup.

View as Markdown
On this page

In this step you put mono from step 2 on a gallery from code and drive its strength with a range slider. Then you change the params of an image that got its effect from markup.

This is the effect, unchanged.

src/effects/mono.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 luminance: (
color: Node,
luminanceCoefficients?: Node
) => Node<"float">
luminance
,
const mix: Mix
mix
,
const vec3: Vec3Function
vec3
,
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 strength: 1;
readonly ink: "#1d2b53";
}

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,
ink: "#1d2b53"
ink
: '#1d2b53' },
({
surface: Surface

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

surface
,
u: Uniforms<
ColorsIn<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>

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
) => {
const
const gray: Node<"float">
gray
=
function luminance(
color: Node,
luminanceCoefficients?: Node
): Node<"float">
luminance
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
)
const
const toned: Node<"vec3">
toned
=
function mix(
a: Vec3OrFloat,
b: Vec3OrFloat,
t: Vec3OrFloat
): Node<"vec3"> (+3 overloads)
mix
(
u: Uniforms<
ColorsIn<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>

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

u
.
ink: UniformNode<"color">
ink
,
function vec3(x?: number, y?: number, z?: number): ConstNode<
"vec3",
Vector3
> (+6 overloads)
vec3
(1),
const gray: Node<"float">
gray
)
return
function vec4(
xyz:
| Node<"vec3">
| Node<"color">
| Vector3
| Color
| Node<"vec4">,
w: Scalar
): JoinNode<"vec4"> (+10 overloads)
vec4
(
function mix(
a: Vec3OrFloat,
b: Vec3OrFloat,
t: Vec3OrFloat
): Node<"vec3"> (+3 overloads)
mix
(
color: Node<"vec4">
color
.
Swizzle3From3<"float">.rgb: Node<"vec3">
rgb
,
const toned: Node<"vec3">
toned
,
u: Uniforms<
ColorsIn<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>

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

u
.
strength: UniformNode<"float">
strength
),
color: Node<"vec4">
color
.
Swizzle1From4<"float">.a: Node<"float">
a
)
})
},
)

Attach from code

The gallery has no data-dio attributes. A slider below it sets how monochrome the images are.

index.html
<div class="gallery">
<img src="/work/aurora.jpg" width="400" height="300" />
<img src="/work/lantern.jpg" width="400" height="300" />
</div>
<input
class="strength"
type="range"
min="0"
max="1"
step="0.01"
value="1"
aria-label="Monochrome"
/>

app.attach() puts an effect on every element that matches a selector and returns a handle. Write to handle.params and every image under the handle changes on the next frame.

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 mono: EffectHandle<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>
mono
=
const app: App
app
.
Services.attach: Attach
<"mono">(
target: Target,
name: "mono",
params?:
| Partial<
ParamsOf<
WidenSpec<
{ readonly strength: 1; readonly ink: "#1d2b53"; }
>
>
>
| undefined,
options?: AttachOptions
) => EffectHandle<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
> (+1 overload)
attach
('.gallery img', 'mono', {
strength?: number | undefined
strength
: 1 })
const
const slider: Element | null
slider
=
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
('.strength')
if (
const slider: Element | null
slider
instanceof
var HTMLInputElement: {
new (): HTMLInputElement;
prototype: HTMLInputElement;
}

The HTMLInputElement interface provides special properties and methods for manipulating the options, layout, and presentation of elements.

MDN Reference

HTMLInputElement
) {
const slider: HTMLInputElement
slider
.
HTMLInputElement.addEventListener<"input">(
type: "input",
listener: (this: HTMLInputElement, ev: InputEvent) => any,
options?: boolean | AddEventListenerOptions
): void (+1 overload)

The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

MDN Reference

addEventListener
('input', () => {
const mono: EffectHandle<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>
mono
.
EffectHandle<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>.params: LiveParams<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>

Runtime layer for every covered instance, now and later. Accessor properties, so tweenable.

params
.
strength: number
strength
=
const slider: HTMLInputElement
slider
.
HTMLInputElement.valueAsNumber: number

The valueAsNumber property of the HTMLInputElement interface represents the current value of the element as a number or NaN if converting to a numeric value is not possible.

MDN Reference

valueAsNumber
})
}

Live preview needs WebGPU or WebGL 2. Two prints with mono attached from code and a range slider under them that brings the color back in both at once.

This example failed to draw.

<div class="gallery">
<img
src="/media/prints/dawn.jpg"
width="400"
height="300"
/>
<img
src="/media/prints/night.jpg"
width="400"
height="300"
/>
</div>
<input
class="strength"
type="range"
min="0"
max="1"
step="0.01"
value="1"
aria-label="Monochrome"
/>
import { app } from '../../docs/diorama.ts'
export default (example: HTMLElement) => {
const mono = app.attach('.gallery img', 'mono', { strength: 1 })
const slider = example.querySelector('.strength')
if (slider instanceof HTMLInputElement) {
slider.addEventListener('input', () => {
mono.params.strength = slider.valueAsNumber
})
}
// The docs page goes away with its gallery; a site keeps the handle for good.
return () => mono()
}
import { effect } from '@dioramajs/diorama'
import { luminance, mix, vec3, vec4 } from 'three/tsl'
export default effect(
{ strength: 1, ink: '#1d2b53' },
({ surface, u }) => {
surface.color((color) => {
const gray = luminance(color.rgb)
const toned = mix(u.ink, vec3(1), gray)
return vec4(mix(color.rgb, toned, u.strength), color.a)
})
},
)

Drag the slider to the left and the color comes back in both images at once. The name 'mono' and the type of strength are checked against the effect file through src/diorama.gen.d.ts: a typo in the name or a string in strength is a type error.

The selector is a live rule, like a CSS rule. An image added to .gallery later gets the effect and the current strength too.

Write several params, then undo

handle.set() writes several params at once. handle.reset() drops what the handle wrote and goes back to the params given to attach(), then to the defaults. Calling the handle removes the effect from the images.

src/main.ts
const mono: EffectHandle<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>
mono
.
EffectHandle<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>.set(
values: Partial<
ParamsOf<
WidenSpec<
{ readonly strength: 1; readonly ink: "#1d2b53"; }
>
>
>
): void

Writes several params at once, whole tuples included.

set
({
strength?: number | undefined
strength
: 0.5,
ink?: string | undefined
ink
: '#5b1a2e' })
const mono: EffectHandle<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>
mono
.
EffectHandle<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>.reset(param?: "strength" | "ink" | undefined): void

Drops the handle's writes (all, or one param) back to attributes, attach params or defaults.

reset
()
const mono: EffectHandle
() => void
mono
()

Change an instance from markup

An image with data-dio="mono" has its own instance, created by the markup. app.effects.get() returns it by element and effect name.

index.html
<img
class="hero"
src="/cover.jpg"
width="800"
height="450"
data-dio="mono"
data-dio-strength="1"
/>
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 hero: Element | null
hero
=
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
('.hero')
const
const instance: EffectInstance<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>
| null
| undefined
instance
=
const hero: Element | null
hero
&&
const app: App
app
.
Services.effects: EffectsApi
effects
.
EffectsApi.get<"mono">(
element: Element,
name: "mono"
): EffectInstance<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>
| undefined

The instance of an effect on an element, if any.

get
(
const hero: Element
hero
, 'mono')
if (
const instance: EffectInstance<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>
| null
| undefined
instance
)
const instance: EffectInstance<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>
instance
.
EffectInstance<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>.params: LiveParams<
WidenSpec<{ readonly strength: 1; readonly ink: "#1d2b53"; }>
>

Reads give the resolved value. Writes go to this element's runtime layer, which beats attributes.

params
.
strength: number
strength
= 0.3
Paper ridges at dusk

Live preview needs WebGPU or WebGL 2. A photo with data-dio-strength="1" that shows mostly in color, because a script set its strength to 0.3.

This example failed to draw.

<img
class="hero"
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="mono"
data-dio-strength="1"
/>
import { app } from '../../docs/diorama.ts'
export default (example: HTMLElement) => {
const hero = example.querySelector('.hero')
const instance = hero && app.effects.get(hero, 'mono')
if (instance) instance.params.strength = 0.3
}
import { effect } from '@dioramajs/diorama'
import { luminance, mix, vec3, vec4 } from 'three/tsl'
export default effect(
{ strength: 1, ink: '#1d2b53' },
({ surface, u }) => {
surface.color((color) => {
const gray = luminance(color.rgb)
const toned = mix(u.ink, vec3(1), gray)
return vec4(mix(color.rgb, toned, u.strength), color.a)
})
},
)

The image shows mostly in color, although the attribute still says 1.

Why it works

A param can come from four places, and the stronger one wins, the way CSS orders its sources. From weakest to strongest: the default in the effect file, the params given to attach(), the data-dio-* attribute, and writes from code to params. A write from code beats the attribute, so a tween from code works on an element with an attribute. reset() removes the writes, and the attribute shows again.

A write changes a uniform and asks for one frame, and only if the image is on screen. A write to an image off screen costs no frame; it shows when the image scrolls in.