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

Your first effect

Write an effect file, put it on an image with one attribute and change its param from markup and from code.

View as Markdown
On this page

An effect is a file in the effects folder that exports effect(). Its name is the file name: src/effects/brighten.ts is brighten, and HeroBrighten.ts is hero-brighten. Files that start with _ are helpers, not effects. This page uses the Vite setup; the folder is app/effects/ in Nuxt.

Write the effect

The first argument of effect() holds the params with their defaults. The setup function gets the effect context: surface changes the picture of the element, and u holds params and inputs as shader nodes.

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>,
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 }, ({
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
.
NumExtensions<"float">.add: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
add
(1)))
})

Param types come from the defaults: a number, a boolean, a tuple of 2 to 4 numbers, or a color such as '#ffd27a', rgb(...) or hsl(...). Use text('#cafe') for a string that only looks like a color.

Put it on an image

Use the file name in data-dio. One element can carry several effects, separated by spaces.

index.html
<img class="hero" src="/cover.jpg" data-dio="brighten" />

Set a param in markup

A param is an attribute named data-dio- plus the param name in kebab case: colorA is data-dio-color-a.

index.html
<img
class="hero"
src="/cover.jpg"
data-dio="brighten"
data-dio-strength="2"
/>

Here is the same effect running on this page. The slider writes strength the way code does, and the code under it follows.

Paper ridges at dusk

Live preview needs WebGPU or WebGL 2. A photo of paper ridges made brighter by the brighten effect, with strength set in markup.

This example failed to draw.

Params of brighten
<img
class="hero"
src="/media/ridges.jpg"
data-dio="brighten"
data-dio-strength="0.6"
/>
import { effect } from '@dioramajs/diorama'
export default effect({ strength: 1 }, ({ surface, u }) => {
surface.color((color) => color.mul(u.strength.add(1)))
})

Set it from code

app.attach() puts an effect on elements from code and returns a handle. Writes to handle.params draw in 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 brighten: EffectHandle<
WidenSpec<{ readonly strength: 1; }>
>
brighten
=
const app: App
app
.
Services.attach: Attach
<"brighten">(
target: Target,
name: "brighten",
params?:
| Partial<ParamsOf<WidenSpec<{ readonly strength: 1; }>>>
| undefined,
options?: AttachOptions
) => EffectHandle<
WidenSpec<{ readonly strength: 1; }>
> (+1 overload)
attach
('.hero', 'brighten', {
strength?: number | undefined
strength
: 2 })
const brighten: EffectHandle<
WidenSpec<{ readonly strength: 1; }>
>
brighten
.
EffectHandle<
WidenSpec<{ readonly strength: 1; }>
>.params: LiveParams<WidenSpec<{ readonly strength: 1; }>>

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

params
.
strength: number
strength
= 3

The name 'brighten' and the type of strength are checked against the effect file. The types come from src/diorama.gen.d.ts, which the plugin writes.

When it draws

This effect reads no time input, so it draws only when the page scrolls, the pointer moves over it or a param changes. An effect that reads u.time draws every frame while it is on screen; one that reads u.hoverTime draws while the pointer is over it and while hover fades. To choose yourself, pass options before the setup function.

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 },
{
animate?:
| AnimatePolicy<ColorsIn<{ readonly strength: 1; }>>
| 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
: 'visible',
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
.
NumExtensions<"float">.add: AddSubMulDivNumberVecNumExtensions
(
b: NumberParameterForNumType<"float">,
...params: NumberParameterForNumType<"float">[]
) => Node<"float"> (+3 overloads)
add
(1)))
},
)

when: 'hover' skips the effect on touch devices, so they never load the engine for it. Option names such as when, animate and media are never param names.