expand
The image of a card grows to fill its sticky stage as the section scrolls.
Next project Sea Steps
Live preview needs WebGPU or WebGL 2. 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.
This example failed to draw.
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. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add expand<div class="next"> <figure class="card"> <img src="/media/portfolio/sea-steps-cover.avif" width="1600" height="1067" data-dio="expand" /> </figure> <p class="label"><span>Next project</span> Sea Steps</p></div>import { start } from '@dioramajs/diorama'
const app = start()app.attach('img', 'expand', { by: 'pin', position: 0, start: 0, end: 0.5, out: 1, zoom: 1.1, radius: 0, ease: 0,})import { start } from '@dioramajs/diorama'import expand from '@dioramajs/media/expand'
const app = start()app.attach('img', expand({ by: 'pin', position: 0, start: 0, end: 0.5, out: 1, zoom: 1.1, radius: 0, ease: 0,}))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. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
intorebuildsdata-dio-into | string | '' | Selector of the ancestor to grow into; empty for the nearest sticky one, or the viewport. |
byrebuildsdata-dio-by | 'pin' | 'progress' | 'manual' | 'pin' | What drives it: pin of the sticky section, viewport progress, or position alone. |
positiondata-dio-position | number0 to 1 | 0 | Expansion by hand, for a button or a GSAP tween; with pin or progress it is a floor. |
startdata-dio-start | number0 to 1 | 0 | Pin at which it starts to grow. |
enddata-dio-end | number0 to 1 | 0.5 | Pin at which it fills the stage. |
outdata-dio-out | number0 to 1 | 1 | Pin from which it folds back into the card; 1 holds it to the end. |
zoomdata-dio-zoom | number0 to 1.5 | 1.1 | How much the content swells halfway, back to 1 at both ends; 1 or 0 for none. |
radiusdata-dio-radius | number0 to 64 px | 0 | Corner radius once it fills the stage. |
easedata-dio-ease | number0 to 1 s | 0 | Smoothing behind scroll; 0 follows it at once (Lenis already smooths it). |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads delta
. Draws only while something on it moves, then stops. Scroll draws its own frames, ease keeps drawing until it settles. None at rest. How a frame is decided.
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.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { expand: { reducedMotion: 'static' } } })Cost and quality
- Download
- 1.0 KB min+gzip 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".
- Frames
- Idle at rest
It runs from the low tier up. Every tier draws it the same way. The quality plugin picks the tier for each device.
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.
<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.
<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.
Live preview needs WebGPU or WebGL 2. The sea steps cover in a card, which a button opens to the whole frame and closes again.
This example failed to draw.
<div class="cover"> <div class="frame"> <figure class="card"> <img src="/media/portfolio/sea-steps-cover.avif" 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>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.
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.