# hotspots

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

Goes on a parent of a `model` element. Its children with `data-dio-at` are lifted out of the flow and follow their points on the model while it turns, is dragged or explodes; a label whose point turns to the back of the model fades out and comes back as the point turns round. Focusing a label, by Tab or a click, turns the model to face its point. The labels stay ordinary buttons or links styled by the page, so keyboard, screen readers and the page without WebGL see a plain list.

Module `model`, category 3D and product. Import `@dioramajs/model/hotspots`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/model/src/hotspots.ts)

A rangefinder camera with four callouts whose leader lines follow their features as it turns; the ones behind it fade.

```html title="index.html"
<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" alt="" 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>
```

## 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.

```html
<figure class="product" data-dio="hotspots"><div class="viewer" data-dio="model" data-dio-src="/models/speaker.glb" style="touch-action: pan-y"><img src="/models/speaker.avif" alt="Speaker" /></div><button type="button" class="spot" data-dio-at="Driver">50 mm driver</button><button type="button" class="spot" data-dio-at="Battery">12 h battery</button><button type="button" class="spot" data-dio-at="0 0.17 0">Volume</button></figure>
```

- `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.

In page transitions: Labels fade with the model during a page transition.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `model` (rebuilds) | `data-dio-model` | `string` | `''` | Selector of the model element, looked up inside the element and then in the page; empty takes the first `data-dio="model"` inside. |
| `occlude` (rebuilds) | `data-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. |
| `face` | `data-dio-face` | `boolean` | `true` | Focusing a label turns the model to face its point. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `occlude`: `normal`, `ray`, `none`
  - `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.

## Inputs and frames

Reads [`height`](/docs/api/inputs#input-height), [`presence`](/docs/api/inputs#presence), [`width`](/docs/api/inputs#input-width), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-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. See [how a frame is decided](/docs/concepts/frames).

## 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.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { hotspots: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 4.3 KB 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.

See [the quality plugin](/docs/effects/quality).

## 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.

```html
<figure class="product" data-dio="hotspots">
  <div class="viewer" data-dio="model" data-dio-src="/models/speaker.glb"><img src="/models/speaker.png" alt="Desk speaker" /></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](/docs/effects/explode) takes the model apart.

```html
<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" alt="Desk speaker" /></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.

## Related

- [book](/docs/effects/book.md): A cover image made a 3D book that turns to show its spine and opens on hover.
- [box](/docs/effects/box.md): A product box built from pictures of its faces: turn it, tilt it, open its lid.
- [coin](/docs/effects/coin.md): A coin, medal or token struck from a logo or a photo: it turns, catches the light and flips over.
- [device](/docs/effects/device.md): A screenshot or a video shown in a 3D phone, tablet, laptop or browser window.
- [explode](/docs/effects/explode.md): An exploded view of a model: its parts move apart as the section scrolls.
- [label](/docs/effects/label.md): A flat label wrapped around a can, a bottle or a jar that turns to show all of it.
- [model](/docs/effects/model.md): A glTF model fitted into the element, lit, turnable and recolored from markup.
- [panorama](/docs/effects/panorama.md): A 360° photo in a box that turns by drag, keys and gyroscope, with hotspots.
- [3D models](/docs/guides/models.md)
- [Reduced motion](/docs/guides/reduced-motion.md)
