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.
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{
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).
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.
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{
functionstart(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'
exportconst
const app:App
app=
functionstart(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.
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{
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).
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.
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.