panorama
A 360° photo in a box that turns by drag, keys and gyroscope, with hotspots.
Live preview needs WebGPU or WebGL 2. A wood-paneled hotel lounge in 360°, with four hotspots on the sofa, the fireplace wall, the window and the billiard table.
This example failed to draw.
Hover a ring, or press Tab to move between them.
Usage
Put it on an element with a poster img. start() registers panorama, 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 panorama<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" 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>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.lounge', 'panorama', { yaw: -20, pitch: -6, fov: 75, range: [35, 100], spin: 0, drag: true, zoom: true, gyro: false, marker: '#ffffff', markerSize: 28,})import { start } from '@dioramajs/diorama'import panorama from '@dioramajs/media/panorama'
const app = start()app.attach('.lounge', panorama({ yaw: -20, pitch: -6, fov: 75, range: [35, 100], spin: 0, drag: true, zoom: true, gyro: false, marker: '#ffffff', markerSize: 28,}))Markup it reads
data-dio-yaw: Yaw of a hotspot in degrees.data-dio-pitch: Pitch of a hotspot in degrees, 0 by default.
Events
It dispatches these on its element; they bubble.
dio:marker,detail: { marker: Element | null; id: string | null }. A hotspot was hovered or left; marker is its element, id its id.
Page transitions
Fades with the page transition; a keyed flight carries the panorama. 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 | '' | Equirectangular 2:1 panorama: jpg, webp, avif or ktx2. |
previewrebuildsdata-dio-preview | string | '' | Small panorama of 1 to 2K shown first. |
yawdata-dio-yaw | number-180 to 180° | 0 | View direction; a change turns smoothly. |
pitchdata-dio-pitch | number-85 to 85° | 0 | View tilt up or down. |
fovdata-dio-fov | number20 to 120° | 75 | Vertical field of view. |
rangedata-dio-range | [number, number]20 to 120° | [35, 100] | Zoom limits of the field of view. |
spindata-dio-spin | number-30 to 30 deg/s | 0 | Turning by itself in degrees per second, 0 for none. |
dragdata-dio-drag | boolean | true | Dragging turns the view. |
zoomdata-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. |
gyrodata-dio-gyro | boolean | false | Phone tilt steers the view; iOS asks for permission on the first tap. |
markersrebuildsdata-dio-markers | string | '[data-dio-yaw]' | Selector of hotspots: inside the element, else in the document. |
markerdata-dio-marker | color | '#ffffff' | Color of the hotspots. |
markerSizedata-dio-marker-size | number8 to 64 px | 28 | Diameter of a hotspot. |
fpsdata-dio-fps | number1 to 60 | 30 | Frame cap while turning by itself.At most low 20 by quality tier. |
resolutionrebuildsdata-dio-resolution | number1024 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 by quality tier. |
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, opacity, reducedMotion, size and 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. 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. 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.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { panorama: { reducedMotion: 'off' } } })Cost and quality
- Download
- 7.9 KB min+gzip 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.
- Frames
- Draws while on screen
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
low | Decodes at most 2048 px and turns by itself at 20 fps. |
medium | Decodes at most 4096 px. |
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.
Live preview needs WebGPU or WebGL 2. The hotel lounge in 360° over a list of five places in the room; each item turns the view to its spot.
This example failed to draw.
<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" 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.
<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.
<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.
<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.
<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.