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 write shimmer, a band of light that sweeps across an image, and count the frames the page draws. You see the difference between u.time, which runs while the image is on screen, and u.hoverTime, which runs only while the pointer is over it.
Count the frames
Diorama draws a frame only when something on the canvas changes. To watch that, add a counter to the page and show app.loop.stats.frames, the number of frames run since the start.
index.html
<p>Frames: <outputclass="frames">0</output></p>
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 meter:Element|null
meter=
var document:Document
window.document returns a reference to the document contained in the window.
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(
const app:App
app.
AppCore<Services & Empty, string>.loop: Loop
Live state of the frame loop.
loop.
Loop.stats: FrameStats
Loop counters.
stats.
FrameStats.frames: number
Frames run so far.
frames)
},250)
Leave the pointer still and the counter stops. Scroll, or move the pointer, and it grows by a few frames for each move: every pointer move and scroll step can change what an effect sees, so each one runs a frame.
Animate with time
u.time is the number of seconds the element has been on screen. Use it to move a light band across the image once every four seconds.
src/effects/shimmer.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).
Live preview needs WebGPU or WebGL 2. A photo with a light band that sweeps across it all the time, next to a counter of the frames the page draws.
This example failed to draw.
The counter counts every frame of this page, so another example on screen adds to it.
Frames:
Last second:
<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="shimmer"
/>
import{ effect }from'@dioramajs/diorama'
import{ fract, smoothstep, vec4 }from'three/tsl'
exportdefaulteffect(
{speed:0.25,width:0.15},
({ surface, u })=>{
surface.color((color,{ box })=>{
// Where the band is, from -0.5 to 1.5, so it enters and leaves fully.
const at =fract(u.time.mul(u.speed)).mul(2).sub(0.5)
const d = box.x.add(box.y).mul(0.5).sub(at).abs()
const band =smoothstep(0, u.width, d).oneMinus().mul(0.35)
returnvec4(color.rgb.add(band), color.a)
})
},
)
import{ app }from'../../docs/diorama.ts'
// The counter of src/main.ts, plus the frames of the last second.
exportdefault(example:HTMLElement)=>{
const meter = example.querySelector('.frames')
const rate = example.querySelector('.rate')
let last = app.loop.stats.frames
let ticks =0
const timer =setInterval(()=>{
const frames = app.loop.stats.frames
if(meter) meter.textContent=String(frames)
if(++ticks %4===0){
if(rate) rate.textContent=String(frames - last)
last = frames
}
},250)
return()=>clearInterval(timer)
}
The band sweeps diagonally across the image, and the counter grows by about 60 a second, one frame per refresh of the display. Scroll the image off screen: the counter stops, and the band waits where it was. Switch to another tab: it stops too.
Run only on hover
An image that shimmers all the time costs a frame on every refresh while it is visible. Replace u.time with u.hoverTime, which runs only while the pointer is over the box, and multiply the band by u.hover so it fades out when the pointer leaves.
src/effects/shimmer.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).
Live preview needs WebGPU or WebGL 2. The photo with a band that sweeps only while the pointer is over it, next to the frame counter that stands still at rest.
This example failed to draw.
The counter counts every frame of this page, so another example on screen adds to it.
Frames:
Last second:
<img
src="/media/ridges.jpg"
width="800"
height="450"
data-dio="shimmer"
/>
import{ effect }from'@dioramajs/diorama'
import{ fract, smoothstep, vec4 }from'three/tsl'
exportdefaulteffect(
{speed:0.25,width:0.15},
({ surface, u })=>{
surface.color((color,{ box })=>{
const at =fract(u.hoverTime.mul(u.speed)).mul(2).sub(0.5)
const d = box.x.add(box.y).mul(0.5).sub(at).abs()
const band =smoothstep(0, u.width, d)
.oneMinus()
.mul(u.hover,0.35)
returnvec4(color.rgb.add(band), color.a)
})
},
)
import{ app }from'../../docs/diorama.ts'
// The counter of src/main.ts, plus the frames of the last second.
exportdefault(example:HTMLElement)=>{
const meter = example.querySelector('.frames')
const rate = example.querySelector('.rate')
let last = app.loop.stats.frames
let ticks =0
const timer =setInterval(()=>{
const frames = app.loop.stats.frames
if(meter) meter.textContent=String(frames)
if(++ticks %4===0){
if(rate) rate.textContent=String(frames - last)
last = frames
}
},250)
return()=>clearInterval(timer)
}
Now the counter stands still while the pointer rests outside the image. Over the image it grows by about 60 a second even when the pointer stops, and it stops a moment after the pointer leaves, once the band has faded out.
Choose the frame policy yourself
Diorama picks the frame policy from the inputs the effect reads: u.time means 'visible', u.hoverTime means 'hover', and neither means 'input'. An update function that returns true or false decides instead, as in Your own effect in 10 minutes. To set it yourself, pass the animate option between the params and the setup function. A shimmer does not need 60 frames a second; 30 is enough.
src/effects/shimmer.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:{
fps:
| number
|(
(
ctx: EffectContext<
ColorsIn<
{ readonly speed:0.25; readonly width: 0.15; }
>
>
)=> number
)
fps:30}},
({
surface: Surface
The shared GL copy of the element, created on first access.
The band moves as before, and the counter grows by about 30 a second while the image is on screen. Frames caused by scrolling or the pointer still run at the full rate.
Why it works
Diorama runs frames on demand. A frame runs when something calls for it: an input event, a param write, a fading input, or an effect whose policy asks for frames. When nothing asks, the loop sleeps and the GPU does no work. Each effect on screen asks according to its policy, and an effect off screen or in a hidden tab asks for nothing.
u.time and u.hoverTime count only the seconds the effect runs, so an animation continues where it stopped instead of jumping ahead after a pause.