Image Split Cards
One image laid over a row of cards. Pinned while you scroll, it splits along the gaps, then each piece turns over to show its card, one after the other. Scroll back and the picture comes back together.
View codeA numbered series of pictures drawn on one canvas as the scroll runs through a section that holds the screen. Captions come in on cue, and the frames load coarse to fine: a fast scroll never shows a blank.
With your AI
Say it to your AI. It gets the exact code below, not its best guess, and knows where it goes.
Add the Kodza Scroll Sequence to my page.
claude mcp add -t http kodza https://kodza.dev/api/mcp
<div class="kodza-scroll-sequence" data-kodza-scroll-sequence data-frames="60" data-src="https://kodza.dev/media/kz-arch-day/{index}.webp">
<div class="kodza-scroll-sequence__stage">
<img class="kodza-scroll-sequence__poster" src="https://kodza.dev/media/kz-arch-day/01.webp" alt="A concrete wall with a single arched opening, its patch of sunlight crossing the ground from morning to evening" width="1440" height="810">
<div class="kodza-scroll-sequence__panel">
<p class="kodza-scroll-sequence__meter" aria-hidden="true">
<span class="kodza-scroll-sequence__counter">01 / 60</span>
<span class="kodza-scroll-sequence__rail"></span>
</p>
<div class="kodza-scroll-sequence__captions">
<div class="kodza-scroll-sequence__caption" data-at="0">
<p class="kodza-scroll-sequence__label">Le Mur — Étretat, 8:10</p>
<h2 class="kodza-scroll-sequence__title">One wall. One hole.</h2>
<p class="kodza-scroll-sequence__text">Poured on the cliff path, facing inland. Nobody moves in this film, and neither does the camera. Only the sun does.</p>
</div>
<div class="kodza-scroll-sequence__caption" data-at="0.36">
<p class="kodza-scroll-sequence__label">Noon — sun 38° up</p>
<h2 class="kodza-scroll-sequence__title">The patch crosses the slab.</h2>
<p class="kodza-scroll-sequence__text">The sun climbs and the light through the arch slides across the ground. A sundial nobody asked for.</p>
</div>
<div class="kodza-scroll-sequence__caption" data-at="0.7">
<p class="kodza-scroll-sequence__label">Evening — sun 14° up</p>
<h2 class="kodza-scroll-sequence__title">Then the wall takes it back.</h2>
<p class="kodza-scroll-sequence__text">Low sun, long shadow. By seven the opening is just a door again, and the scroll gives you the morning back.</p>
</div>
</div>
</div>
</div>
</div>
/* Kodza License: free to use, client work included. Not to resell or redistribute, ported or not: https://kodza.dev/docs/#print-license */
/* Kodza — Scroll Sequence
A numbered series of pictures (a turn around an object, an assembly, a time lapse) drawn on one
canvas, frame by frame, as the scroll runs through the section. The stage holds the screen,
the captions roll in at their own point of the sequence, and a rail with a frame counter says
where you are. No video file: the frames are plain images, loaded coarse to fine.
Without the script, or with reduced motion, the first picture sits above the captions.
Tuned with the --kz-scroll-sequence-* variables; the frames are set on the root
(data-frames, data-src, data-pad), the pacing in the constants at the top of component.js. */
.kodza-scroll-sequence {
/* How much scroll the whole sequence takes, on top of the screen the stage holds */
--kz-scroll-sequence-distance: 300svh;
/* How the frames fill the stage, and which part of them a crop keeps */
--kz-scroll-sequence-fit: cover;
--kz-scroll-sequence-position: 50% 50%;
/* On a tall screen (a phone, a tablet, a narrow window) the frames sit in a box at the bottom,
under the captions: its share of the stage's height (at most a square), and the part of the
frames it keeps */
--kz-scroll-sequence-media-portrait: 56%;
--kz-scroll-sequence-position-portrait: 83% 25%;
/* The room kept around the captions and the rail */
--kz-scroll-sequence-gutter: clamp(20px, 5cqi, 72px);
/* The colour behind the frames: match the sky of your pictures, so a portrait screen reads as
one picture with a tall sky */
--kz-scroll-sequence-sky: #e5e3e1;
--kz-scroll-sequence-ink: var(--kz-color-ink, #0a0a0a);
--kz-scroll-sequence-muted: color-mix(in srgb, var(--kz-scroll-sequence-ink) 66%, transparent);
--kz-scroll-sequence-rail: color-mix(in srgb, var(--kz-scroll-sequence-ink) 18%, transparent);
--kz-scroll-sequence-radius: calc(16px * var(--kz-radius-scale, 1));
--kz-scroll-sequence-mono: var(--kz-font-mono, "Geist Mono", ui-monospace, SFMono-Regular, Menlo, monospace);
position: relative;
display: block;
container-type: inline-size;
color: var(--kz-scroll-sequence-ink);
}
/* At rest (no script, reduced motion): the first picture, the captions under it */
.kodza-scroll-sequence__stage {
box-sizing: border-box;
display: grid;
gap: 32px;
width: min(1080px, 100% - 2 * var(--kz-scroll-sequence-gutter));
margin-inline: auto;
}
.kodza-scroll-sequence__poster {
display: block;
width: 100%;
max-width: none;
height: auto;
margin: 0;
border-radius: var(--kz-scroll-sequence-radius);
background: var(--kz-scroll-sequence-sky);
aspect-ratio: 16 / 9;
object-fit: var(--kz-scroll-sequence-fit);
object-position: var(--kz-scroll-sequence-position);
}
.kodza-scroll-sequence__panel {
display: grid;
gap: 28px;
}
/* The frame counter and the rail: only while the frames follow the scroll */
.kodza-scroll-sequence__meter {
display: none;
grid-template-columns: auto minmax(0, 1fr);
gap: 16px;
align-items: center;
margin: 0;
}
.kodza-scroll-sequence__captions {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(240px, 100%), 1fr));
gap: 28px;
}
.kodza-scroll-sequence__caption {
display: grid;
align-content: start;
gap: 12px;
min-width: 0;
}
.kodza-scroll-sequence__label,
.kodza-scroll-sequence__counter {
margin: 0;
color: var(--kz-scroll-sequence-muted);
font-family: var(--kz-font-label, var(--kz-scroll-sequence-mono));
font-size: var(--kz-font-label-size, 12.5px);
font-weight: var(--kz-font-label-weight, 500);
line-height: var(--kz-font-label-leading, 1.4);
letter-spacing: var(--kz-font-label-tracking, 0.02em);
text-transform: var(--kz-font-label-case, inherit);
}
.kodza-scroll-sequence__title {
margin: 0;
color: inherit;
font-family: var(--kz-font-display, inherit);
font-size: calc(clamp(28px, 3.6cqi, 52px) * var(--kz-font-display-scale, 1));
font-weight: var(--kz-font-display-weight, 600);
line-height: 1.02;
letter-spacing: var(--kz-font-display-tracking, -0.045em);
text-transform: var(--kz-font-display-case, inherit);
text-wrap: balance;
}
/* At rest the captions share a row: a smaller title keeps each to two lines, and the label, title
and text rows line up across the columns */
.kodza-scroll-sequence:not(.is-live) .kodza-scroll-sequence__title {
font-size: calc(clamp(24px, 2.6cqi, 34px) * var(--kz-font-display-scale, 1));
}
.kodza-scroll-sequence:not(.is-live) .kodza-scroll-sequence__caption {
grid-row: span 3;
grid-template-rows: subgrid;
}
.kodza-scroll-sequence__text {
max-width: 30em;
margin: 0;
color: var(--kz-scroll-sequence-muted);
font-size: 16px;
line-height: 1.55;
text-wrap: pretty;
}
/* Running: the section is a track, the stage holds the screen while the scroll runs through it */
.kodza-scroll-sequence.is-live::after {
content: "";
display: block;
height: var(--kz-scroll-sequence-distance);
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__stage {
position: sticky;
top: 0;
display: block;
width: auto;
height: 100svh;
overflow: clip;
background: var(--kz-scroll-sequence-sky);
container: kodza-scroll-sequence / size;
}
/* The picture under the canvas until the first frame is drawn, then the canvas over it */
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__poster,
.kodza-scroll-sequence__canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
max-width: none;
height: 100%;
margin: 0;
border-radius: 0;
aspect-ratio: auto;
object-fit: var(--kz-scroll-sequence-fit);
object-position: var(--kz-scroll-sequence-position);
}
/* Once a frame is drawn, the picture steps back (still read out: its alt names the sequence) */
.kodza-scroll-sequence.is-drawn .kodza-scroll-sequence__poster {
opacity: 0;
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__panel {
position: absolute;
top: clamp(28px, 9cqh, 96px);
left: var(--kz-scroll-sequence-gutter);
width: min(420px, 34cqw);
gap: clamp(20px, 4cqh, 36px);
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__meter {
display: grid;
}
.kodza-scroll-sequence__counter {
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
/* The rail: it fills with the scroll (--kz-scroll-sequence-progress, 0 to 1), and a dot marks
where each caption comes in, inked once passed */
.kodza-scroll-sequence__rail {
position: relative;
display: block;
height: 1px;
background: var(--kz-scroll-sequence-rail);
}
.kodza-scroll-sequence__rail::before {
content: "";
position: absolute;
inset: 0;
background: var(--kz-scroll-sequence-ink);
transform: scaleX(var(--kz-scroll-sequence-progress, 0));
transform-origin: left center;
}
.kodza-scroll-sequence__tick {
position: absolute;
top: 50%;
left: calc(var(--kz-scroll-sequence-at, 0) * 100%);
box-sizing: border-box;
width: 7px;
height: 7px;
border: 1px solid var(--kz-scroll-sequence-rail);
border-radius: var(--kz-radius-pill, 999px);
background: var(--kz-scroll-sequence-sky);
transform: translate(-50%, -50%);
transition: background-color calc(0.2s * var(--kz-motion-scale, 1)) ease, border-color calc(0.2s * var(--kz-motion-scale, 1)) ease;
}
.kodza-scroll-sequence__tick:first-child {
transform: translate(0, -50%);
}
.kodza-scroll-sequence__tick.is-passed {
border-color: var(--kz-scroll-sequence-ink);
background: var(--kz-scroll-sequence-ink);
}
/* The captions share one cell. The one on now stands; the next waits below, the last one has
gone up: scrolling on rolls them up, scrolling back rolls them down */
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__captions {
grid-template-columns: minmax(0, 1fr);
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption {
grid-area: 1 / 1;
pointer-events: none;
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption > * {
opacity: 0;
transform: translateY(18px);
/* Leaving: it fades as it slides, on the same curve, so the way out reads */
transition: opacity calc(0.24s * var(--kz-motion-scale, 1)) cubic-bezier(0.4, 0, 1, 1), transform calc(0.24s * var(--kz-motion-scale, 1)) cubic-bezier(0.4, 0, 1, 1);
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption.is-past > * {
transform: translateY(-18px);
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption.is-current {
pointer-events: auto;
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption.is-current > * {
opacity: 1;
transform: none;
transition: opacity calc(0.5s * var(--kz-motion-scale, 1)) ease, transform calc(0.7s * var(--kz-motion-scale, 1)) cubic-bezier(0.22, 1, 0.36, 1);
/* In once the last one is nearly gone, so two titles never stand over each other */
transition-delay: calc(0.16s * var(--kz-motion-scale, 1));
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption.is-current > :nth-child(2) {
transition-delay: calc(0.2s * var(--kz-motion-scale, 1));
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption.is-current > :nth-child(n + 3) {
transition-delay: calc(0.24s * var(--kz-motion-scale, 1));
}
/* A tall stage (a phone, a tablet, a 4:3 window): the captions on top, the frames in a box at the
bottom that fades into the sky above it. Side by side, the captions would cover the subject. On
a wide one (4:3) the box grows to the whole height of a 16:9 frame, as far as the captions let
it, so nothing of the subject is cut at the bottom */
@container kodza-scroll-sequence (max-aspect-ratio: 14 / 9) {
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__poster,
.kodza-scroll-sequence__canvas {
top: auto;
height: min(max(var(--kz-scroll-sequence-media-portrait), 56.25cqw), 100cqh - 190px, 100cqw);
object-position: var(--kz-scroll-sequence-position-portrait);
-webkit-mask-image: linear-gradient(to bottom, transparent, #000 12%);
mask-image: linear-gradient(to bottom, transparent, #000 12%);
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__panel {
top: clamp(20px, 4cqh, 40px);
right: var(--kz-scroll-sequence-gutter);
width: auto;
max-width: 520px;
}
}
/* kodza-guard f3278ebfb1a9 — generated by Kodza, do not edit.
Theme guard: keeps a site's own CSS (WordPress themes, Bootstrap, CSS resets…) off this
component. :not(#_) is always true and weighs as much as an id, so these lines win over
theme rules made of classes and tags. Customise the component through its --kz-* variables. */
/* What the browser gives, where the component sets it only in some states: at the lowest weight, so its own rules below put it back */
:where(.kodza-scroll-sequence__poster):not(#_) {
position: revert;
}
:where(.kodza-scroll-sequence__label):not(#_),
:where(.kodza-scroll-sequence__title):not(#_),
:where(.kodza-scroll-sequence__text):not(#_) {
transition: revert;
}
/* What the browser gives, on everything the component leaves to it */
.kodza-scroll-sequence:not(#_),
.kodza-scroll-sequence__canvas:not(#_),
.kodza-scroll-sequence__panel:not(#_),
.kodza-scroll-sequence__counter:not(#_),
.kodza-scroll-sequence__rail:not(#_),
.kodza-scroll-sequence__captions:not(#_),
.kodza-scroll-sequence__caption:not(#_) {
box-sizing: revert;
}
.kodza-scroll-sequence__poster:not(#_) {
border: revert;
padding: revert;
outline: revert;
text-decoration: revert;
transition: revert;
list-style: revert;
box-sizing: revert;
float: revert;
box-shadow: revert;
outline-offset: revert;
appearance: revert;
max-height: revert;
color: revert;
font-family: revert;
font-size: revert;
font-weight: revert;
font-style: revert;
line-height: revert;
letter-spacing: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
}
.kodza-scroll-sequence__meter:not(#_) {
border: revert;
padding: revert;
border-radius: revert;
outline: revert;
text-decoration: revert;
transition: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
background-color: revert;
background-image: revert;
box-shadow: revert;
outline-offset: revert;
appearance: revert;
max-width: revert;
max-height: revert;
color: revert;
font-family: revert;
font-size: revert;
font-weight: revert;
font-style: revert;
line-height: revert;
letter-spacing: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
}
.kodza-scroll-sequence__label:not(#_),
.kodza-scroll-sequence__title:not(#_) {
border: revert;
padding: revert;
border-radius: revert;
outline: revert;
text-decoration: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
background-color: revert;
background-image: revert;
box-shadow: revert;
outline-offset: revert;
appearance: revert;
max-width: revert;
max-height: revert;
font-style: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
}
.kodza-scroll-sequence__text:not(#_) {
border: revert;
padding: revert;
border-radius: revert;
outline: revert;
text-decoration: revert;
list-style: revert;
box-sizing: revert;
float: revert;
position: revert;
background-color: revert;
background-image: revert;
box-shadow: revert;
outline-offset: revert;
appearance: revert;
max-height: revert;
font-family: revert;
font-weight: revert;
font-style: revert;
letter-spacing: revert;
text-transform: revert;
text-underline-offset: revert;
text-align: revert;
text-indent: revert;
text-shadow: revert;
vertical-align: revert;
}
.kodza-scroll-sequence__label:not(#_):where(:not([hidden])),
.kodza-scroll-sequence__title:not(#_):where(:not([hidden])),
.kodza-scroll-sequence__text:not(#_):where(:not([hidden])) {
display: revert;
}
.kodza-scroll-sequence__label:not(#_):where([hidden]),
.kodza-scroll-sequence__title:not(#_):where([hidden]),
.kodza-scroll-sequence__text:not(#_):where([hidden]) {
display: none;
}
.kodza-scroll-sequence:not(#_)::after,
.kodza-scroll-sequence__rail:not(#_)::before {
box-sizing: revert;
}
/* The component's own rules, one step stronger: real buttons and fields, what it sets on tags themes style (links, lists, headings, images…), and the root width (a theme may narrow it, never widen it) */
.kodza-scroll-sequence__poster:not(#_) {
display: block;
max-width: none;
margin: 0;
border-radius: var(--kz-scroll-sequence-radius);
background: var(--kz-scroll-sequence-sky);
}
.kodza-scroll-sequence__meter:not(#_) {
display: none;
margin: 0;
}
.kodza-scroll-sequence__label:not(#_) {
margin: 0;
color: var(--kz-scroll-sequence-muted);
font-family: var(--kz-font-label, var(--kz-scroll-sequence-mono));
font-size: var(--kz-font-label-size, 12.5px);
font-weight: var(--kz-font-label-weight, 500);
line-height: var(--kz-font-label-leading, 1.4);
letter-spacing: var(--kz-font-label-tracking, 0.02em);
text-transform: var(--kz-font-label-case, inherit);
}
.kodza-scroll-sequence__title:not(#_) {
margin: 0;
color: inherit;
font-family: var(--kz-font-display, inherit);
font-size: calc(clamp(28px, 3.6cqi, 52px) * var(--kz-font-display-scale, 1));
font-weight: var(--kz-font-display-weight, 600);
line-height: 1.02;
letter-spacing: var(--kz-font-display-tracking, -0.045em);
text-transform: var(--kz-font-display-case, inherit);
}
.kodza-scroll-sequence:not(.is-live) .kodza-scroll-sequence__title:not(#_) {
font-size: calc(clamp(24px, 2.6cqi, 34px) * var(--kz-font-display-scale, 1));
}
.kodza-scroll-sequence__text:not(#_) {
max-width: 30em;
margin: 0;
color: var(--kz-scroll-sequence-muted);
font-size: 16px;
line-height: 1.55;
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__poster:not(#_) {
position: absolute;
display: block;
max-width: none;
margin: 0;
border-radius: 0;
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__meter:not(#_) {
display: grid;
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption > *:not(#_) {
transition: opacity calc(0.24s * var(--kz-motion-scale, 1)) cubic-bezier(0.4, 0, 1, 1), transform calc(0.24s * var(--kz-motion-scale, 1)) cubic-bezier(0.4, 0, 1, 1);
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption.is-current > *:not(#_) {
transition: opacity calc(0.5s * var(--kz-motion-scale, 1)) ease, transform calc(0.7s * var(--kz-motion-scale, 1)) cubic-bezier(0.22, 1, 0.36, 1);
transition-delay: calc(0.16s * var(--kz-motion-scale, 1));
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption.is-current > :nth-child(2):not(#_) {
transition-delay: calc(0.2s * var(--kz-motion-scale, 1));
}
.kodza-scroll-sequence.is-live .kodza-scroll-sequence__caption.is-current > :nth-child(n + 3):not(#_) {
transition-delay: calc(0.24s * var(--kz-motion-scale, 1));
}
// Kodza License: free to use, client work included. Not to resell or redistribute, ported or not: https://kodza.dev/docs/#print-license
// Kodza — Scroll Sequence
// The section becomes a track: its stage holds the screen while the scroll runs through it, and the
// script draws the frame that matches the scroll on one canvas. The frames load coarse to fine
// (every eighth first, then the ones between), so a fast scroll shows the nearest frame already
// there, never a blank. Each caption comes in at its own point (data-at, 0 to 1), the rail fills
// and the counter counts. The frames are set on the root: data-frames (how many), data-src (their
// address, {index} standing for 01, 02…) and data-pad (the digits of {index}, those of
// data-frames by default). The pacing is in the constants below. Reduced motion, or a site pace
// of 0: the first picture sits above the captions, and nothing loads.
(() => {
const SELECTOR = '[data-kodza-scroll-sequence]';
// The order the frames load in: every 8th, then every 4th, every 2nd, the rest
const PASSES = [8, 4, 2, 1];
// How many frames load at once
const PARALLEL = 6;
// The frames start loading this far ahead of the section (a share of the screen)
const AHEAD = '100%';
// How long the sequence takes to catch up with the scroll, in ms (times the site's pace), in two
// soft stages like a smooth-scroll library: it picks up gently, and when the wheel stops it slows
// down on its own instead of stopping with it. 0 glues it to the wheel
const SMOOTHING = 170;
// While the scroll runs, the picture blends the two frames around its exact place, so the turn
// moves on every screen refresh instead of frame by frame. Once the scroll has stood still this
// long, in ms, it settles on the next whole frame the way it was turning (never back, so it does
// not shiver as you stop): never a double picture at rest
const REST = 120;
// The progress left to travel below this ends the loop
const SETTLE = 0.0005;
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// The site's pace, --kz-motion-scale (set on :root, read on the instance): every duration of the
// script times it, and 0 moves nothing, like reduced motion
const paceOf = (el) => {
const pace = parseFloat(getComputedStyle(el).getPropertyValue('--kz-motion-scale'));
return pace >= 0 ? pace : 1;
};
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
// One instance, with its own state: two copies on a page never interfere. Everything it
// starts is tied to `signal` (listeners get { signal }, observers and timers stop on its
// 'abort' event), so kodza:destroy stops it all.
function mount(root, signal) {
const stage = root.querySelector('.kodza-scroll-sequence__stage');
const poster = root.querySelector('.kodza-scroll-sequence__poster');
const counter = root.querySelector('.kodza-scroll-sequence__counter');
const rail = root.querySelector('.kodza-scroll-sequence__rail');
const captions = [...root.querySelectorAll('.kodza-scroll-sequence__caption')];
const frames = parseInt(root.dataset.frames, 10) || 0;
const pace = paceOf(root);
const still = reduceMotion || !pace; // the still path: no motion asked, by the visitor or the site
if (still || !stage || frames < 2 || !root.dataset.src?.includes('{index}')) return;
const smoothing = SMOOTHING * pace;
const digits = String(frames).length;
const pad = parseInt(root.dataset.pad, 10) || digits;
const at = captions.map((caption) => clamp(parseFloat(caption.dataset.at) || 0, 0, 1));
const total = String(frames).padStart(digits, '0');
// A dot on the rail where each caption comes in
const ticks = at.map((point) => {
const tick = document.createElement('span');
tick.className = 'kodza-scroll-sequence__tick';
tick.style.setProperty('--kz-scroll-sequence-at', point);
rail?.append(tick);
return tick;
});
root.classList.add('is-live');
const images = new Array(frames).fill(null);
const loading = new Set();
let order = [];
let next = 0;
let canvas = null;
let context = null;
let drawn = -1; // what the canvas shows: a frame, plus the share of the next one over it
let picture = null; // the place in the sequence drawn: between two frames while it runs
let leading = null; // the first stage of each glide, the one `current` and `picture` follow
let turning = null;
let index = 0; // the frame the scroll asks for
let target = null; // the progress the scroll asks for, when it last changed, and which way
let moved = 0;
let way = 1;
let held = null; // the whole frame it settled on, kept until the scroll goes past it
let on = -1; // the caption on now
let current = null; // the progress written now
let raf = 0;
let last = 0;
let shown = false;
// The frame asked for if it is there, or the nearest one loaded, the earlier one first
const nearest = (wanted) => {
for (let distance = 0; distance < frames; distance += 1) {
if (images[wanted - distance]) return wanted - distance;
if (images[wanted + distance]) return wanted + distance;
}
return -1;
};
const paint = () => {
if (picture === null) return;
// The frame under the place drawn, and the share of the next one, in steps of 1/32
let frame = Math.floor(picture);
let share = Math.round((picture - frame) * 64) / 64;
if (share === 1) {
frame += 1;
share = 0;
}
// Both frames not in yet: the nearest one loaded, alone
if (!images[frame] || (share && !images[frame + 1])) {
frame = nearest(Math.round(picture));
share = 0;
}
if (frame < 0 || frame + share === drawn) return;
const image = images[frame];
if (!canvas) {
// Sized on the first frame in: the CSS fits it to the stage like the picture under it
canvas = document.createElement('canvas');
canvas.className = 'kodza-scroll-sequence__canvas';
canvas.setAttribute('aria-hidden', 'true');
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
context = canvas.getContext('2d');
if (poster) poster.after(canvas);
else stage.prepend(canvas);
root.classList.add('is-drawn');
}
context.globalAlpha = 1;
context.drawImage(image, 0, 0, canvas.width, canvas.height);
if (share) {
context.globalAlpha = share;
context.drawImage(images[frame + 1], 0, 0, canvas.width, canvas.height);
context.globalAlpha = 1;
}
drawn = frame + share;
};
const pump = () => {
const pattern = root.dataset.src;
while (loading.size < PARALLEL && next < order.length) {
const frame = order[next];
next += 1;
const image = new Image();
image.decoding = 'async';
image.src = pattern.replace('{index}', String(frame + 1).padStart(pad, '0'));
loading.add(image);
image.decode().then(() => {
if (signal.aborted) return;
images[frame] = image;
paint();
}, () => {}).finally(() => {
loading.delete(image);
if (!signal.aborted) pump();
});
}
};
const load = () => {
if (order.length) return;
const queued = new Set();
PASSES.forEach((step) => {
for (let frame = 0; frame < frames; frame += step) queued.add(frame);
});
queued.add(frames - 1);
order = [...queued];
pump();
};
// The track: 0 as the stage reaches the top of the screen, 1 as it lets go
const goal = () => {
const box = root.getBoundingClientRect();
const span = box.height - stage.getBoundingClientRect().height;
return span > 0 ? clamp(-box.top / span, 0, 1) : 0;
};
const write = () => {
root.style.setProperty('--kz-scroll-sequence-progress', current.toFixed(4));
index = Math.round(current * (frames - 1));
if (counter) counter.textContent = `${String(index + 1).padStart(digits, '0')} / ${total}`;
paint();
// The caption on is the last one whose point is passed
let now = 0;
at.forEach((point, i) => {
if (current >= point - SETTLE) now = i;
});
ticks.forEach((tick, i) => tick.classList.toggle('is-passed', current >= at[i] - SETTLE));
if (now === on) return;
on = now;
captions.forEach((caption, i) => {
caption.classList.toggle('is-current', i === now);
caption.classList.toggle('is-past', i < now);
});
};
const tick = (time) => {
raf = 0;
const to = goal();
const frame = last ? Math.min(time - last, 64) : 16.7;
last = time;
if (to !== target) {
if (target !== null) way = Math.sign(to - target);
target = to;
moved = time;
}
const ease = 1 - Math.exp(-frame / smoothing);
if (current === null || !smoothing) leading = current = to;
else {
leading += (to - leading) * ease;
current += (leading - current) * ease;
}
if (Math.abs(to - current) < SETTLE && Math.abs(to - leading) < SETTLE) leading = current = to;
// The picture follows the same glide, between two frames, then on to a whole one at rest. It
// waits there while the scroll picks up the same way, so the turn never steps back
const exact = to * (frames - 1);
const resting = time - moved >= REST;
if (resting) held = way < 0 ? Math.floor(exact + 0.001) : Math.ceil(exact - 0.001);
else if (held !== null && (way < 0 ? exact <= held : exact >= held)) held = null;
const aim = !smoothing ? Math.round(exact) : held ?? exact;
if (picture === null || !smoothing) turning = picture = aim;
else {
turning += (aim - turning) * ease;
picture += (turning - picture) * ease;
}
if (Math.abs(aim - picture) < 0.01 && Math.abs(aim - turning) < 0.01) turning = picture = aim;
write();
if (current !== to || picture !== aim || !resting) raf = requestAnimationFrame(tick);
else last = 0;
};
const queue = () => {
if (!shown || raf) return;
raf = requestAnimationFrame(tick);
};
const place = () => {
current = goal();
target = current;
picture = Math.round(current * (frames - 1));
held = picture;
leading = current;
turning = picture;
write();
};
// Placed before it first shows, so the captions do not roll in front of you on load
place();
// Focus landing in a caption that is not on (a link in its text, from the keyboard): the page
// scrolls to where that caption comes in
root.addEventListener('focusin', (event) => {
const i = captions.findIndex((caption) => caption.contains(event.target));
if (i < 0 || i === on) return;
const box = root.getBoundingClientRect();
const span = box.height - stage.getBoundingClientRect().height;
window.scrollTo({ top: scrollY + box.top + at[i] * span, behavior: 'instant' });
place();
}, { signal });
const io = new IntersectionObserver(([entry]) => {
shown = entry.isIntersecting;
if (!shown) return;
load();
place(); // back on screen after a jump (an anchor, Home): placed, not chased
}, { rootMargin: `${AHEAD} 0px` });
io.observe(root);
const resize = new ResizeObserver(queue);
resize.observe(stage);
window.addEventListener('scroll', queue, { passive: true, signal });
window.addEventListener('resize', queue, { signal });
signal.addEventListener('abort', () => {
io.disconnect();
resize.disconnect();
cancelAnimationFrame(raf);
// Downloads still running stop, and the frames go with the instance
loading.forEach((image) => image.removeAttribute('src'));
loading.clear();
images.fill(null);
canvas?.remove();
});
}
// Kodza lifecycle, the same in every component: `kodza:init` starts the instances inside
// event.detail.root (the whole page by default), `kodza:destroy` stops them and puts their
// markup back. For themes and apps that swap content without a page load. Each running root
// keeps its markup as written under KODZA, shared by every Kodza script on the page: a Kodza
// component inside another one is saved as written, not half-started, and stopped with it. Two
// components on one root (a reveal on a stat card) both start, and stop together.
const KODZA = Symbol.for('kodza');
const RUNNING = Symbol.for(`kodza ${SELECTOR}`); // a root this component runs on, for every copy of this script
const instances = new WeakMap();
const find = (scope = document) => [scope, ...scope.querySelectorAll(SELECTOR)].filter((el) => el.matches?.(SELECTOR));
const attrsOf = (el) => [...el.attributes].map((a) => [a.name, a.value]);
const put = (el, saved) => {
[...el.attributes].forEach((a) => el.removeAttribute(a.name));
saved.attrs.forEach(([name, value]) => el.setAttribute(name, value));
el.innerHTML = saved.html;
};
// The markup inside a root as written: Kodza components already running in it give back their own
const written = (root) => {
const all = [...root.querySelectorAll('*')];
if (!all.some((el) => el[KODZA])) return root.innerHTML;
const copy = root.cloneNode(true);
const twins = copy.querySelectorAll('*');
let last = null;
all.forEach((el, i) => {
if (!el[KODZA] || last?.contains(el)) return;
last = el;
put(twins[i], el[KODZA]);
});
return copy.innerHTML;
};
const init = (scope) => find(scope).forEach((root) => {
// Already running, maybe from another copy of this script. Marked is-ready with no Kodza component
// running on it, a root is a copy of a started one (a cloned slide): it is left as it is
if (root[RUNNING] || (root.classList.contains('is-ready') && !root[KODZA])) return;
// Rendered by React (data-kodza-manual): React starts it, with an init aimed at that markup once
// it has hydrated it. Any earlier start would change markup React has not taken over yet
const owner = root.closest('[data-kodza-manual]');
if (owner && !(scope && owner.contains(scope))) return;
const controller = new AbortController();
// Another Kodza component already running on this root: its markup as written is kept, and the
// two stop together
const before = root[KODZA];
const saved = { html: before ? before.html : written(root), attrs: before ? before.attrs : attrsOf(root), stop: () => destroy(root) };
if (before) before.above = saved;
instances.set(root, { controller, saved, before });
root[KODZA] = saved;
root[RUNNING] = true;
mount(root, controller.signal);
root.classList.add('is-ready');
});
const destroy = (scope) => find(scope).forEach((root) => {
const instance = instances.get(root);
if (!instance) return;
instances.delete(root);
instance.saved.above?.stop(); // a Kodza component started after it on the same root stops first
// The Kodza components running inside it stop first: once its markup is put back, their own
// script could not find them any more
root.querySelectorAll('*').forEach((el) => el[KODZA]?.stop());
instance.controller.abort();
delete root[RUNNING];
// Started on top of another one: that one stops next, and puts the markup back
if (instance.before) {
delete instance.before.above;
root[KODZA] = instance.before;
return instance.before.stop();
}
delete root[KODZA];
put(root, instance.saved);
});
document.addEventListener('kodza:init', (event) => init(event.detail?.root));
document.addEventListener('kodza:destroy', (event) => destroy(event.detail?.root));
init();
// Loaded in <head>, the script runs before the markup exists: once more when it is parsed
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', () => init(), { once: true });
})();
'use client';
// Kodza — Scroll Sequence, for React. From https://kodza.dev/components/scroll-sequence/
// Kodza License: free to use, client work included. Not to resell or redistribute, ported or not: https://kodza.dev/docs/#print-license
// Next to this file: scroll-sequence.css and scroll-sequence.js, the component's CSS and JS from Kodza, as they are.
// Yours to edit: the markup is plain JSX below.
// The script changes this markup once React has rendered it (it adds layers, splits text, moves
// nodes), so React must not patch it afterwards. When a prop changes, the key gives React a fresh
// element: kodza:destroy stops the script on the old one, kodza:init starts it on the new one.
// data-kodza-manual keeps the script from starting it on its own: the effect below does, once
// React has hydrated the markup (on a page that streams in, the script can load first).
// Built from your data (tabs, rows, options)? Put what the markup is made of in the key, as plain
// values (ids, text, numbers): a change starts the script again on fresh markup. What the visitor
// changes (the open tab, the sort, what they type) is the script's: keep it out of the key, follow
// it with onClick or onInput on the root, and change it from your code as a visitor would (click
// the tab; set the field's value, then dispatch a bubbling input event).
import { useRef, useEffect, type ComponentPropsWithoutRef, type ComponentRef, type CSSProperties } from 'react';
import './scroll-sequence.css';
export type ScrollSequenceProps = Omit<ComponentPropsWithoutRef<'div'>, "children" | "vars" | "frames" | "src"> & {
/** data-frames on the root (default: 60) */
frames?: "60";
/** data-src on the root (default: https://kodza.dev/media/kz-arch-day/{index}.webp) */
src?: "https://kodza.dev/media/kz-arch-day/{index}.webp";
/** Settings, as CSS variables on the root */
vars?: {
/** default: 300svh */
"--kz-scroll-sequence-distance"?: string | number;
/** default: cover */
"--kz-scroll-sequence-fit"?: string | number;
/** default: 50% 50% */
"--kz-scroll-sequence-position"?: string | number;
/** default: 56% */
"--kz-scroll-sequence-media-portrait"?: string | number;
/** default: 83% 25% */
"--kz-scroll-sequence-position-portrait"?: string | number;
/** default: clamp(20px, 5cqi, 72px) */
"--kz-scroll-sequence-gutter"?: string | number;
/** default: #e5e3e1 */
"--kz-scroll-sequence-sky"?: string | number;
/** default: var(--kz-color-ink, #0a0a0a) */
"--kz-scroll-sequence-ink"?: string | number;
/** default: color-mix(in srgb, var(--kz-scroll-sequence-ink) 66%, transparent) */
"--kz-scroll-sequence-muted"?: string | number;
/** default: color-mix(in srgb, var(--kz-scroll-sequence-ink) 18%, transparent) */
"--kz-scroll-sequence-rail"?: string | number;
/** default: calc(16px * var(--kz-radius-scale, 1)) */
"--kz-scroll-sequence-radius"?: string | number;
/** default: var(--kz-font-mono, "Geist Mono", ui-monospace, SFMono-Regular, Menlo, monospace) */
"--kz-scroll-sequence-mono"?: string | number;
};
className?: string;
};
export function ScrollSequence({ frames = "60", src = "https://kodza.dev/media/kz-arch-day/{index}.webp", vars, className, style, ...rest }: ScrollSequenceProps) {
const ref = useRef<ComponentRef<'div'>>(null);
const classes = ["kodza-scroll-sequence", className].filter(Boolean).join(' ');
// Everything the markup is made of: when it changes, React gets a fresh element (see the top)
const key = JSON.stringify([classes, frames, src, vars, style, rest]);
// Starts the script on this element once React has rendered it, stops it when it goes
useEffect(() => {
const root = ref.current;
let live = true;
// @ts-ignore: a plain script, no types to find
import('./scroll-sequence.js').then(() => {
if (live) document.dispatchEvent(new CustomEvent('kodza:init', { detail: { root } }));
});
return () => {
live = false;
document.dispatchEvent(new CustomEvent('kodza:destroy', { detail: { root } }));
};
}, [key]);
return (
<div data-kodza-scroll-sequence="" key={key} {...rest} className={classes} data-kodza-manual data-frames={frames} data-src={src} style={{ ...vars, ...style } as CSSProperties} ref={ref}>
<div className="kodza-scroll-sequence__stage">
<img className="kodza-scroll-sequence__poster" src="https://kodza.dev/media/kz-arch-day/01.webp" alt="A concrete wall with a single arched opening, its patch of sunlight crossing the ground from morning to evening" width="1440" height="810" />
<div className="kodza-scroll-sequence__panel">
<p className="kodza-scroll-sequence__meter" aria-hidden="true">
<span className="kodza-scroll-sequence__counter">01 / 60</span>
{' '}
<span className="kodza-scroll-sequence__rail" />
</p>
<div className="kodza-scroll-sequence__captions">
<div className="kodza-scroll-sequence__caption" data-at="0">
<p className="kodza-scroll-sequence__label">Le Mur — Étretat, 8:10</p>
<h2 className="kodza-scroll-sequence__title">One wall. One hole.</h2>
<p className="kodza-scroll-sequence__text">Poured on the cliff path, facing inland. Nobody moves in this film, and neither does the camera. Only the sun does.</p>
</div>
<div className="kodza-scroll-sequence__caption" data-at="0.36">
<p className="kodza-scroll-sequence__label">Noon — sun 38° up</p>
<h2 className="kodza-scroll-sequence__title">The patch crosses the slab.</h2>
<p className="kodza-scroll-sequence__text">The sun climbs and the light through the arch slides across the ground. A sundial nobody asked for.</p>
</div>
<div className="kodza-scroll-sequence__caption" data-at="0.7">
<p className="kodza-scroll-sequence__label">Evening — sun 14° up</p>
<h2 className="kodza-scroll-sequence__title">Then the wall takes it back.</h2>
<p className="kodza-scroll-sequence__text">Low sun, long shadow. By seven the opening is just a door again, and the scroll gives you the morning back.</p>
</div>
</div>
</div>
</div>
</div>
);
}
Checked
Robots poked at it before you did. Every component goes through the same checks, and again each time it changes.