# tilt

A card image tilts toward the pointer in its own perspective, with glare and foil.

The image tilts toward the pointer up to tilt degrees with its own perspective, as CSS perspective on the card would, lifts toward the viewer and catches a glare. With foil a rainbow holographic sheen shifts with the viewing angle inside the mask. A soft shadow lies under it, and leaving lets it settle flat.

Module `media`, category Photo. Import `@dioramajs/media/tilt`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/media/src/tilt.ts)

A dark green gift card with gold lines that tilts toward the pointer, lifts and catches a glare, with gold foil on its lines when foil is raised.

```html title="index.html"
<img class="gift-card" src="/media/shop/gift-card.webp" alt="A gift card in dark green with fine gold lines and a gold star" width="1040" height="656" data-dio="tilt" data-dio-mask="/media/shop/gift-foil.png" />
```

## Usage

Put it on: img, video.

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

```html
<img src="/cards/0427.png" alt="Card No. 0427" data-dio="tilt" data-dio-foil="0.8" data-dio-gyro="true" />
```

In page transitions: Fades with the page transition; a flight carries the flat copy.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `tilt` | `data-dio-tilt` | `number` 0 to 45° | `12` | Largest tilt. |
| `lift` | `data-dio-lift` | `number` 0 to 100 px | `20` | Rise toward the viewer while hovered. |
| `perspective` | `data-dio-perspective` | `number` 200 to 3000 px | `800` | Perspective distance of the card. |
| `ease` | `data-dio-ease` | `number` 0 to 1 s | `0.12` | Smoothing of the tilt. |
| `glare` | `data-dio-glare` | `number` 0 to 1 | `0.25` | Strength of the glare. |
| `foil` | `data-dio-foil` | `number` 0 to 1 | `0` | Strength of the holographic foil. |
| `mask` (rebuilds) | `data-dio-mask` | `string` | `''` | URL of the foil mask, white where the foil shows. |
| `shadow` | `data-dio-shadow` | `number` 0 to 1 | `0.35` | Density of the shadow under the card. |
| `reverse` | `data-dio-reverse` | `boolean` | `false` | Tilts away from the pointer. |
| `gyro` | `data-dio-gyro` | `boolean` | `false` | Tilting the phone leans the card where no pointer does, against the angle the phone is held at, which it follows over a few seconds; iOS asks for permission on the first tap. |

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), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. Only while the card tilts or settles; none at rest, and pointer moves outside the card 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. No tilt or lift; glare and foil still follow the pointer.

Runs on every device. The card tilts toward the finger while it is down; scrolling is never blocked. With gyro the phone tilt leans it, and glare and foil follow.

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

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

## Cost and quality

Loads 3.4 KB the first time a page uses it. GPU: A 16 × 16 surface, one noise read for the foil and a shadow plane. Memory: The image and the optional foil mask.

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

## Recipes

### A festival pass with foil

Foil needs a mask: a grayscale image of the card, white where the foil is. Here the mask covers the title, the rings and the stripe, so the rest of the pass stays matte. The pass is a link to the tickets page, and its focus ring stays on the link in the page.

A blue weekend pass whose title, rings and stripe shift through rainbow colors as it tilts.

```html title="index.html"
<a class="pass" href="/docs/effects/tilt">
  <img src="/media/festival/pass.webp" alt="Blue Signal weekend pass No. 0427, 23 to 25 July 2027" width="1080" height="680" data-dio="tilt" data-dio-foil="0.7" data-dio-mask="/media/festival/pass-foil.png" />
</a>
```

```ts title="script.ts"
// The link of the example goes nowhere: a click would only jump the page.
export default function mount(example: HTMLElement): () => void {
  const link = example.querySelector('.pass')
  const stop = (e: Event) => e.preventDefault()
  link?.addEventListener('click', stop)
  return () => link?.removeEventListener('click', stop)
}
```

### Covers in a grid

Album, book and episode covers are pictures with the text already in them, so they tilt as one piece. Lower `tilt`, `lift` and `glare` for a grid, where several covers sit side by side and a strong tilt would cover the neighbors.

Three square podcast covers in a row, each leaning slightly toward the pointer.

```html title="index.html"
<ol class="episodes">
  <li><img src="/media/festival/notes-01.webp" alt="Signal Notes 01, T-minus" width="480" height="480" data-dio="tilt" data-dio-tilt="8" data-dio-lift="12" data-dio-glare="0.15" /></li>
  <li><img src="/media/festival/notes-02.webp" alt="Signal Notes 02, The Eagle Has Landed" width="480" height="480" data-dio="tilt" data-dio-tilt="8" data-dio-lift="12" data-dio-glare="0.15" /></li>
  <li><img src="/media/festival/notes-03.webp" alt="Signal Notes 03, We've Had a Problem" width="480" height="480" data-dio="tilt" data-dio-tilt="8" data-dio-lift="12" data-dio-glare="0.15" /></li>
</ol>
```

### Foil that follows the phone

A phone has no pointer to hover, so turn on `gyro`: tilting the phone leans the card and moves the foil. The card follows the change from the angle you hold the phone at and settles flat after a few seconds of holding still. iOS asks for permission on the first tap on the page.

```html
<img
  src="/cards/0427.png"
  data-dio="tilt"
  data-dio-foil="0.8"
  data-dio-mask="/cards/0427-foil.png"
  data-dio-gyro="true"
/>
```

## Notes

The engine draws pictures, not HTML: a card whose title and price are HTML over the image stays flat while the image tilts under it. Render the card as one image, as tickets, covers and collectible cards already are, and keep live text next to it.

A tilted card rises above the copies of its neighbors and goes back to their level when it settles.

The link or button around the card keeps its own focus ring. On keyboard focus the card lifts without tilting.

## Related

- [bend](/docs/effects/bend.md): Scroll speed bends the image like paper and splits its color channels.
- [compare](/docs/effects/compare.md): Two pictures in one box, before and after, split by a line you drag, hover or scroll.
- [flare](/docs/effects/flare.md): A lens flare or a glossy light sweep over a photo that follows the pointer or the scroll.
- [frost](/docs/effects/frost.md): Frosted glass over a photo, or over the whole page, that the pointer wipes clear and that slowly fogs back.
- [glitch](/docs/effects/glitch.md): A digital glitch in short bursts: blocks slide sideways, color channels split, scanlines flicker.
- [image](/docs/effects/image.md): Draws the image or video unchanged, as a GL copy that page flights and other effects build on.
- [liquid](/docs/effects/liquid.md): The pointer leaves a fluid trail on a photo that settles back like water.
- [parallax](/docs/effects/parallax.md): The image drifts inside its frame with the pointer and the scroll; with a depth map, in 2.5D.
- [Reduced motion](/docs/guides/reduced-motion.md)
