# expand

The image of a card grows to fill its sticky stage as the section scrolls.

Between start and end of the pin a copy of the image grows from the box of its card to the box of the sticky stage, usually the whole screen, easing in and out. Its corners go to radius, object-fit frames it anew at every size, so cover never stretches, and the content swells a little on the way and lands at its size. It then holds the screen; when the section lets the stage go, it leaves with it. With out below 1 it folds back into the card before the pin ends. Scrolling up plays it all backward.

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

The cover of the next project in a small card, which grows to fill the pinned frame as you scroll and shrinks back as you scroll up.

```html title="index.html"
<div class="next">
  <figure class="card">
    <img src="/media/portfolio/sea-steps-cover.avif" alt="Concrete steps into a grey sea, a wave breaking white behind them" width="1600" height="1067" data-dio="expand" />
  </figure>
  <p class="label"><span>Next project</span> Sea Steps</p>
</div>
```

## Usage

Put it on: img, video inside a card on a sticky stage.

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

```html
<section class="detail" style="height: 250vh"><div class="stage" style="position: sticky; top: 0; height: 100vh"><figure class="card" style="overflow: hidden; border-radius: 24px"><img src="/img/cushion.avif" alt="Memory foam cushion" style="border-radius: inherit" data-dio="expand" /></figure></div></section>
```

In page transitions: Fades with the page transition. A keyed flight takes the copy and starts from the box it was drawn at, expanded or not.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `into` (rebuilds) | `data-dio-into` | `string` | `''` | Selector of the ancestor to grow into; empty for the nearest sticky one, or the viewport. |
| `by` (rebuilds) | `data-dio-by` | `'pin' \| 'progress' \| 'manual'` | `'pin'` | What drives it: pin of the sticky section, viewport progress, or position alone. |
| `position` | `data-dio-position` | `number` 0 to 1 | `0` | Expansion by hand, for a button or a GSAP tween; with pin or progress it is a floor. |
| `start` | `data-dio-start` | `number` 0 to 1 | `0` | Pin at which it starts to grow. |
| `end` | `data-dio-end` | `number` 0 to 1 | `0.5` | Pin at which it fills the stage. |
| `out` | `data-dio-out` | `number` 0 to 1 | `1` | Pin from which it folds back into the card; 1 holds it to the end. |
| `zoom` | `data-dio-zoom` | `number` 0 to 1.5 | `1.1` | How much the content swells halfway, back to 1 at both ends; 1 or 0 for none. |
| `radius` | `data-dio-radius` | `number` 0 to 64 px | `0` | Corner radius once it fills the stage. |
| `ease` | `data-dio-ease` | `number` 0 to 1 s | `0` | Smoothing behind scroll; 0 follows it at once (Lenis already smooths it). |

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, ease keeps drawing until it 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 image stays in its card, as the DOM shows it.

Runs on every device. Works from scroll. On iOS without Lenis the full-screen copy can trail composited scroll by a frame as the stage is let go; Lenis avoids it.

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

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

## Cost and quality

Loads 1.0 KB the first time a page uses it. GPU: One texture read per pixel, up to the whole viewport once expanded. Memory: The image the browser picked for the card. For a sharp full screen, let sizes allow it, such as sizes="100vw".

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

## Recipes

### A case study cover that takes over the screen

The section is tall and its stage sticks to the top at the height of the screen. Between `start` and `end` of the pin, the image grows from its card to the stage, and holds the screen until the section lets the stage go. With `sizes="100vw"` the browser picks a source sharp enough for the full screen.

```html
<section
  class="next"
  data-dio-section
  style="height: 250vh"
>
  <div
    class="stage"
    style="position: sticky; top: 0; height: 100vh"
  >
    <figure
      class="card"
      style="overflow: hidden; border-radius: 16px"
    >
      <img
        src="/work/sea-steps/cover-1920.avif"
        srcset="/work/sea-steps/cover-960.avif 960w, /work/sea-steps/cover-1920.avif 1920w"
        sizes="100vw"
        style="border-radius: inherit"
        data-dio="expand"
        data-dio-end="0.55"
      />
    </figure>
  </div>
</section>
```

A card that clips its image clips the copy as a rectangle, so its rounded corners would turn square: give the `<img>` `border-radius: inherit` as here. With a `data-dio-key` and a page transition, the cover flies into the next page from the box it was drawn at, expanded or not.

### Open it, then fold it back

`out` below 1 folds the image back into its card before the pin ends, so a section can show a detail full screen and return to the layout around it. `radius` keeps rounded corners at full size.

```html
<img
  src="/img/dial.avif"
  data-dio="expand"
  data-dio-start="0.1"
  data-dio-end="0.4"
  data-dio-out="0.75"
  data-dio-radius="24"
/>
```

### Open it from a button

With `by` set to `manual` the scroll leaves it alone and `position` sets the expansion, from 0 in the card to 1 at the size of `into`. Tween it from a button for a “view full frame” control. With `pin` or `progress`, `position` is a floor under the scroll instead.

The sea steps cover in a card, which a button opens to the whole frame and closes again.

```html title="index.html"
<div class="cover">
  <div class="frame">
    <figure class="card">
      <img src="/media/portfolio/sea-steps-cover.avif" alt="Concrete steps into a grey sea, a wave breaking white behind them" width="1600" height="1067" data-dio="expand" data-dio-by="manual" data-dio-into=".frame" />
    </figure>
  </div>
  <button type="button" class="open" aria-pressed="false">View full frame</button>
</div>
```

```ts title="script.ts"
import { app } from '../../../../docs/diorama.ts'

// The button tweens `position` from 0 to 1 and back; by="manual" leaves the scroll out of it.
export default function mount(example: HTMLElement): () => void {
  const image = example.querySelector('.cover img')
  const button = example.querySelector<HTMLButtonElement>('.open')
  let raf = 0
  const toggle = () => {
    const expand = image && app.effects.get(image, 'expand')
    if (!expand || !button) return
    const open = button.getAttribute('aria-pressed') !== 'true'
    button.setAttribute('aria-pressed', String(open))
    button.textContent = open ? 'Back to the card' : 'View full frame'
    cancelAnimationFrame(raf)
    const from = expand.params.position
    const to = open ? 1 : 0
    const start = performance.now()
    const step = (now: number) => {
      const t = Math.min(1, (now - start) / 700)
      const eased = t < 0.5 ? 4 * t ** 3 : 1 - (-2 * t + 2) ** 3 / 2
      expand.params.position = from + (to - from) * eased
      if (t < 1) raf = requestAnimationFrame(step)
    }
    raf = requestAnimationFrame(step)
  }
  button?.addEventListener('click', toggle)
  return () => {
    cancelAnimationFrame(raf)
    button?.removeEventListener('click', toggle)
  }
}
```

### Text over the full-screen image

The canvas sits over the page by default, so the expanded copy covers the stage’s own text. For a caption over the image, start Diorama with the canvas behind the page and keep the stage background transparent.

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

start({ canvas: 'behind' })
```

## Notes

The copy grows from the source the browser picked for the card. A card of 400 px with `srcset` and no `sizes` gets a small file, and it looks soft on a full screen. Give it `sizes="100vw"`, or `sizes` that match the stage.

Without `into`, it grows into the closest sticky ancestor, the stage, and without one into the viewport. Pinned stages should fill half the viewport or more: then the canvas holds still while the stage is pinned, and the expanded copy does not trail native scroll. Without Lenis on iOS, the full-screen copy can still trail composited scroll by a frame as the section lets the stage go; Lenis avoids it.

Under reduced motion the image stays in its card, and the page reads as it would without Diorama. Keep anything the expanded image tells in the text around it as well.

## 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.
- [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.
- [sequence](/docs/effects/sequence.md): An image sequence scrubbed by scroll, frame by frame, from a poster <img>.
- [zoom-scroll](/docs/effects/zoom-scroll.md): A picture in a pinned section zooms from its box to fill the screen as you scroll.
- [ambient](/docs/effects/ambient.md): A soft glow in the colors of the frame around a video or an image, like ambient mode.
- [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.
- [How a layer follows scroll](/docs/concepts/scroll.md)
- [How a flight flies](/docs/concepts/flights.md)
- [GSAP and Lenis](/docs/guides/gsap-lenis.md)
