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 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{
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).
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{
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.
The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.
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.
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.
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.
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{
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.
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.
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.
Related
Markup: attributes and their short and addressed forms.