# sequence

An image sequence scrubbed by scroll, frame by frame, from a poster <img>.

The image becomes a flip book driven by the sticky section it sits in: scrolling down plays the frames forward, scrolling up plays them back, the way product pages take a device apart. Each frame is framed by the object-fit and object-position of the <img>. With blend, neighboring frames mix by the fraction of the index, so a short sequence of 24 to 36 frames moves smoothly.

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

A gold pocket watch whose crystal and bow lift away frame by frame while the frame stays pinned and you scroll.

```html title="index.html"
<div class="explode">
  <img class="watch" src="/media/landing/watch/01.avif" alt="A gold pocket watch whose crystal and bow lift away from the case" width="800" height="800" data-dio="sequence" data-dio-frames="/media/landing/watch/{01..36}.avif" />
</div>
```

## Usage

Put it on: img, the poster frame.

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

```html
<section class="explode" style="height: 400vh"><div class="stage" style="position: sticky; top: 0; height: 100vh"><img src="/seq/0001.webp" alt="The headphones, layer by layer" data-dio="sequence" data-dio-frames="/seq/{0001..0096}.webp" /></div></section>
```

In page transitions: Fades with the page transition; a keyed flight carries the frame shown at the time.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `frames` (rebuilds) | `data-dio-frames` | `string` | `''` | Frame URLs by commas, or a range like /seq/{0001..0096}.webp padded as its first number. |
| `atlas` (rebuilds) | `data-dio-atlas` | `string` | `''` | Atlas pages instead of frames, a range or list; cells read left to right, top to bottom. |
| `grid` (rebuilds) | `data-dio-grid` | `[number, number]` 1 to 16 | `[1, 1]` | Columns and rows of cells on an atlas page. |
| `count` (rebuilds) | `data-dio-count` | `number \| 'auto'` 1 to 2000 | `'auto'` | Number of frames; auto takes all of the range, or every cell of every atlas page. |
| `by` (rebuilds) | `data-dio-by` | `'pin' \| 'progress' \| 'manual'` | `'pin'` | What drives the frame: pin of the sticky section, viewport progress, or frame alone. |
| `start` | `data-dio-start` | `number` 0 to 1 | `0` | Progress at which the first frame shows. |
| `end` | `data-dio-end` | `number` 0 to 1 | `1` | Progress at which the last frame shows. |
| `frame` | `data-dio-frame` | `number` 0 to 100 | `0` | Frame shown when by is manual, for a tween or a slider; with blend a fraction mixes two. |
| `blend` | `data-dio-blend` | `boolean` | `false` | Mix neighboring frames by the fraction of the index. |
| `ease` | `data-dio-ease` | `number` 0 to 1 s | `0` | Smoothing of the index behind scroll; 0 follows scroll at once (Lenis already smooths it). |
| `cache` | `data-dio-cache` | `number` 1 to 32 | `6` | Decoded frames kept around the current one, two of them ahead in the scroll direction. At most low 2, medium 4. |
| `density` (rebuilds) | `data-dio-density` | `number` 0.1 to 1 | `1` | Share of the frames loaded: 0.5 loads every second one and the others show the nearest. At most low 0.5. |
| `resolution` (rebuilds) | `data-dio-resolution` | `number` 320 to 3840 px | `1920` | Longest side in px a frame is decoded at; larger frames shrink while decoding. 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). Draws only while something on it moves, then stops. Scroll draws its own frames; a frame that finishes decoding asks for one, and ease keeps drawing until the index settles. 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 <img> shows its poster and not a single frame is downloaded. The poster is what people with reduced motion and without WebGL see, so choose it on purpose; another still frame for them is a <picture> with <source media="(prefers-reduced-motion: reduce)">.

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

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

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

## Cost and quality

Loads 3.8 KB the first time a page uses it. GPU: Two texture reads per pixel of the box; one upload of the frame per drawn frame at most. Memory: The compressed bytes of every frame it loads, cache decoded frames on the CPU and one texture of a frame on the GPU (two with blend). An atlas holds its pages instead, .webp, .avif or .ktx2 images, and compressed .ktx2 pages take 4 to 8 times less GPU memory. Under Save-Data or on 2G it loads a quarter of the frames at up to 960 px.

- medium: Up to 4 decoded frames, decoded at up to 1440 px.
- low: Every second frame, up to 2 decoded frames at up to 960 px.

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

## Recipes

### Take a product apart on scroll

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

```html
<section
  class="explode"
  data-dio-section
  style="height: 400vh"
>
  <div
    class="stage"
    style="position: sticky; top: 0; height: 100vh"
  >
    <img
      src="/seq/watch/01.avif"
      data-dio="sequence"
      data-dio-frames="/seq/watch/{01..72}.avif"
      data-dio-end="0.85"
    />
    <h2
      data-dio="text-reveal"
      data-dio-trigger="scroll"
    >Eleven parts, one hand.</h2>
  </div>
</section>
```

The `<img>` is the poster: it shows before the first frame decodes, without WebGL and with reduced motion. Make it the frame that tells the story on its own.

### Pack a short sequence into an atlas

Eighteen frames in two pages of 3 × 3 cost two requests instead of eighteen. A sparse set like this steps from frame to frame, and `blend` mixes neighbors by the fraction of the index, so it still moves smoothly. Here `progress` drives it, with no sticky stage: the watch opens while the frame crosses the middle of the viewport.

The pocket watch from an atlas of eighteen frames, blended, opening as the frame crosses the middle of the viewport.

```html title="index.html"
<div class="explode">
  <img class="watch" src="/media/landing/watch/01.avif" alt="A gold pocket watch whose crystal and bow lift away from the case" width="800" height="800" data-dio="sequence" data-dio-atlas="/media/landing/watch-atlas-{0..1}.avif" data-dio-grid="3 3" data-dio-blend="true" data-dio-by="progress" data-dio-start="0.25" data-dio-end="0.75" />
</div>
```

Atlas pages can be `.ktx2` too. Compressed pages take 4 to 8 times less GPU memory than `.avif` or `.webp`, which matters for long sequences on phones.

### Drive the frame from a tween

With `by` set to `manual` the scroll leaves it alone and `frame` picks the frame. Tween it with GSAP, tie it to a range input, or play it on a button.

```ts
const watch = app.effects.get(
  document.querySelector('.watch'),
  'sequence',
)
gsap.to(watch.params, {
  frame: 35,
  duration: 2.4,
  ease: 'power2.inOut',
})
```

## Notes

Each frame decodes off the main thread and only a window of `cache` frames stays decoded around the current one, two of them ahead in the scroll direction. A fast fling skips frames rather than stalling, and the nearest decoded frame shows meanwhile. On the `low` tier it loads every second frame, and under Save-Data or on 2G a quarter of them at up to 960 px.

Without `data-dio-section`, the section is the parent of the closest sticky ancestor. Add the attribute when the markup differs, for example when ScrollTrigger pins the stage, and on the section of any sticky stage whose copies should hold still while it is pinned. A pinned stage holds the canvas still during native scroll only when it fills half the viewport or more.

The sequence is decoration over a poster, so it needs no controls of its own. Put what the frames show into the `alt` of the `<img>` or into the text next to it: a screen reader reads that, never the frames.

## Related

- [curtain](/docs/effects/curtain.md): A cover picture opens from behind a curtain as you scroll: strips slide away, halves part or a liquid edge drains past.
- [expand](/docs/effects/expand.md): The image of a card grows to fill its sticky stage as the section scrolls.
- [horizontal](/docs/effects/horizontal.md): A row of cards on a pinned stage slides sideways as the page scrolls down, its pictures bending with the speed.
- [zoom-scroll](/docs/effects/zoom-scroll.md): A picture in a pinned section zooms from its box to fill the screen as you scroll.
- [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.
- [How a layer follows scroll](/docs/concepts/scroll.md)
- [Performance and quality](/docs/guides/performance.md)
- [GSAP and Lenis](/docs/guides/gsap-lenis.md)
