# box

A product box built from pictures of its faces: turn it, tilt it, open its lid.

At rest the front of the box lies exactly where the image was, with a soft shadow under it. Under the pointer the box turns to show its right side and leans toward the pointer; a drag turns it all the way round and tips it to show the top and the bottom. With a motion set, the box also opens on hover: the top flap of a folding carton swings back, the lid of a gift box lifts off, or the tray of a sleeve box slides out, and the box leans forward so the inside shows.

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

A green tea carton with rolling hills on its front, turned a little to show its printed side; under the pointer it turns further and leans toward the pointer.

```html title="index.html"
<img class="tea" src="/media/boxes/tea-front.webp" alt="Hillside Tea, sencha green tea, 20 bags" width="600" height="800" data-dio="box" data-dio-right="/media/boxes/tea-side.webp" data-dio-top="/media/boxes/tea-top.webp" data-dio-back="/media/boxes/tea-back.webp" data-dio-yaw="20" tabindex="0" style="touch-action: pan-y" />
```

## Usage

Put it on: img: the front of the box; its aspect is the front of the box, and without WebGL the plain front image shows.

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

```html
<img src="/shop/tea-front.webp" alt="Morning Hills green tea, 20 bags" width="600" height="800" data-dio="box" data-dio-right="/shop/tea-side.webp" />
```

In page transitions: Leaves by dithering its pixels away while shrinking to 96%, and its shadow fades with it.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `motion` (rebuilds) | `data-dio-motion` | `'none' \| 'flap' \| 'lift' \| 'drawer'` | `'none'` | How the box opens on hover, click or Enter. none stays closed; flap swings the top back on a hinge, as on a folding carton of tea or cosmetics; lift raises a lid over the top fifth, as on a rigid gift box; drawer slides a tray out of a sleeve to the right, as on a box of chocolates or a phone. |
| `back` (rebuilds) | `data-dio-back` | `string` | `''` | Back face image URL or color; empty prints the front on the back too. |
| `left` (rebuilds) | `data-dio-left` | `string` | `''` | Left side image URL or color; empty takes the right side picture, else the color of the left edge of the front. |
| `right` (rebuilds) | `data-dio-right` | `string` | `''` | Right side image URL or color; empty takes the left side picture, else the color of the right edge of the front. |
| `top` (rebuilds) | `data-dio-top` | `string` | `''` | Top face image URL or color, its top edge toward the back; empty takes the bottom picture, else the color of the top edge of the front. |
| `bottom` (rebuilds) | `data-dio-bottom` | `string` | `''` | Bottom face image URL or color; empty takes the top picture, else the color of the bottom edge of the front. |
| `inside` (rebuilds) | `data-dio-inside` | `string` | `''` | Color of the bare board inside an opening box; empty is an off-white board. |
| `depth` (rebuilds) | `data-dio-depth` | `number \| 'auto'` 0 to 2 | `'auto'` | Depth as a share of the front width, so an 18 × 24 × 6 cm box has 0.33, and 0 a flat card; auto takes it from the side picture, else the top picture, else 0.3. |
| `yaw` | `data-dio-yaw` | `number` -180 to 180° | `0` | Rest turn; positive shows the right side. |
| `pitch` | `data-dio-pitch` | `number` -80 to 80° | `0` | Rest tilt; positive shows the top. |
| `swing` | `data-dio-swing` | `number` -90 to 90° | `30` | Turn under the pointer or keyboard focus: 0 keeps it facing the viewer. |
| `open` | `data-dio-open` | `number` 0 to 1 | `0.6` | Share of the full opening shown on hover; a click or Enter opens it all the way. 0 keeps it shut. |
| `tilt` | `data-dio-tilt` | `number` 0 to 25° | `5.7` | How far it leans toward the pointer: 0 turns the lean off. |
| `spin` | `data-dio-spin` | `number` -180 to 180 deg/s | `0` | Idle turn while nobody touches the box, pausing under the pointer and for 1.5 s after: 0 turns it off. |
| `drag` | `data-dio-drag` | `boolean` | `true` | Turns and tips it under a drag, and with the arrow keys when it has a tabindex. |
| `shadow` | `data-dio-shadow` | `number` 0 to 1 | `0.5` | Density of the shadow on the page. |

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

### Variants

- `motion`: `none`, `flap`, `lift`, `drawer`
  - `flap` swings the top back on a hinge, as on a folding carton of tea or cosmetics.
  - `lift` raises a lid over the top fifth, as on a rigid gift box.
  - `drawer` slides a tray out of a sleeve to the right, as on a box of chocolates or a phone.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`presence`](/docs/api/inputs#presence), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`width`](/docs/api/inputs#input-width). Draws only while something on it moves, then stops. Only while it turns, leans, opens, coasts after a drag or eases back, and while spin turns it; none at rest, and pointer moves elsewhere draw nothing. 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. It rests as posed: no swing, lean or spin, and hover does not open it. A click or Enter still opens it, at once; dragging and arrow keys still turn it, without coasting.

Runs on every device. Nothing hovers. A tap opens and closes a box with a motion, unless the image sits in a link or a button. A finger turns the box only when the element has `touch-action: pan-y` or `none`, and tips it only with `none`; otherwise the gesture scrolls the page.

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

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

## Cost and quality

Loads 4.9 KB the first time a page uses it. GPU: Six to twenty flat faces with a simple light, no environment map. Memory: The front image, and the pictures of the other faces when given.

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

## Recipes

### A carton from its front and one side

Most packaging is printed from a flat dieline, so the pictures already exist: export each face as its own image. The front image is the front of the box and sets its width and height. A side picture sets the depth by its own proportions: a side 240 px wide next to an 800 px tall front makes a box 0.3 of its height deep. Faces you leave out borrow from their pair, or take the color of the matching edge of the front.

```html
<img
  src="/shop/tea-front.webp"
  width="600"
  height="800"
  data-dio="box"
  data-dio-right="/shop/tea-side.webp"
  data-dio-top="/shop/tea-top.webp"
  data-dio-back="/shop/tea-back.webp"
  data-dio-yaw="20"
/>
```

### A gift box that lifts its lid

`motion="lift"` raises a lid that covers the top fifth of the box. Hover opens it by `open`, a click or Enter all the way. `inside` colors the bare board inside, which shows once the lid is off.

A burgundy gift box with a gold ribbon; under the pointer its lid lifts and tilts back, showing a cream board inside.

```html title="index.html"
<img class="gift" src="/media/boxes/gift-front.webp" alt="The Winter Gift Set: candles, soap and tea" width="900" height="600" data-dio="box" data-dio-motion="lift" data-dio-top="/media/boxes/gift-top.webp" data-dio-inside="#f3e3c3" data-dio-depth="0.6" tabindex="0" style="touch-action: pan-y" />
```

### A sleeve with a tray

`motion="drawer"` slides a tray out of its sleeve to the right, as on a box of chocolates or a phone. `motion="flap"` swings back the top flap of a folding carton instead.

A navy sleeve of pralines; under the pointer a brown tray slides out of its right end.

```html title="index.html"
<img class="pralines" src="/media/boxes/choc-front.webp" alt="Nocturne, twelve dark pralines" width="900" height="450" data-dio="box" data-dio-motion="drawer" data-dio-right="/media/boxes/choc-side.webp" data-dio-inside="#5b3a26" data-dio-yaw="15" tabindex="0" style="touch-action: pan-y" />
```

### Real proportions from the size

Without a side picture `depth` is `auto` and comes out at 0.3 of the width; give it as a share of the front width instead: a box 18 × 24 × 6 cm has a front 18 cm wide, so `depth` is 6 / 18, about 0.33.

```html
<img
  src="/games/harbor-front.webp"
  width="800"
  height="800"
  data-dio="box"
  data-dio-depth="0.25"
  data-dio-spin="18"
/>
```

## Notes

At rest the front is drawn exactly where the image is, so nothing jumps when the engine takes over, and the plain image with its `alt` text stays for screen readers and without WebGL.

The box draws only while it moves: a hover, a drag, the lean, the lid or the idle `spin`. The faces are flat pictures with a simple light, so a box costs its pictures and nothing else to download.

A box inside a link keeps the link’s click; it still opens on hover and turns under a drag. On touch screens a tap opens and closes it, and a finger turns it only when the image has `touch-action: pan-y` or `none`.

`motion` and the face pictures rebuild the effect; every other param changes live.

## Related

- [book](/docs/effects/book.md): A cover image made a 3D book that turns to show its spine and opens on hover.
- [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.
- [panorama](/docs/effects/panorama.md): A 360° photo in a box that turns by drag, keys and gyroscope, with hotspots.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
