Diorama

Made for a big screen.

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.

GitHub

DioramaDocs

hotspots

3D and product

@dioramajs/model/hotspotsSource

Feature callouts pinned to points of a model, real buttons that follow it as it turns.

View as Markdown
On this page

Live preview needs WebGPU or WebGL 2. A rangefinder camera with four callouts whose leader lines follow their features as it turns; the ones behind it fade.

This example failed to draw.

Drag the camera, or press Tab to move through the callouts. Each one turns the camera to its feature: the eyepiece is on the back. The ones behind it fade.
Params of hotspots
occlude

normal hides a point label whose point faces away from the viewer (from the center of the model, or along `data-dio-normal`), and a node label once its part is well behind the center, so exploded parts keep theirs.

Usage

Put it on a parent of the data-dio="model" element, holding the labels next to it: the drawn model element hides its own children. Labels get a z-index above a canvas over the page; a sticky stage around them needs one too, or the model covers them (a development note names it). start() registers hotspots, 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.

Terminal window
npx @dioramajs/diorama add hotspots
<figure class="product" data-dio="hotspots">
<div
class="camera"
data-dio="model"
data-dio-src="/media/camera/camera.glb"
data-dio-yaw="-30"
data-dio-pitch="16"
role="img"
aria-label="A rangefinder camera in 3D. Drag to turn it, or pick a feature to turn it there."
style="touch-action: pan-y"
>
<img
src="/media/camera/camera.webp"
width="900"
height="600"
/>
</div>
<ul>
<li>
<button
type="button"
class="spot"
data-dio-at="Shutter dial"
>
<span>
<b>Shutter dial</b>
1 s to 1/1000
</span>
</button>
</li>
<li>
<button
type="button"
class="spot"
data-dio-at="Rewind knob"
>
<span>
<b>Rewind knob</b>
Lifts to load film
</span>
</button>
</li>
<li>
<button
type="button"
class="spot"
data-dio-at="Front ring"
data-dio-normal="0 0 1"
>
<span>
<b>50 mm lens</b>
f/1.4, six elements
</span>
</button>
</li>
<li>
<button
type="button"
class="spot"
data-dio-at="Eyepiece"
data-dio-normal="0 0 -1"
>
<span>
<b>Eyepiece</b>
Bright frame finder
</span>
</button>
</li>
</ul>
</figure>
import { start } from '@dioramajs/diorama'
const app = start()
app.attach('.product', 'hotspots', {
occlude: 'normal',
face: true,
})
import { start } from '@dioramajs/diorama'
import hotspots from '@dioramajs/model/hotspots'
const app = start()
app.attach('.product', hotspots({
occlude: 'normal',
face: true,
}))

Markup it reads

  • data-dio-at: A point in the model file's own coordinates as x y z (Alt+click on the model logs one in development), or the name of a node whose center the label follows, also while explode moves it.
  • data-dio-normal: The direction the point faces as x y z, for the normal test; without it, the direction from the center of the model.
  • data-dio-side: Set by the effect to left or right: the side of the model the label's point is on, where its card goes. With it come --dio-reach, px from the point to the near edge of the card, just past the model's edge and inside the element, and --dio-shift, px from the point to the middle of the card, so the cards on a side never overlap. The card is the label's last child element, or the label itself; place it with these in CSS.
  • data-dio-away: Set by the effect on a label turned away, which also gets opacity 0; add transition: opacity 0.2s to fade it.

Page transitions

Labels fade with the model during a page transition. How a flight flies.

Params

Every param is also an attribute, and both app.attach() and the handle take it by name.

ParamTypeDefaultDescription
modelrebuildsdata-dio-modelstring''Selector of the model element, looked up inside the element and then in the page; empty takes the first data-dio="model" inside.
occluderebuildsdata-dio-occlude'normal' | 'ray' | 'none''normal'
  • normal hides a point label whose point faces away from the viewer (from the center of the model, or along data-dio-normal), and a node label once its part is well behind the center, so exploded parts keep theirs.
  • ray hides a label wherever the model covers its point, for rooms, hollow shapes and parts inside a shell, settling a moment after the model stops.
  • none never hides.
facedata-dio-facebooleantrueFocusing a label turns the model to face its point.

Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.

Inputs and frames

It reads height, presence, width, x and y . Draws only when you scroll, move the pointer over it or change a param. None of its own: labels move in the frames the model draws, written once per frame as a transform and two custom properties. Card sizes are read once and on resize. Focusing a label draws the turn of the model. 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. Labels follow the model as it stands; focusing a label turns the model at once, without easing.

Runs on every device. A tap on a label is a click and turns the model to it; a press on a label never starts a drag of the model.

To change what reduced motion does, patch it in start().

import { start } from '@dioramajs/diorama'
start({ effects: { hotspots: { reducedMotion: 'off' } } })

Cost and quality

Download
4.3 KB min+gzip the first time a page uses it
GPU
None: the labels are DOM.
Memory
One empty node per label. With `occlude="ray"`, a ray test against the model for two labels per frame and for all of them once it stops.
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

Put the labels next to the model

hotspots goes on an element that holds both the model element and the labels. Not on the model element itself: once the model draws, its element is hidden with everything inside it. The labels can sit in a list, with any markup, and stay buttons or links the page styles and handles.

<figure class="product" data-dio="hotspots">
<div
class="viewer"
data-dio="model"
data-dio-src="/models/speaker.glb"
>
<img src="/models/speaker.png" />
</div>
<ul>
<li>
<button
type="button"
data-dio-at="Knob"
>Volume knob</button>
</li>
<li>
<button
type="button"
data-dio-at="0 0.12 0.072"
>Fabric grille</button>
</li>
</ul>
</figure>

Find a point

data-dio-at takes either the name of a node in the file, whose center the label follows, or a point in the file’s own units. To get a point, hold Alt and click the model in development: the console logs a ready data-dio-at for the spot under the pointer, with the name of the mesh it hit.

Labels on an exploded view

Name the nodes, and the labels move with their parts as explode takes the model apart.

<div class="product" data-dio="hotspots">
<div
class="viewer"
data-dio="model explode"
data-dio-src="/models/speaker.glb"
data-dio-mode="extras"
>
<img src="/models/speaker.png" />
</div>
<button
type="button"
data-dio-at="Battery"
>12 hour battery</button>
<button
type="button"
data-dio-at="Driver"
>50 mm driver</button>
</div>

Rooms and hollow shapes

occlude="normal" hides a point that faces away from the viewer, measured from the center of the model, which suits rounded products. In a room, a building or a car interior that guess is wrong: occlude="ray" tests whether the model covers each point. It costs a ray test against the model for two labels per frame, and for all of them a moment after the model stops. Give a point its own facing with data-dio-normal="x y z" when it sits on a flat face.

Notes

The labels move by transform only, written once per frame and only when they move; the page never lays out again for them. A label turned away gets data-dio-away, opacity: 0 and no pointer events. Add transition: opacity 0.2s to fade it. A focused label always shows, so the focus ring never hides behind the model.

The canvas draws over the page, so the labels get a z-index one above it. A sticky stage around them starts its own stacking context below the canvas; give it a z-index above the canvas (2 with the default) or the model covers the labels. In development a note names the element.

Without WebGL, before the model loads and for screen readers, the labels are what they are in the markup: a list of buttons next to the poster. Write them so that list reads well.

A label is centered on its point. The callouts above are CSS only: the button is the dot on the point, its ::before draws the leader line, and a <span> inside is the card at the end of it. The effect lays the cards out: each label gets data-dio-side="left" or "right", the side of the model its point is on, --dio-reach, how far out its card goes to clear the model while staying inside the element, and --dio-shift, how far up or down so the cards on one side never overlap. The leader line is width: hypot(var(--dio-reach), var(--dio-shift)) turned by atan2() of the two. The callouts switch on once the model draws, with .product:has(> .camera[data-dio-state="drawn"]), so the list stays a list without WebGL.