# scrub

A video scrubbed by scroll: its time follows the sticky section it plays in, forward and back.

While the section pins its stage, scrolling down plays the video forward and scrolling up plays it back, the way product pages turn a device or take it apart. Every frame the browser decodes crossfades from the one before for as long as the frames take to come, at most blend, so the picture moves smoothly even when seeks lag behind the scroll and stays sharp when they keep up. Frames are framed by the object-fit and object-position of the video. Encoding decides how smooth it is: give the file a keyframe every few frames (ffmpeg -g 5, or -g 1 for every frame) and enough frames for the scroll, 24 to 30 a second, since a few frames stretched over a long scroll step however they blend; keep it short and at most 1080p, with no sound; a file from a phone or an editor has a keyframe every few seconds, and every seek backwards then decodes up to that many frames.

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

A short film of a gold pocket watch whose crystal and bow lift away while the frame stays pinned and you scroll.

```html title="index.html"
<div class="teardown">
  <video class="watch" src="/media/film/watch.webm" poster="/media/film/watch.webp" width="720" height="720" muted playsinline preload="auto" aria-label="A gold pocket watch whose crystal and bow lift away from the case" data-dio="scrub"></video>
</div>
```

## Usage

Put it on: video, muted, with preload="auto" and a poster of its first frame.

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

```html
<section class="turn" style="height: 400vh"><div class="stage" style="position: sticky; top: 0; height: 100vh"><video src="/film/turn.mp4" poster="/film/turn.webp" muted="" playsinline="" preload="auto" data-dio="scrub"></video></div></section>
```

In page transitions: Fades with the page transition.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `by` (rebuilds) | `data-dio-by` | `'pin' \| 'progress' \| 'manual'` | `'pin'` | What drives the time: pin of the sticky section, viewport progress, or position alone. |
| `start` | `data-dio-start` | `number` 0 to 1 | `0` | Progress at which the scrubbed span begins. |
| `end` | `data-dio-end` | `number` 0 to 1 | `1` | Progress at which the scrubbed span ends. |
| `position` | `data-dio-position` | `number` 0 to 1 | `0` | Place in the span shown when by is manual, for a tween or a slider. |
| `from` | `data-dio-from` | `number` 0 to 60 s | `0` | Time in the video the span starts at. |
| `to` (rebuilds) | `data-dio-to` | `number \| 'auto'` 0 to 60 s | `'auto'` | Time in the video the span ends at, in seconds; auto is the end of the video. |
| `ease` | `data-dio-ease` | `number` 0 to 1 s | `0.1` | Smoothing of the time behind scroll; 0 follows scroll at once (Lenis already smooths it). |
| `blend` | `data-dio-blend` | `number` 0 to 0.5 s | `0.12` | Longest crossfade from one decoded frame to the next; frames that come quicker fade quicker. 0 cuts. |
| `resolution` (rebuilds) | `data-dio-resolution` | `number` 320 to 3840 px | `1920` | Longest side in px a frame is copied at; larger videos shrink on the GPU. At most low 960, medium 1440. |

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

### Variants

- `by`: `pin`, `progress`, `manual`

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`width`](/docs/api/inputs#input-width). Draws only while something on it moves, then stops. Scroll draws its own frames; a decoded frame asks for one, and ease and blend keep drawing until the time and the crossfade settle. None at rest, and no seeks while the video is off screen. 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 video shows its poster and is never seeked. People with reduced motion and without WebGL see the poster, so choose it on purpose.

Runs on every device. Works as on desktop: scroll drives it.

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

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

## Cost and quality

Loads 2.6 KB the first time a page uses it. GPU: Two texture reads per pixel of the box, one frame copy and upload per decoded frame. Memory: The video as the browser buffers it, and two copied frames on the GPU. A 4 second 1080p clip with a keyframe every 5 frames is about 2 to 4 MB as H.264 or VP9.

- medium: Frames copied at up to 1440 px.
- low: Frames copied at up to 960 px.

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

## Recipes

### Turn a product on scroll

The usual page is a tall section with a sticky stage the height of the screen, and the video in the stage. `scrub` follows the pin of that section, so 400vh gives the film three screens of scroll. An `end` below 1 reaches the last frame early and holds it while the reader takes in the heading.

```html
<section class="turn" data-dio-section style="height: 400vh">
  <div class="stage" style="position: sticky; top: 0; height: 100vh">
    <video src="/film/turn.mp4" poster="/film/turn.webp" muted playsinline preload="auto" aria-label="The headphones, turning to show the hinge" data-dio="scrub" data-dio-end="0.85"></video>
  </div>
</section>
```

### Encode the file for scrubbing

A browser seeks by decoding from the keyframe before the wanted time. Files from a phone or an editor have a keyframe every few seconds, so every step back decodes dozens of frames and the picture stops while you scroll. Encode the film with a keyframe every few frames, without sound, at most 1080p and with the index at the start of the file:

```sh
ffmpeg -i turn.mov -an -c:v libx264 -g 5 -crf 20 -pix_fmt yuv420p -movflags +faststart turn.mp4
ffmpeg -i turn.mov -an -c:v libvpx-vp9 -g 5 -crf 34 -b:v 0 turn.webm
```

With `-g 1` every frame is a keyframe: the smoothest scrub at about twice the size. Keep the film short, 3 to 6 seconds is plenty for a turn or a teardown. The film in the example is 4 seconds at 30 frames a second, 720 px, with a keyframe every 5 frames: 660 KB as WebM. A few frames stretched over a long scroll step however they blend, so give the scroll as many frames as it has screens times 30 or so.

The effect asks for the next time only when the last seek is done, so seeks never pile up, and each frame the browser decodes crossfades from the one before for as long as the frames take to come, at most `blend`. A slow seek then shows as a short dissolve instead of a stop, and quick seeks stay sharp.

### Part of a longer film

`from` and `to` pick the seconds that scroll plays through, so one film can serve several sections.

```html
<video src="/film/assembly.mp4" muted playsinline preload="auto" data-dio="scrub" data-dio-from="2" data-dio-to="6"></video>
```

### Driven by your own code

With `by` set to `manual`, `position` from 0 to 1 picks the moment, for a slider or a GSAP tween.

```html
<video class="film" src="/film/assembly.mp4" muted playsinline preload="auto" data-dio="scrub" data-dio-by="manual"></video>
<input class="at" type="range" min="0" max="1" step="0.001" value="0" aria-label="Assembly step" />
```

```ts
const film = app.effects.get(document.querySelector('.film'), 'scrub')
document.querySelector('.at').addEventListener('input', (e) => {
  film?.set({ position: Number(e.target.value) })
})
```

## Notes

`scrub` or `sequence`: a video is one file that the browser streams and decodes on the GPU, so it is smaller and quicker to start than a hundred images. A `sequence` of images steps without any seek and suits short loops and atlases. Pick `scrub` when the film is longer than a few dozen frames.

The video never plays: the effect pauses it whenever something starts it. It needs `muted` and `playsinline` like any inline video, and `preload="auto"`, which the effect sets if it is missing.

The poster is what people with reduced motion and without WebGL see, so make it the first frame or the one that tells the most.

A video from another origin needs `crossorigin="anonymous"` and a server that sends `Access-Control-Allow-Origin`, or the effect stops with [`EFFECT_CORS`](/docs/errors/effect-cors).

`resolution` rebuilds the effect when it changes. Set it in the markup.

## Related

- [ambient](/docs/effects/ambient.md): A soft glow in the colors of the frame around a video or an image, like ambient mode.
- [chroma](/docs/effects/chroma.md): A video shot on a green or blue screen, played over the page with the screen keyed out.
- [video-hover](/docs/effects/video-hover.md): A thumbnail plays a short muted video preview while the pointer rests on it or its link has keyboard focus.
- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [bend](/docs/effects/bend.md): Scroll speed bends the image like paper and splits its color channels.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [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.
- [How a layer follows scroll](/docs/concepts/scroll.md)
- [Performance and quality](/docs/guides/performance.md)
- [GSAP and Lenis](/docs/guides/gsap-lenis.md)
