# parallax

The image drifts inside its frame with the pointer and the scroll; with a depth map, in 2.5D.

The picture is shown slightly enlarged and moves within its box: away from the pointer while it is over the image, and down through the frame as the page scrolls, centered when the box is centered. With a depth map (a grayscale image of the same frame, white is near) near parts move more than far ones and pass in front of them, so a flat photo turns into a shallow 3D scene. Where depth jumps sharply the far side shows a stretched sliver; a slightly blurred map hides it.

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

Misty ridges at dusk in 2.5D from a photo and a depth map; near ridges move more than far ones as the pointer moves and the page scrolls.

```html title="index.html"
<img src="/media/ridges.jpg" alt="Misty ridges at dusk, one behind another" width="1800" height="1200" data-dio="parallax" data-dio-map="/media/ridges-depth.png" />
```

## Usage

Put it on: img, video.

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

```html
<img src="/hero.avif" alt="Studio at night" data-dio="parallax" />
```

In page transitions: Fades with the page transition. A keyed flight carries the framing it had.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `depth` | `data-dio-depth` | `number` 0 to 0.2 | `0.04` | How far the pointer moves the picture, as a share of the box. |
| `scroll` | `data-dio-scroll` | `number` 0 to 0.5 | `0.12` | How far the picture travels while the box crosses the viewport. |
| `ease` | `data-dio-ease` | `number` 0 to 1 s | `0.12` | How softly the picture follows the pointer. |
| `map` (rebuilds) | `data-dio-map` | `string` | `''` | URL of a depth map for the same frame, white near; empty for a flat shift. |
| `focus` | `data-dio-focus` | `number` 0 to 1 | `0.5` | Depth that stays put, 0 far to 1 near; nearer parts move one way, farther the other. |

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

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`over`](/docs/api/inputs#input-over), [`pointer`](/docs/api/inputs#pointer), [`progress`](/docs/api/inputs#progress). Draws only while something on it moves, then stops. While the pointer moves over it and the motion settles, and while scrolling; none at rest. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it is off and the element shows as plain HTML. Off: the image is shown as it is, and the engine does not load for it.

Runs on every device. The pointer part is off on devices without hover, so taps do not jolt the picture; the scroll part works.

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

start({ effects: { parallax: { reducedMotion: 'static' } } })
```

## Cost and quality

Loads 1.1 KB the first time a page uses it. GPU: One texture read per pixel; with a depth map 7 to 16 reads of the map, by quality tier. Memory: The shared texture of the image, plus the depth map. A map half the size of the image, as a grayscale PNG, is enough.

- low: Depth search in 3 layers, refined 3 times.
- medium: Depth search in 6 layers, refined 4 times.
- high: Depth search in 10 layers, refined 5 times.

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

## Recipes

### A depth map from the photo

`map` is a grayscale image of the same frame, white where things are near. The map of the ridges above comes from the photo itself, since haze makes far ridges lighter: inverted, blurred and saved at half the size, it weighs 70 KB. A depth estimation model gives one for any photo.

```html
<img src="/img/ridges.jpg" data-dio="parallax" data-dio-map="/img/ridges-depth.png" />
```

`focus` picks the depth that stays put. At 0.5 the middle ridges hold still while the near ones move one way and the far ones the other. At 0 the sky holds and everything in front of it moves.

### Without a map

Without `map` the whole picture moves as one layer: it drifts away from the pointer and travels down through its frame as the page scrolls. It needs nothing but the photo.

A photo of a cliff above the sea that drifts inside its frame with the pointer and the scroll.

```html title="index.html"
<img src="/media/portfolio/outer-coast.webp" alt="A cliff edge above a grey sea under scattered clouds" width="1600" height="1067" data-dio="parallax" />
```

### A headline over a moving hero

The canvas draws the copy above the page, so text laid over the image needs a `z-index` above it. Keep `depth` low on a hero with text, so the picture moves under the words without pulling the eye from them.

A hero image of a wave on sea steps that drifts behind a white headline.

```html title="index.html"
<header class="hero">
  <img src="/media/portfolio/sea-steps.webp" alt="A wave breaking over concrete steps into the sea" width="1600" height="1067" data-dio="parallax" data-dio-depth="0.025" />
  <div class="hero-copy">
    <p>Sea Steps, 2026</p>
    <h2>Twelve tides on one concrete stair</h2>
  </div>
</header>
```

```css
.hero-copy {
  position: relative;
  z-index: 2;
}
```

### Scroll only

With `depth` at 0 the pointer does nothing and the picture only travels with the scroll. Use it where the pointer is busy with something else, such as a card that is also a link, or to make the motion the same on touch screens and desktops.

```html
<img
  src="/img/canyon.avif"
  data-dio="parallax"
  data-dio-depth="0"
  data-dio-scroll="0.2"
/>
```

## Notes

The picture is drawn slightly larger than its box so the moving edge never shows, which crops a little of it. Keep faces and text away from the edges of a photo you give it.

A depth map needs no special tool: any grayscale image of the same frame works, white near and black far. A map from a depth estimation model, a render pass of a 3D scene or a hand-painted mask all do. Where the depth jumps sharply, the far side shows a stretched sliver; blur the map by a few pixels to hide it.

## 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.
- [pixelate](/docs/effects/pixelate.md): The image resolves from big pixels to sharp as it scrolls into view, or breaks into pixels on hover.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [How a layer follows scroll](/docs/concepts/scroll.md)
