model
A glTF model fitted into the element, lit, turnable and recolored from markup.
Live preview needs WebGPU or WebGL 2. A low lounge chair in moss velvet in 3D, resting in a three-quarter view, turned by a drag or the arrow keys and recolored by the color param.
This example failed to draw.
Usage
Put it on any element; put a poster <img> inside, it shows without WebGL and until the model is ready. start() registers model, so the attribute alone puts it on an element. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add model<div class="chair" data-dio="model" data-dio-src="/media/product/lounge.glb" data-dio-part="Fabric" data-dio-color="#434c27" data-dio-yaw="32" data-dio-pitch="10" data-dio-scale="0.8" role="img" aria-label="Lounge chair in moss velvet, in 3D. Drag or use the arrow keys to turn it." tabindex="0" style="touch-action: pan-y"> <img src="/media/product/lounge-moss.webp" width="960" height="800" /></div>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.chair', 'model', { color: '#434c27', duration: 0.4, scale: 0.8, yaw: 32, pitch: 10, spin: 0, turn: 0, tilt: 8.6, drag: true, exposure: 1,})import { start } from '@dioramajs/diorama'import model from '@dioramajs/model/model'
const app = start()app.attach('.chair', model({ color: '#434c27', duration: 0.4, scale: 0.8, yaw: 32, pitch: 10, spin: 0, turn: 0, tilt: 8.6, drag: true, exposure: 1,}))Page transitions
Leaves by dithering its pixels away while shrinking to 96%, so its opaque materials never switch to transparent mid-transition. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
srcrebuildsdata-dio-src | string | '' | URL of the GLB or glTF file. |
partrebuildsdata-dio-part | string | '' | Material or mesh names to recolor, comma separated; empty keeps the file's colors. |
colordata-dio-color | color | '#ffffff' | Color of the part, any CSS color. |
swatchesrebuildsdata-dio-swatches | string | '' | Selector of a radio group or <select> whose pick recolors the part, as color does. |
durationdata-dio-duration | number0 to 2 s | 0.4 | Time a color or rest pose change takes. |
scaledata-dio-scale | number0.1 to 1.5 | 0.9 | Size in the box: 1 fits the model as it is seen in its rest pose, its near side larger in perspective, centered on what is drawn. |
yawdata-dio-yaw | number-180 to 180° | 0 | Rest pose turn around the vertical axis; positive turns it as a drag to the right does. |
pitchdata-dio-pitch | number-20 to 20° | 0 | Rest pose tilt toward the viewer; positive shows it from above, -20 to 20. |
spindata-dio-spin | number-360 to 360 deg/s | 0 | Turns on its own around the vertical; 0 stands still. |
turndata-dio-turn | number-720 to 720° | 0 | Turns as the element passes through the viewport. |
tiltdata-dio-tilt | number0 to 35° | 8.6 | How far it leans toward the pointer. |
dragdata-dio-drag | boolean | true | Turns it under a drag, and with the arrow keys when it has a tabindex. |
cliprebuildsdata-dio-clip | string | '' | Clip to loop by name, * for the first; step keys alone draw only at each key. |
exposuredata-dio-exposure | number0.2 to 3 | 1 | Exposure of the tone mapping. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads delta, height, over, pointer, presence, progress, reducedMotion and width
. Draws only while something on it moves, then stops. Only while something moves: spin, a clip, drag inertia, the tilt settling, or a color or rest pose blending. A clip whose every track steps from key to key, like the hands of a ticking watch, draws one frame per key on a timer while visible, so a watch at rest costs a frame a second. None at rest without spin and clip. How a frame is decided.
Reduced motion and touch
With reduced motion it shows a still frame: motion inputs are zero and time stands still. The rest pose shows; spin, clips, scroll turn and tilt stop, and a new rest pose is taken at once. Dragging, arrow keys and color changes still work: the visitor starts them.
Runs on every device. No tilt, nothing hovers. A finger drags the model only when the element has `touch-action: pan-y` or `none`, otherwise the gesture scrolls the page.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { model: { reducedMotion: 'off' } } })Cost and quality
- Download
- 5.2 KB min+gzip the first time a page uses it
- GPU
- The model as authored, lit by one prefiltered environment. One own material per material of the file.
- Memory
- Geometry and textures of the file, shared with every instance of the same URL, and the environment map.
- Frames
- Idle at rest
It runs from the low tier up. Every tier draws it the same way. The quality plugin picks the tier for each device.
Recipes
A product configurator
swatches points at a radio group or a <select>, and the part named in part takes the color of the picked value, easing to it over duration seconds in a perceptual color space. The radios stay a form: they submit, work with the keyboard and read as a group to a screen reader.
Live preview needs WebGPU or WebGL 2. The lounge chair over six velvet swatches; picking one repaints the seat and back.
This example failed to draw.
<div class="configurator"> <div class="chair" data-dio="model" data-dio-src="/media/product/lounge.glb" data-dio-part="Fabric" data-dio-swatches="#velvet" data-dio-yaw="32" data-dio-pitch="10" data-dio-scale="0.8" role="img" aria-label="Lounge chair in 3D, in the velvet picked below. Drag or use the arrow keys to turn it." tabindex="0" style="touch-action: pan-y" > <img src="/media/product/lounge-moss.webp" width="960" height="800" /> </div> <fieldset id="velvet" class="velvets"> <legend>Velvet</legend> <label> <input type="radio" name="velvet" value="#434c27" checked /> Moss </label> <label> <input type="radio" name="velvet" value="#a8700f" /> Saffron </label> <label> <input type="radio" name="velvet" value="#8a3519" /> Rust </label> <label> <input type="radio" name="velvet" value="#0d4a4f" /> Peacock </label> <label> <input type="radio" name="velvet" value="#232b42" /> Ink </label> <label> <input type="radio" name="velvet" value="#b9b09f" /> Oyster </label> </fieldset></div>part takes material or mesh names from the file, separated by commas. A name that is not there logs the names it has in development, so open the console when the color does not change. Give each velvet its own poster, too: without WebGL the <img> is all the visitor sees.
Turn it as the page scrolls
turn adds that many degrees of turn while the element crosses the viewport, on top of the rest pose and whatever the visitor dragged, and it faces the rest pose when the element is in the middle. Inside a data-dio-section it takes the progress of the whole section instead, so it keeps turning while a sticky stage holds still.
<section class="materials" data-dio-section style="height: 300vh"> <div class="stage" style="position: sticky; top: 0; height: 100vh" > <div data-dio="model" data-dio-src="/models/chair.glb" data-dio-turn="360" data-dio-drag="false" > <img src="/models/chair.avif" /> </div> </div></section>A hero that turns on its own
spin turns it around the vertical axis in degrees per second. Keep it slow: 10 to 20 reads as a display stand, faster reads as a loading spinner. Under reduced motion it rests in its pose.
<div class="hero" data-dio="model" data-dio-src="/models/headphones.glb" data-dio-spin="14.4" data-dio-pitch="12" data-dio-load="eager"> <img src="/img/headphones.avif" /></div>Play an animation clip
clip loops one of the file’s animations by name, or the first with *. From code, the same params go to attach().
app.attach('.robot', 'model', { src: '/models/robot.glb', clip: 'Walk', drag: false, scale: 0.8,})Notes
The model draws only while something moves: a drag, spin, a clip, a color or pose easing, or the tilt settling. A chair at rest costs no frames. The file is the cost that matters: this chair is 1.5 MB, a quarter of it textures. Compress geometry with Meshopt or Draco and textures with KTX2 before you ship; the 3D models guide shows the commands.
Put a poster <img> inside the element, rendered from the same view. It shows while the file loads, without WebGL and in search results, and the model takes its place only once it has drawn. Match yaw, pitch and scale to the poster so the swap does not jump.
A turning model needs a name for assistive technology: role="img" with an aria-label that says what it is and how to turn it, and tabindex="0" for the arrow keys. Without tabindex it never takes focus, and drag alone needs a mouse or a finger.
On touch screens nothing hovers, so tilt does nothing. A finger turns the model only with touch-action: pan-y or none on the element; otherwise the gesture scrolls the page, which is the right default for a model in the flow of a long page.