# panorama

A 360° photo in a box that turns by drag, keys and gyroscope, with hotspots.

The box shows an equirectangular 360° × 180° photo in perspective with the vertical field of view fov. Dragging turns it with inertia, arrows turn it by steps, the wheel with Ctrl or a pinch zooms, and a phone can steer it by tilt. Hotspots are rings on the sphere: hover lights them, a click follows their link, and focus on a link turns the view to it.

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

A wood-paneled hotel lounge in 360°, with four hotspots on the sofa, the fireplace wall, the window and the billiard table.

```html title="index.html"
<div class="lounge" data-dio="panorama" data-dio-src="/media/hotel/pano/lounge-2k.webp" data-dio-preview="/media/hotel/pano/lounge-1k.webp" data-dio-yaw="-20" data-dio-pitch="-6" tabindex="0" role="application" aria-roledescription="360° view" aria-label="A wood-paneled hotel lounge, 360° view. Drag or use the arrow keys to look around, + and - to zoom." style="touch-action: pan-y">
  <img src="/media/hotel/pano/lounge.avif" alt="" width="1200" height="675" />
  <button type="button" data-dio-yaw="-22" data-dio-pitch="-12">Corner sofa</button>
  <button type="button" data-dio-yaw="22" data-dio-pitch="4">Fireplace wall</button>
  <button type="button" data-dio-yaw="50" data-dio-pitch="0">Window to the veranda</button>
  <button type="button" data-dio-yaw="121" data-dio-pitch="-12">Billiard table</button>
</div>
```

## Usage

Put it on: an element with a poster img.

`start()` registers `panorama`, so the attribute alone puts it on an element.

```html
<div class="tour" data-dio="panorama" data-dio-src="/pano/suite.ktx2" data-dio-preview="/pano/suite-1k.webp" data-dio-yaw="40" data-dio-spin="3" tabindex="0" style="touch-action: pan-y"><img src="/img/suite.avif" alt="Garden suite, 360° view" /></div><ul class="tour-legend"><li data-dio-yaw="-95" data-dio-pitch="-8"><a href="/rooms/veranda">Veranda</a></li><li data-dio-yaw="160" data-dio-pitch="-5"><a href="/rooms/lounge">Wooden lounge</a></li></ul>
```

- `data-dio-yaw`: Yaw of a hotspot in degrees.

- `data-dio-pitch`: Pitch of a hotspot in degrees, 0 by default.

In page transitions: Fades with the page transition; a keyed flight carries the panorama.

- Event `dio:marker`, detail `{ marker: Element | null; id: string | null }`: A hotspot was hovered or left; marker is its element, id its id.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `src` (rebuilds) | `data-dio-src` | `string` | `''` | Equirectangular 2:1 panorama: jpg, webp, avif or ktx2. |
| `preview` (rebuilds) | `data-dio-preview` | `string` | `''` | Small panorama of 1 to 2K shown first. |
| `yaw` | `data-dio-yaw` | `number` -180 to 180° | `0` | View direction; a change turns smoothly. |
| `pitch` | `data-dio-pitch` | `number` -85 to 85° | `0` | View tilt up or down. |
| `fov` | `data-dio-fov` | `number` 20 to 120° | `75` | Vertical field of view. |
| `range` | `data-dio-range` | `[number, number]` 20 to 120° | `[35, 100]` | Zoom limits of the field of view. |
| `spin` | `data-dio-spin` | `number` -30 to 30 deg/s | `0` | Turning by itself in degrees per second, 0 for none. |
| `drag` | `data-dio-drag` | `boolean` | `true` | Dragging turns the view. |
| `zoom` | `data-dio-zoom` | `boolean` | `true` | Ctrl or ⌘ with the wheel, a pinch and the + and - keys zoom within range; off keeps fov, and Ctrl with the wheel zooms the page as usual. |
| `gyro` | `data-dio-gyro` | `boolean` | `false` | Phone tilt steers the view; iOS asks for permission on the first tap. |
| `markers` (rebuilds) | `data-dio-markers` | `string` | `'[data-dio-yaw]'` | Selector of hotspots: inside the element, else in the document. |
| `marker` | `data-dio-marker` | `color` | `'#ffffff'` | Color of the hotspots. |
| `markerSize` | `data-dio-marker-size` | `number` 8 to 64 px | `28` | Diameter of a hotspot. |
| `fps` | `data-dio-fps` | `number` 1 to 60 | `30` | Frame cap while turning by itself. At most low 20. |
| `resolution` (rebuilds) | `data-dio-resolution` | `number` 1024 to 8192 px | `4096` | Cap on the long side when decoding a JPEG, PNG or AVIF panorama. A KTX2 file is used as it is, up to the device limit: the way to 8K without a long stall. At most low 2048, medium 4096. |

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

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-width). Draws at a steady rate while it is on screen and stops when it leaves. None at rest; full rate while dragging or coasting, at most fps while turning by itself. 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. No spin and no inertia; turning to a hotspot is instant.

Runs on every device. With touch-action: pan-y a drag turns only sideways and vertical swipes scroll the page; with none, both, and a pinch zooms.

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

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

## Cost and quality

Loads 7.9 KB the first time a page uses it. GPU: One quad and one texture read with gradients; hotspots as instances. Memory: An 8192 × 4096 picture is about 179 MB with mipmaps; serve 8K as KTX2 for about 45 MB.

- low: Decodes at most 2048 px and turns by itself at 20 fps.
- medium: Decodes at most 4096 px.

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

## Recipes

### A room tour with a visible legend

Hotspots outside the panorama make a legend people can read: hovering an item lights its ring, focusing it turns the view there, and `dio:marker` fires as either happens. Here `markers` names the list, so the panorama ignores every other `data-dio-yaw` on the page.

The hotel lounge in 360° over a list of five places in the room; each item turns the view to its spot.

```html title="index.html"
<div class="tour">
  <div class="lounge" data-dio="panorama" data-dio-src="/media/hotel/pano/lounge-2k.webp" data-dio-preview="/media/hotel/pano/lounge-1k.webp" data-dio-markers="#lounge-spots li" data-dio-yaw="-20" data-dio-pitch="-6" tabindex="0" role="application" aria-roledescription="360° view" aria-label="A wood-paneled hotel lounge, 360° view. Drag or use the arrow keys to look around." style="touch-action: pan-y">
    <img src="/media/hotel/pano/lounge.avif" alt="" width="1200" height="675" />
  </div>
  <ul id="lounge-spots" class="spots" aria-label="In this room">
    <li data-dio-yaw="-22" data-dio-pitch="-12"><button type="button">Corner sofa</button></li>
    <li data-dio-yaw="22" data-dio-pitch="4"><button type="button">Fireplace wall</button></li>
    <li data-dio-yaw="-122" data-dio-pitch="4"><button type="button">Cottonwood print</button></li>
    <li data-dio-yaw="121" data-dio-pitch="-12"><button type="button">Billiard table</button></li>
    <li data-dio-yaw="139" data-dio-pitch="0"><button type="button">Hall to the garden room</button></li>
  </ul>
</div>
```

On a real tour each item is a link to the next room. A click on a ring follows the link inside its item, and the keyboard reaches the same links in the list.

```html
<ul class="tour-legend" aria-label="Where to go next">
  <li data-dio-yaw="50" data-dio-pitch="0">
    <a href="/rooms/veranda">Through the window to the veranda</a>
  </li>
  <li data-dio-yaw="139" data-dio-pitch="-6">
    <a href="/rooms/garden">Down the hall to the garden room</a>
  </li>
</ul>
```

### Show a preview first, then the full picture

`preview` takes a small panorama of 1 to 2K that shows within a second; the full `src` replaces it with a cross-fade when it has decoded. JPEG, WebP and AVIF decode at up to `resolution`, 4096 px by default. For 8K, serve KTX2: it uploads as it is, without a long decode, and takes a quarter of the GPU memory.

```html
<div
  class="tour"
  data-dio="panorama"
  data-dio-src="/pano/suite-8k.ktx2"
  data-dio-preview="/pano/suite-1k.webp"
  tabindex="0"
  role="application"
  aria-roledescription="360° view"
  aria-label="Garden suite, 360° view"
  style="touch-action: pan-y"
>
  <img src="/img/suite.avif" />
</div>
```

### Steer it by tilting the phone

With `gyro` on, a phone looks around as the visitor turns it. iOS asks for permission on the first tap, so say in the label that a tap starts it.

```html
<div
  class="tour"
  data-dio="panorama"
  data-dio-src="/pano/terrace.webp"
  data-dio-gyro="true"
  tabindex="0"
  role="application"
  aria-roledescription="360° view"
  aria-label="The terrace, 360° view. Tap, then tilt your phone to look around."
  style="touch-action: pan-y"
>
  <img src="/img/terrace.avif" />
</div>
```

### A slow turn behind a heading

`spin` turns it by itself in degrees per second until the visitor drags it. With `drag` and `zoom` off, it becomes a moving backdrop that leaves the wheel and the pointer to the page. It turns at most `fps` frames a second, 20 on the `low` tier.

```html
<div
  class="hero"
  data-dio="panorama"
  data-dio-src="/pano/field.webp"
  data-dio-spin="3"
  data-dio-drag="false"
  data-dio-zoom="false"
  aria-hidden="true"
>
  <img src="/img/field.avif" />
</div>
```

## Notes

A 4096 × 2048 picture takes about 45 MB of GPU memory with mipmaps, and 8192 × 4096 about 179 MB. The `low` tier decodes at most 2048 px, which is what this page’s panorama uses: 114 KB as WebP, with a 29 KB preview.

`zoom` takes Ctrl or ⌘ with the wheel, a pinch and the + and - keys. Turn it off and Ctrl with the wheel zooms the page as usual. With `touch-action: pan-y` a finger turns the view sideways and vertical swipes scroll the page; with `none` a finger turns it both ways and a pinch zooms.

A panorama is an application for assistive technology: give the element `role="application"`, `aria-roledescription="360° view"`, a label that says how to use it, and `tabindex="0"` for the keys. Hotspots inside the element are hidden with it but keep their focus; Tab reaches them, the view turns to each, and the effect draws the focus ring on the sphere.

Under reduced motion it does not spin or coast after a drag, and turning to a hotspot is instant.

## 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.
- [hotspots](/docs/effects/hotspots.md): Feature callouts pinned to points of a model, real buttons that follow it as it turns.
- [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.
- [Performance and quality](/docs/guides/performance.md)
- [Loading and preloading](/docs/guides/loading.md)
