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.
In this tutorial you write lamp, an effect for a product page. While the pointer is over the photo, the photo dims and a soft light follows the pointer, as if someone moved a lamp over the product. By the end the light trails the pointer smoothly, fades in and out, follows a finger without blocking the scroll, keeps still for visitors who prefer reduced motion and takes its color from markup. The finished file is about 40 lines long.
You need a site with Diorama and an effects folder; Install with Vite or the page for your framework sets both up. The steps use src/effects/, which is app/effects/ in Nuxt.
1. Light the photo at the pointer
An effect is a file in the effects folder whose default export is effect(). The file name is the effect name: src/effects/lamp.ts is lamp. The first argument of effect() holds the params and their defaults. The second is the setup function, which runs once per element and describes how its picture changes.
src/effects/lamp.ts
import{
functioneffect(
setup:EffectSetup<Empty>
):Effect<Empty>(+2overloads)
Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its
registry key (the file name when a build plugin finds it).
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)),
color: Node<"vec4">
color.
Swizzle1From4<"float">.a: Node<"float">
a)
})
},
)
surface.color() takes a function that gets the color of each pixel of the photo and returns a new one. It is written in TSL, the shading language of three.js, and runs on the GPU. u holds the params and the built-in inputs as values the GPU reads.
{ box }, the second argument of the color function, holds the position of the pixel, 0 to 1 across and from bottom to top. u.pointer is the pointer in the same units and u.size is the size of the photo in CSS px, so distance is in px.
lit is 1 under the pointer and falls to 0 at radius px from it.
light is what the pixel is multiplied by: 1 - dim far from the pointer, u.color times u.strength under it. A strength above 1 makes the lit part brighter than the photo, so even a black camera body catches the light.
u.hover rises from 0 to 1 while the pointer is over the photo and falls back when it leaves, so the light fades in and out.
Move the pointer over the photo. The light works, but it is glued to the pointer and jerks with every move of the mouse. Two other problems are harder to see. Under reduced motion u.hover stays at 0, so those visitors never see the light. And reading u.pointer makes every pointer move anywhere on the page draw a frame while the photo is on screen.
2. Ease the lamp with chase and approach
Smoothing needs a value that carries over from one frame to the next, which is a job for JavaScript. Setup can return an update function: it runs in every frame Diorama draws while the photo is on screen, gets the inputs as numbers and writes uniforms, the values the shader reads.
src/effects/lamp.ts
import{
functioneffect(
setup:EffectSetup<Empty>
):Effect<Empty>(+2overloads)
Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its
registry key (the file name when a build plugin finds it).
Moves current towards target by at most step: a linear fade, delta / seconds a frame.
approach,
functionchase(
at:{ x:number; y:number;},
aim:{readonly x:number;readonly y:number;},
seconds:number,
delta:number,
near?:number
):boolean
Moves at towards aim with damp on both axes and lands on it once within near (a
distance), like damp's own near. Returns whether it is still on its way, which an update
can return to keep frames coming. seconds 0 jumps.
chase}from'@dioramajs/diorama/kit'
import{
const mix:Mix
mix,
const smoothstep:Smoothstep
smoothstep,
const uniform:Uniform
TSL function for creating a uniform node.
@function
@param ― value - The value of this uniform or your type. Usually a JS primitive or three.js object (vector, matrix, color, texture).
@param ― type - The node type. If no explicit type is defined, the node tries to derive the type from its value.
@returns
uniform,
const vec3:Vec3Function
vec3,
const vec4:Vec4Function
vec4}from'three/tsl'
import{
classVector2
2D vector.
Vector2}from'three/webgpu'
// Seconds to cover most of the way to the pointer, and of a fade.
const
const LAG:0.12
LAG=0.12
const
const FADE:0.3
FADE=0.3
exportdefault
effect<P>(
params: P & CheckSpec<P>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>>(+2 overloads)
typeP={
readonly radius:160;
readonly dim:0.5;
readonly strength:1.5;
readonly color:"#fff1dc";
}
Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its
registry key (the file name when a build plugin finds it).
Pointer is over the box right now, without damping or reduced motion.
over?1:0
// Off, the lamp jumps to the pointer instead of sliding in.
const
const lag:0.12|0
lag=
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value===0?0:
const LAG:0.12
LAG
const
const moving:boolean
moving=
functionchase(
at:{ x:number; y:number;},
aim:{readonly x:number;readonly y:number;},
seconds:number,
delta:number,
near?:number
):boolean
Moves at towards aim with damp on both axes and lands on it once within near (a
distance), like damp's own near. Returns whether it is still on its way, which an update
can return to keep frames coming. seconds 0 jumps.
chase(
const at:UniformNode<"vec2",Vector2>
at.
InputNodeInterface<Vector2>.value: Vector2
value,
const aim:{
x:number;
y:number;
}
aim,
const lag:0.12|0
lag,
input: EffectInput
input.
EffectInput.delta: number
Seconds since the previous frame.
delta,0.5)
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value=
functionapproach(
current:number,
target:number,
step:number
):number
Moves current towards target by at most step: a linear fade, delta / seconds a frame.
approach(
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value,
const target:0|1
target,
input: EffectInput
input.
EffectInput.delta: number
Seconds since the previous frame.
delta/
const FADE:0.3
FADE)
// true asks for the next frame, until the lamp settles.
return
const moving:boolean
moving||
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value!==
const target:0|1
target
}
},
)
at is the lamp’s position in px from the bottom left of the photo, and on is how far the lamp is on, from 0 to 1. The shader reads them in place of u.pointer and u.hover.
input.pointer is the pointer in the same units as u.pointer, and input.over is true while the pointer is over the photo.
chase() moves at.value toward aim and covers most of the way in LAG seconds at any frame rate. Within 0.5 px it lands on aim and returns false. The 0.5 is optional, as in damp(), but without it the lamp lands only on the exact spot, after seconds of frames that change nothing you can see. With 0 seconds it jumps, which the lamp does while it is off, so it lights up at the pointer instead of sliding in from where it went out.
approach() moves on toward its target by input.delta / FADE per frame, so a fade takes 0.3 seconds at any frame rate.
chase() and approach() come from @dioramajs/diorama/kit, the helpers the built-in effects are written with.
The update returns whether the lamp still moves. Diorama draws a frame only when something asks for one, and true from the update asks for the next frame. While the lamp moves or fades, each frame asks for the next; once it settles, the update returns false and the frames stop. The answer also takes the place of the clocks: reading input.delta counts as reading a clock, and an update that reads it and returns nothing draws every frame while the photo is on screen. Return true or false on every path of the update.
Live preview needs WebGPU or WebGL 2. The camera photo with a warm light that trails the pointer and fades in and out with it.
// true asks for the next frame, until the lamp settles.
return moving || on.value!== target
}
},
)
The light trails the pointer a little and catches up when the pointer stops. It fades in where the pointer enters and fades out where it leaves.
3. Follow a finger with follow
input.pointer and input.over describe one pointer for the whole page. follow() from @dioramajs/diorama/kit listens to this photo only, the way a light wants it:
a mouse or a pen while it is over the photo;
the first finger while it is pressed on the photo. When the finger starts a scroll, the browser takes it with pointercancel, the lamp goes out and the page scrolls as usual.
src/effects/lamp.ts
import{
functioneffect(
setup:EffectSetup<Empty>
):Effect<Empty>(+2overloads)
Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its
registry key (the file name when a build plugin finds it).
Moves current towards target by at most step: a linear fade, delta / seconds a frame.
approach,
functionchase(
at:{ x:number; y:number;},
aim:{readonly x:number;readonly y:number;},
seconds:number,
delta:number,
near?:number
):boolean
Moves at towards aim with damp on both axes and lands on it once within near (a
distance), like damp's own near. Returns whether it is still on its way, which an update
can return to keep frames coming. seconds 0 jumps.
Follows the pointer the way a light or a brush on an element wants it: a mouse or pen while it
is over the element, the first finger while it is pressed on it. Touch never blocks the page's
scroll: the browser takes the finger with pointercancel when one starts. Each change asks
for a frame. Listeners go with ctx.signal. Returns the live pointer; on adds callbacks.
follow}from'@dioramajs/diorama/kit'
import{
const mix:Mix
mix,
const smoothstep:Smoothstep
smoothstep,
const uniform:Uniform
TSL function for creating a uniform node.
@function
@param ― value - The value of this uniform or your type. Usually a JS primitive or three.js object (vector, matrix, color, texture).
@param ― type - The node type. If no explicit type is defined, the node tries to derive the type from its value.
@returns
uniform,
const vec3:Vec3Function
vec3,
const vec4:Vec4Function
vec4}from'three/tsl'
import{
classVector2
2D vector.
Vector2}from'three/webgpu'
// Seconds to cover most of the way to the pointer, and of a fade.
const
const LAG:0.12
LAG=0.12
const
const FADE:0.3
FADE=0.3
exportdefault
effect<P>(
params: P & CheckSpec<P>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>>(+2 overloads)
typeP={
readonly radius:160;
readonly dim:0.5;
readonly strength:1.5;
readonly color:"#fff1dc";
}
Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its
registry key (the file name when a build plugin finds it).
Follows the pointer the way a light or a brush on an element wants it: a mouse or pen while it
is over the element, the first finger while it is pressed on it. Touch never blocks the page's
scroll: the browser takes the finger with pointercancel when one starts. Each change asks
for a frame. Listeners go with ctx.signal. Returns the live pointer; on adds callbacks.
follow(
ctx: EffectContext<
ColorsIn<
{
readonly radius:160;
readonly dim:0.5;
readonly strength:1.5;
readonly color:"#fff1dc";
}
>
>
ctx)
return(
input: EffectInput
input)=>{
const
const target:0|1
target=
const pointer:FollowedPointer
pointer.
FollowedPointer.over: boolean
A mouse or pen is over the element, or a finger is pressed on it.
over?1:0
// Off, the lamp jumps to the pointer instead of sliding in.
const
const lag:0.12|0
lag=
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value===0?0:
const LAG:0.12
LAG
const
const aim:{
readonly x:number;
readonly y:number;
}
aim=
const pointer:FollowedPointer
pointer.
FollowedPointer.box(box: ClientBox): {
readonly x: number;
readonly y: number;
}
Its last position in px of box with y up, as toBox gives it: pass input in the update,
so a still pointer keeps its place on the page while it scrolls. The same object every call.
box(
input: EffectInput
input)
const
const moving:boolean
moving=
functionchase(
at:{ x:number; y:number;},
aim:{readonly x:number;readonly y:number;},
seconds:number,
delta:number,
near?:number
):boolean
Moves at towards aim with damp on both axes and lands on it once within near (a
distance), like damp's own near. Returns whether it is still on its way, which an update
can return to keep frames coming. seconds 0 jumps.
chase(
const at:UniformNode<"vec2",Vector2>
at.
InputNodeInterface<Vector2>.value: Vector2
value,
const aim:{
readonly x:number;
readonly y:number;
}
aim,
const lag:0.12|0
lag,
input: EffectInput
input.
EffectInput.delta: number
Seconds since the previous frame.
delta,0.5)
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value=
functionapproach(
current:number,
target:number,
step:number
):number
Moves current towards target by at most step: a linear fade, delta / seconds a frame.
approach(
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value,
const target:0|1
target,
input: EffectInput
input.
EffectInput.delta: number
Seconds since the previous frame.
delta/
const FADE:0.3
FADE)
// true asks for the next frame, until the lamp settles.
return
const moving:boolean
moving||
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value!==
const target:0|1
target
}
},
)
follow() takes the context and returns the pointer it follows. pointer.over is true while a mouse is over the photo or a finger is pressed on it. pointer.box(input) is its position in px from the bottom left of the photo, the units of at. It is measured from the box of this frame, so the lamp stays under a still pointer while the page scrolls. Each move asks for a frame by itself.
The setup function now takes the whole context as ctx, because follow() needs its element, its abort signal and invalidate(). The listeners go when the effect leaves the element: the signal aborts and removes them. The effect no longer reads the page’s pointer, so a pointer that moves elsewhere on the page draws nothing for this photo.
A pressed finger lights the photo where it touches, and a drag scrolls the page. To let a finger drag the lamp across the photo instead, add style="touch-action: none" to the image. The page then does not scroll from a drag that starts on the photo.
4. Respect reduced motion
input.reducedMotion is true when the visitor asked the system for less motion. The light is not motion, but the trail and the fade are. With the setting on, the lamp jumps to the pointer and turns on and off at once.
src/effects/lamp.ts
import{
functioneffect(
setup:EffectSetup<Empty>
):Effect<Empty>(+2overloads)
Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its
registry key (the file name when a build plugin finds it).
Moves current towards target by at most step: a linear fade, delta / seconds a frame.
approach,
functionchase(
at:{ x:number; y:number;},
aim:{readonly x:number;readonly y:number;},
seconds:number,
delta:number,
near?:number
):boolean
Moves at towards aim with damp on both axes and lands on it once within near (a
distance), like damp's own near. Returns whether it is still on its way, which an update
can return to keep frames coming. seconds 0 jumps.
Follows the pointer the way a light or a brush on an element wants it: a mouse or pen while it
is over the element, the first finger while it is pressed on it. Touch never blocks the page's
scroll: the browser takes the finger with pointercancel when one starts. Each change asks
for a frame. Listeners go with ctx.signal. Returns the live pointer; on adds callbacks.
follow}from'@dioramajs/diorama/kit'
import{
const mix:Mix
mix,
const smoothstep:Smoothstep
smoothstep,
const uniform:Uniform
TSL function for creating a uniform node.
@function
@param ― value - The value of this uniform or your type. Usually a JS primitive or three.js object (vector, matrix, color, texture).
@param ― type - The node type. If no explicit type is defined, the node tries to derive the type from its value.
@returns
uniform,
const vec3:Vec3Function
vec3,
const vec4:Vec4Function
vec4}from'three/tsl'
import{
classVector2
2D vector.
Vector2}from'three/webgpu'
// Seconds to cover most of the way to the pointer, and of a fade.
const
const LAG:0.12
LAG=0.12
const
const FADE:0.3
FADE=0.3
exportdefault
effect<P>(
params: P & CheckSpec<P>,
setup: EffectSetup<ColorsIn<P>>
): Effect<WidenSpec<P>>(+2 overloads)
typeP={
readonly radius:160;
readonly dim:0.5;
readonly strength:1.5;
readonly color:"#fff1dc";
}
Short form of defineEffect: defaults, optional options, setup. An unnamed effect takes its
registry key (the file name when a build plugin finds it).
Follows the pointer the way a light or a brush on an element wants it: a mouse or pen while it
is over the element, the first finger while it is pressed on it. Touch never blocks the page's
scroll: the browser takes the finger with pointercancel when one starts. Each change asks
for a frame. Listeners go with ctx.signal. Returns the live pointer; on adds callbacks.
follow(
ctx: EffectContext<
ColorsIn<
{
readonly radius:160;
readonly dim:0.5;
readonly strength:1.5;
readonly color:"#fff1dc";
}
>
>
ctx)
return(
input: EffectInput
input)=>{
const
const target:0|1
target=
const pointer:FollowedPointer
pointer.
FollowedPointer.over: boolean
A mouse or pen is over the element, or a finger is pressed on it.
over?1:0
// Off, or under reduced motion, the lamp jumps to the pointer.
const
const still:boolean
still=
input: EffectInput
input.
EffectInput.reducedMotion: boolean
The page prefers reduced motion.
reducedMotion
const
const lag:0.12|0
lag=
const still:boolean
still||
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value===0?0:
const LAG:0.12
LAG
const
const aim:{
readonly x:number;
readonly y:number;
}
aim=
const pointer:FollowedPointer
pointer.
FollowedPointer.box(box: ClientBox): {
readonly x: number;
readonly y: number;
}
Its last position in px of box with y up, as toBox gives it: pass input in the update,
so a still pointer keeps its place on the page while it scrolls. The same object every call.
box(
input: EffectInput
input)
const
const moving:boolean
moving=
functionchase(
at:{ x:number; y:number;},
aim:{readonly x:number;readonly y:number;},
seconds:number,
delta:number,
near?:number
):boolean
Moves at towards aim with damp on both axes and lands on it once within near (a
distance), like damp's own near. Returns whether it is still on its way, which an update
can return to keep frames coming. seconds 0 jumps.
chase(
const at:UniformNode<"vec2",Vector2>
at.
InputNodeInterface<Vector2>.value: Vector2
value,
const aim:{
readonly x:number;
readonly y:number;
}
aim,
const lag:0.12|0
lag,
input: EffectInput
input.
EffectInput.delta: number
Seconds since the previous frame.
delta,0.5)
const
const step:number
step=
const still:boolean
still?1:
input: EffectInput
input.
EffectInput.delta: number
Seconds since the previous frame.
delta/
const FADE:0.3
FADE
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value=
functionapproach(
current:number,
target:number,
step:number
):number
Moves current towards target by at most step: a linear fade, delta / seconds a frame.
approach(
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value,
const target:0|1
target,
const step:number
step)
// true asks for the next frame, until the lamp settles.
return
const moving:boolean
moving||
const on:UniformNode<"float",number>
on.
InputNodeInterface<number>.value: number
value!==
const target:0|1
target
}
},
)
chase() with 0 seconds lands at once, and approach() with a step of 1 reaches 0 or 1 in one frame, so the update returns false in the same frame. Diorama reads the setting live, so the lamp changes as soon as the visitor turns it on. To try it, emulate prefers-reduced-motion: reduce in the Rendering panel of DevTools.
5. Set the color in markup
The lamp’s color is a color param because its default, '#fff1dc', is a CSS color. Every param is also an attribute, data-dio- plus its name, so each page can set its own lamp without touching the file. An amber light suits the brass of this camera.
product.html
<img
src="/products/camera.jpg"
width="900"
height="600"
data-dio="lamp"
data-dio-color="#ffc27a"
/>
Live preview needs WebGPU or WebGL 2. The camera photo with the finished lamp in amber, with sliders for radius, dim and strength and a color picker for the light.
// true asks for the next frame, until the lamp settles.
return moving || on.value!== target
}
},
)
Move the pointer over the photo, then drag the sliders. All four params are uniforms, so a new value draws one frame and compiles nothing.
A color param also takes a custom property of the site, such as data-dio-color="var(--brand)", and currentColor. Both are read from the photo’s computed style, and again in every frame the lamp draws, so a theme switch shows in the next one.
What it costs
The file is its own chunk. It loads with the engine, and only when an element with lamp nears the viewport. The helpers from @dioramajs/diorama/kit load with it, never with the script every page runs.
For each pixel the GPU reads the photo once, as it does without the effect, and does a few multiplications.
At rest the effect draws nothing. A pointer moving over the photo draws one frame per move, the trail and the fade draw for a fraction of a second after it, and then the frames stop. Frames and time shows how to count them.
Where the engine does not run, such as a browser without WebGPU and WebGL 2, the visitor sees the plain photo.
Compare with the built-in
Diorama ships the same idea as spotlight, with a softness param, a resting dim, a blur style and a reveal style that shows a second picture in the light. Copy its source into your effects folder to read it next to lamp.
It follows the pointer with follow(), chase() and approach() as lamp does, shapes the fade with ease() and returns from its update whether the light still moves. The copy replaces the built-in on your site; --as gives it another name and leaves the built-in in place.
Next steps
Write an effect: the rest of the effect API, from surface.uv() to your own three.js objects.