Count Grid
Number count-up grid: cards multiply and a counter jumps, the it-scales stat shot. A grid of cards rushes in under a horizontal smear, settles on five and a label, then the camera pulls back a fraction while every empty cell around them fills in from the middle out and the number jumps.
Source
components/kuchi/count-grid.tsx
1"use client";23import { loadFont as loadSans } from "@remotion/google-fonts/Inter";4import {5 AbsoluteFill,6 getRemotionEnvironment,7 Img,8 interpolate,9 staticFile,10 useCurrentFrame,11 useVideoConfig,12} from "remotion";13import {14 Item,15 mixOklch,16 type KuchiTheme,17 useKuchiTheme,18} from "@/lib/kuchi-ui";1920/**21 * A grid of cards that rushes in, settles on a small count, then fills the22 * frame while the number jumps.23 *24 * Five cards and a label; the camera pulls back a fraction, every empty cell25 * around them fills in from the middle out, and the label goes from five to26 * five hundred. It is the "and it scales" shot — one thing, then all of them,27 * without a cut.28 *29 * Beat by beat, measured off the reference at 30fps:30 *31 * | beat | frames | what moves |32 * | --- | --- | --- |33 * | rush in | 0 – 7 | cell pitch 184 → 138, hard horizontal blur |34 * | settle | 7 – 19 | 138 → 121, blur gone by 12 |35 * | label | 11 – 18 | the whole line fades up at once |36 * | drift | 19 – 36 | 121 → 118, still moving, never held |37 * | fill | 37 – 46 | 118 → 96.5, every cell populates, 5 → 500 |38 *39 * The pull-back is a *scale*, not a camera z-move: the two centre columns hold40 * their midpoint at x 358.5 for all 47 frames while the pitch between them41 * walks 138.5 → 96.5, and the cards keep a constant fraction of the cell. And42 * the settle never actually settles — the pitch is still shedding a fifth of a43 * pixel per frame at frame 37, which is what stops the middle of the shot44 * looking like a freeze-frame.45 */4647const { fontFamily: SANS } = loadSans();4849/* ─────────────────────────────────────────────────────────────────────────50 Geometry, in the reference's own 712×398 pixels51 ───────────────────────────────────────────────────────────────────────── */5253export const REF_W = 712;54export const REF_H = 398;5556/**57 * What the pull-back scales about.58 *59 * Horizontally it is the midpoint the two centre columns hold all clip: 358.75.60 * Vertically it is *not* the middle row — the centre row's own middle sits at61 * 202.5 and moves to 203.5 as the grid shrinks, which puts the fixed point 5px62 * lower, at 207.6. Scaling about the row instead leaves the whole grid a63 * pixel high through the entire fill.64 */65export const ORIGIN = { x: 358.75, y: 207.6 };6667/**68 * One cell at scale 1, and how much of it the card takes.69 *70 * The *cell* is measured — 120.5 × 147, the pitch the whole pull-back is fitted71 * to. The card inside it is a design choice and it is deliberately not the72 * reference's: measured, that card is 90.5 × 117 with a 30px gutter and a 6px73 * radius, which reads as a scatter of small rounded stickers. This one fills74 * 95% of its cell with a 3px corner, so the grid reads as a wall of frames.75 */76export const CELL = { w: 120.5, h: 147 };77export const CARD = { w: 114.5, h: 139.5, radius: 3 };7879/**80 * How much closer the grid sits than the reference's camera.81 *82 * One, now. It was 1.75 for exactly as long as it took to find out that the83 * cards were being *drawn* at reference-pixel size inside cells laid out at84 * render-pixel size — 0.42 of their own cell rather than 0.95 — and that no85 * amount of zoom fixes a missing unit conversion, it only moves the error into86 * the cell spacing as well. Left in place because it is the honest knob if the87 * grid should ever sit closer than the reference's, and because a `1` with this88 * note attached is cheaper than someone rediscovering the bug.89 */90export const GRID_ZOOM = 1;9192/** A column *boundary* sits on the centre; a row *centre* does. */93export const COL_EDGE = 359;94export const ROW_MID = 202.5;9596/**97 * The five that are there before the grid fills.98 *99 * `[col, row]`, with column 0 immediately right of the centre boundary and row100 * 0 the middle row. Four make a square around the label and the fifth hangs101 * below on the right — an even 2×2 reads as a swatch, and the odd one out is102 * what makes it read as a handful of people.103 */104export const SEED: readonly (readonly [number, number])[] = [105 [-1, -1],106 [0, -1],107 [-1, 0],108 [0, 0],109 [0, 1],110];111112/** The label, at scale 1. */113export const LABEL = {114 cy: ROW_MID,115 size: 30,116 padX: 15,117 height: 41,118 radius: 9,119};120121/* ─────────────────────────────────────────────────────────────────────────122 Curves123 ───────────────────────────────────────────────────────────────────────── */124125/** When the grid stops being five cards and becomes all of them. */126export const FILL_AT = 37;127128/**129 * The cell pitch, in reference px — the one number the whole scene hangs on.130 *131 * Tracked as the distance between the two centre columns: 138.5 at frame 7,132 * 128 at 11, 121.5 at 19, 120 at 26, 118 from 32 to 36, then 113, 104, 99.5,133 * 96.5 across the fill. Before frame 7 the horizontal blur is wider than a card134 * and nothing can be measured, so that stretch is the same exponential run135 * backwards, which puts frame 0 at 174 — a card and a half outside the frame.136 *137 * Knots, not that exponential. Two of them fitted the settle and the fill to138 * within a pixel and a half each, which is a pixel and a half of grid, and they139 * met 3.7px apart at the switch going the *wrong way* — the pitch grew by 0.17140 * between 36 and 37, so the grid jolted outward on the exact frame it was141 * supposed to start collapsing. Every knot below is a measured frame and the142 * table is strictly decreasing, so it cannot do that.143 *144 * Every frame number here is zero-based, and every one of them was read off a145 * file called `f0NN.png` that is one-based: `f038.png` is frame 37. Taking the146 * filenames at face value put the whole clip a frame late and cost the switch147 * frame alone 17 mean.148 */149export function pitch(t: number, fps = 30): number {150 return interpolate(151 t * fps,152 [153 0, 7, 9, 11, 13, 15, 17, 19, 22, 26, 29, 31, 36, 37, 38, 39, 40, 41, 42,154 43, 45, 46,155 ],156 [157 174, 138.5, 132, 128, 125, 124, 122.5, 121.5, 120, 119.6, 119, 118.5, 118,158 117.9, 113, 106, 104, 102, 99.5, 97.5, 96.6, 96.5,159 ],160 { extrapolateLeft: "clamp", extrapolateRight: "clamp" },161 );162}163164/** The pitch as a multiple of the cell, which is what everything scales by. */165export function gridScale(t: number, fps = 30): number {166 return pitch(t, fps) / CELL.w;167}168169/**170 * The horizontal blur, in reference px.171 *172 * Measured as the frame's own horizontal gradient, which is what a smear173 * actually costs: 0.28 at frame 3, 1.90 at 4, 2.75 at 5, and then flat for ten174 * frames while it creeps to 3.18. So the blur is spent inside five frames and175 * the long ease that follows it is sharp.176 *177 * The first cut derived this from the pull-back's own speed, on the theory that178 * a blur and its motion should not be able to disagree. They can: the motion is179 * still shedding two pixels of pitch a frame at frame 8 and the reference is180 * already sharp there, so the derivative kept a smear on screen for seven181 * frames the reference does not have, and cost those frames 3 mean each.182 */183export function entryBlur(t: number, fps = 30): number {184 return interpolate(t * fps, [0, 2, 3, 4, 5], [22, 14, 6, 2, 0], {185 extrapolateLeft: "clamp",186 extrapolateRight: "clamp",187 });188}189190/**191 * The one card that arrives late, in reference px of extra smear.192 *193 * The scene's own blur is spent by frame 5, but the card behind the label is194 * still a horizontal streak at 7 and only lands around 9 — every other card is195 * sharp and at scale by 5. It is the detail that makes the entry read as five196 * separate things arriving rather than one picture being pushed in, and it is197 * the only per-card timing in the piece.198 */199export const LATE_CARD: readonly [number, number] = [-1, 0];200201export function lateBlur(t: number, fps = 30): number {202 return interpolate(t * fps, [3, 5, 7, 9], [26, 17, 7, 0], {203 extrapolateLeft: "clamp",204 extrapolateRight: "clamp",205 });206}207208/** The label fading up, all of it at once. */209export function labelIn(t: number, fps = 30): number {210 return interpolate(t * fps, [11, 13, 15, 17, 18], [0, 0.22, 0.55, 0.9, 1], {211 extrapolateLeft: "clamp",212 extrapolateRight: "clamp",213 });214}215216/**217 * How present a cell is.218 *219 * `ring` is the Chebyshev distance to the nearest seed cell, so the ring220 * touching the five arrives first and each one after it a frame later. That221 * ordering is measured: at frame 37 exactly two new columns exist, either side222 * of the original two, and by 38 there are eight.223 */224export function cellIn(ring: number, t: number, fps = 30): number {225 if (ring === 0) return 1;226 // Half a frame per ring and two frames each: the reference is 90% done in227 // three frames — 20.3, 19.0, 19.8 mean change and then a decay — and a228 // ring-per-frame stagger over three turned that into a five-frame ramp that229 // peaked two frames after the reference had finished.230 // The ramp *straddles* the switch frame rather than starting on it: at frame231 // 37 the reference's first new columns are already half there — 80px wide232 // against a settled 90 — so a ring that is still zero on 37 arrives a whole233 // frame late and the change profile peaks two frames after the reference's.234 //235 // A whole frame of stagger, not half. On frame 37 the reference has ring one236 // and nothing else; by 38 it has rings one and two, the second of them237 // narrower than the first. Half a frame put ring two on screen a frame early238 // and drew cards where the reference still had page.239 const at = FILL_AT - 1 + (ring - 1);240 return interpolate(t * fps, [at, at + 1.5], [0, 1], {241 extrapolateLeft: "clamp",242 extrapolateRight: "clamp",243 });244}245246/** Chebyshev distance from a cell to the nearest of the five. */247export function ringOf(col: number, row: number): number {248 let best = Number.POSITIVE_INFINITY;249 for (const [c, r] of SEED) {250 best = Math.min(best, Math.max(Math.abs(col - c), Math.abs(row - r)));251 }252 return best;253}254255/* ─────────────────────────────────────────────────────────────────────────256 Component257 ───────────────────────────────────────────────────────────────────────── */258259export interface CountGridProps {260 /** What the label counts, before the grid fills and after. */261 from?: string;262 to?: string;263 /** The noun after the number. */264 noun?: string;265 /** Card images, cycled across the grid. Anything `<Img>` can load. */266 cards?: string[];267 /** Defaults to the theme's `background`. */268 background?: string;269 /**270 * The label's ink, left end and right end of its gradient. Default to271 * `primary` walked halfway toward `foreground`, then `primary` — the navy →272 * blue the reference had, but one that turns pale in dark mode instead of273 * vanishing into it.274 */275 inkFrom?: string;276 inkTo?: string;277 theme?: Partial<KuchiTheme>;278 mode?: "light" | "dark";279 /** 1 is the measured speed. */280 speed?: number;281}282283/**284 * The card faces: frames out of kuchi's own showcase clips, and one still per285 * component in the registry — which is what the label is counting.286 *287 * Through `staticFile`, not as a bare path. A Remotion bundle copies the public288 * directory to `<bundle>/public/` and serves it from there, so a hard-coded289 * `/demos/posters/x.webp` is a 404 in every render while looking perfect in the290 * app — `staticFile` is what knows the difference. None of these live in291 * `public/` any more: a fresh install has no kuchi `public/`, so a relative292 * path there failed every render outside this repo. All of them are full URLs293 * on kuchi's media host; `staticFile` is only for a user's own relative paths.294 *295 * Both kinds on purpose. Eight frames alone tile visibly across forty cells;296 * the registry's stills alone are 16:9 scenes of dark text on white, and a 3:4297 * crop of one is a blank card. Interleaved they read the way a real library of298 * clips does — some motion, some interface — and land between the two on299 * weight, which is also where the reference's grid of portraits sits.300 */301const ART = [302 "https://kuchi-media.kalarislabs.com/components/art/art-08.svg",303 "https://kuchi-media.kalarislabs.com/components/art/art-08.svg",304 "https://kuchi-media.kalarislabs.com/components/art/art-05.svg",305 "https://kuchi-media.kalarislabs.com/components/art/art-11.svg",306 "https://kuchi-media.kalarislabs.com/components/art/art-11.svg",307 "https://kuchi-media.kalarislabs.com/components/art/art-10.svg",308 "https://kuchi-media.kalarislabs.com/components/art/art-10.svg",309 "https://kuchi-media.kalarislabs.com/components/art/art-05.svg",310];311const STILLS = [312 "https://kuchi-media.kalarislabs.com/components/art/art-11.svg",313 "https://kuchi-media.kalarislabs.com/components/art/art-04.svg",314 "https://kuchi-media.kalarislabs.com/components/art/art-03.svg",315 "https://kuchi-media.kalarislabs.com/components/art/art-03.svg",316 "https://kuchi-media.kalarislabs.com/components/art/art-05.svg",317 "https://kuchi-media.kalarislabs.com/components/art/art-04.svg",318 "https://kuchi-media.kalarislabs.com/components/art/art-11.svg",319 "https://kuchi-media.kalarislabs.com/components/art/art-04.svg",320 "https://kuchi-media.kalarislabs.com/components/art/art-06.svg",321 "https://kuchi-media.kalarislabs.com/components/art/art-02.svg",322 "https://kuchi-media.kalarislabs.com/components/art/art-02.svg",323 "https://kuchi-media.kalarislabs.com/components/art/art-01.svg",324 "https://kuchi-media.kalarislabs.com/components/art/art-06.svg",325 "https://kuchi-media.kalarislabs.com/components/art/art-01.svg",326 "https://kuchi-media.kalarislabs.com/components/art/art-07.svg",327 "https://kuchi-media.kalarislabs.com/components/art/art-04.svg",328 "https://kuchi-media.kalarislabs.com/components/art/art-01.svg",329 "https://kuchi-media.kalarislabs.com/components/art/art-02.svg",330 "https://kuchi-media.kalarislabs.com/components/art/art-02.svg",331 "https://kuchi-media.kalarislabs.com/components/art/art-04.svg",332 "https://kuchi-media.kalarislabs.com/components/art/art-02.svg",333 "https://kuchi-media.kalarislabs.com/components/art/art-09.svg",334 "https://kuchi-media.kalarislabs.com/components/art/art-01.svg",335 "https://kuchi-media.kalarislabs.com/components/art/art-02.svg",336 "https://kuchi-media.kalarislabs.com/components/art/art-04.svg",337 "https://kuchi-media.kalarislabs.com/components/art/art-05.svg",338 "https://kuchi-media.kalarislabs.com/components/art/art-12.svg",339 "https://kuchi-media.kalarislabs.com/components/art/art-10.svg",340 "https://kuchi-media.kalarislabs.com/components/art/art-03.svg",341 "https://kuchi-media.kalarislabs.com/components/art/art-11.svg",342 "https://kuchi-media.kalarislabs.com/components/art/art-03.svg",343 "https://kuchi-media.kalarislabs.com/components/art/art-10.svg",344 "https://kuchi-media.kalarislabs.com/components/art/art-10.svg",345];346// `concat(...map)`, not `flatMap`: a stock Remotion project types against347// ES2015, where `flatMap` does not exist and the install shows red.348const OWN_CARDS = ([] as string[])349 .concat(...STILLS.map((s, i) => [ART[i % ART.length] as string, s]))350 .map((f) => (f.startsWith("https://") ? f : staticFile(f)));351// index 0, 2, 4 … are ART by construction, and the five seed cells only ever352// reach the first `SEED.length` of them.353354/**355 * A root-relative asset is a `public/` file. Only the site's own Player serves356 * it at that path; Remotion Studio and a render know its URL through357 * `staticFile()` alone — so every environment but the Player rewrites it.358 */359function resolveSrc(src: string): string {360 const isLocal = src.startsWith("/") && !src.startsWith("//");361 if (!isLocal || getRemotionEnvironment().isPlayer) return src;362 // A value that already came out of staticFile() carries the static base363 // (`/static-<hash>/…`); running it through again would prefix it twice.364 const base = staticFile("_").slice(0, -2);365 if (base && src.startsWith(`${base}/`)) return src;366 try {367 return staticFile(src.replace(/^\/+/, ""));368 } catch {369 return src;370 }371}372373export function CountGrid({374 from = "5",375 to = "500",376 noun = "clips",377 cards = OWN_CARDS,378 background,379 inkFrom,380 inkTo,381 speed = 1,382 theme,383 mode,384}: CountGridProps) {385 const t = useKuchiTheme(theme, mode);386 const page = background ?? t.background;387 const ink0 = inkFrom ?? mixOklch(t.primary, t.foreground, 0.5);388 const ink1 = inkTo ?? t.primary;389 // The card edge is half a hairline: forty full-strength borders read as a390 // grid of boxes, not a wall of frames.391 const cardEdge = mixOklch(t.card, t.border, 0.5);392 const frame = useCurrentFrame();393 const { fps, width, height } = useVideoConfig();394 const now = frame / fps / (speed || 1);395396 const k = Math.min(width / REF_W, height / REF_H);397 const ox = (width - REF_W * k) / 2;398 const oy = (height - REF_H * k) / 2;399400 const s = gridScale(now, fps);401 // The cards live one zoom step closer than everything else — see GRID_ZOOM.402 const g = s * GRID_ZOOM;403 const blur = entryBlur(now, fps) * k;404 const late = lateBlur(now, fps) * k;405 const label = labelIn(now, fps);406407 // Everything is laid out at scale 1 and then scaled about ORIGIN, so a cell's408 // place in the grid never depends on the zoom — only its size does.409 const at = (v: number, origin: number, scale = s) =>410 (v - origin) * scale + origin;411 // `cellW`/`cellH` stay in reference px — they are only ever compared against412 // REF_W/REF_H to count how many cells reach the frame.413 const cellW = CELL.w * g;414 const cellH = CELL.h * g;415 // The card's size is drawn, so it needs `k` — the same reference→render scale416 // its own position already goes through. Without it the card was laid out in417 // reference px inside a cell laid out in render px: 0.42 of its cell on a418 // 1280-wide render instead of 0.95, which is every "the images are too small419 // and too far apart" note in one bug.420 const cardW = CARD.w * g * k;421 const cardH = CARD.h * g * k;422423 // How many cells reach the frame at this scale, plus one so nothing pops in424 // at the edge.425 const cols = Math.ceil(REF_W / cellW / 2) + 2;426 const rows = Math.ceil(REF_H / cellH / 2) + 2;427428 const cells: {429 col: number;430 row: number;431 x: number;432 y: number;433 o: number;434 i: number;435 src: string;436 }[] = [];437 for (let row = -rows; row <= rows; row++) {438 for (let col = -cols; col <= cols; col++) {439 const o = cellIn(ringOf(col, row), now, fps);440 if (o <= 0) continue;441 const cx = at(COL_EDGE + (col + 0.5) * CELL.w, ORIGIN.x, g) * k + ox;442 const cy = at(ROW_MID + row * CELL.h, ORIGIN.y, g) * k + oy;443 if (cx < -cardW || cx > width + cardW) continue;444 if (cy < -cardH || cy > height + cardH) continue;445 // A stable, well-mixed index: neighbours must not share a card or the446 // grid reads as wallpaper.447 //448 // `cards` alternates the two kinds, so its even slots are the ones with449 // pictures on them. The five that carry the whole first half of the shot450 // take only those; everything else takes the mix. Sharing one index put451 // three of the five on registry stills — 16:9 scenes of dark text on452 // white, cropped to 3:4 — and the hero moment was two blank cards and a453 // label.454 const mix = col * 7 + row * 13;455 // `col * 2 + row` is the one small combination that is injective over the456 // five seed cells modulo five — 7/13 sends (-1,-1) and (0,0) to the same457 // slot, and the two cards diagonally across the label came out identical.458 const seed = col * 2 + row;459 const i =460 ringOf(col, row) === 0461 ? (2 * (((seed % SEED.length) + SEED.length) % SEED.length)) %462 cards.length463 : ((mix % cards.length) + cards.length) % cards.length;464 cells.push({ col, row, x: cx, y: cy, o, i, src: cards[i] ?? "" });465 }466 }467468 // The grid cycles `cards`, so one card is drawn many times. Studio outlines469 // the copy nearest the middle of the frame; the others are only pictures of470 // it.471 const nearest = new Map<number, (typeof cells)[number]>();472 const off = (c: (typeof cells)[number]) =>473 Math.hypot(c.x - width / 2, c.y - height / 2);474 for (const c of cells) {475 const held = nearest.get(c.i);476 if (!held || off(c) < off(held)) nearest.set(c.i, c);477 }478479 const filled = now * fps >= FILL_AT;480 const pillH = LABEL.height * s * k;481482 return (483 <AbsoluteFill484 style={{485 backgroundColor: page,486 fontFamily: SANS,487 overflow: "hidden",488 }}489 >490 {/* A directional blur: the motion is horizontal, and CSS `blur()` is491 round, so it softens the card's top and bottom edges — which are not492 moving — as much as its sides. */}493 <svg width={0} height={0} style={{ position: "absolute" }} aria-hidden>494 <title>Motion blur</title>495 <filter496 id="count-grid-smear"497 x="-40%"498 y="-10%"499 width="180%"500 height="120%"501 >502 <feGaussianBlur stdDeviation={`${blur} 0`} />503 </filter>504 <filter505 id="count-grid-late"506 x="-90%"507 y="-10%"508 width="280%"509 height="120%"510 >511 <feGaussianBlur stdDeviation={`${late} 0`} />512 </filter>513 </svg>514515 <div516 style={{517 position: "absolute",518 inset: 0,519 filter: blur > 0.35 ? "url(#count-grid-smear)" : undefined,520 ...(getRemotionEnvironment().isRendering521 ? null522 : { willChange: "filter" as const }),523 }}524 >525 {cells.map((c) => (526 <Item527 key={`${c.col},${c.row}`}528 index={c.i}529 primary={nearest.get(c.i) === c}530 >531 <div532 style={{533 position: "absolute",534 left: c.x - cardW / 2,535 top: c.y - cardH / 2,536 width: cardW,537 height: cardH,538 borderRadius: CARD.radius * g * k,539 overflow: "hidden",540 background: t.card,541 // A hairline, not a shadow: a drop shadow under forty white542 // tiles on a white page is forty grey smears.543 boxShadow: `inset 0 0 0 ${Math.max(0.5, 0.9 * g * k)}px ${cardEdge}`,544 display: "flex",545 alignItems: "center",546 opacity: c.o,547 filter:548 late > 0.35 &&549 c.col === LATE_CARD[0] &&550 c.row === LATE_CARD[1]551 ? "url(#count-grid-late)"552 : undefined,553 }}554 >555 {c.src ? (556 <Img557 src={resolveSrc(c.src)}558 style={{559 width: cardW,560 height: cardH,561 // Preflight's `img { max-width: 100% }` against a box that562 // sizes from its content is 100% of nothing — the card563 // renders zero px wide on the site and perfectly in the mp4.564 maxWidth: "none",565 objectFit: "cover",566 }}567 alt=""568 />569 ) : null}570 </div>571 </Item>572 ))}573 </div>574575 <div576 style={{577 position: "absolute",578 left: 0,579 right: 0,580 top: at(LABEL.cy, ORIGIN.y) * k + oy - pillH / 2,581 display: "flex",582 justifyContent: "center",583 opacity: label,584 }}585 >586 <div587 style={{588 display: "flex",589 alignItems: "center",590 height: pillH,591 padding: `0 ${LABEL.padX * s * k}px`,592 borderRadius: LABEL.radius * s * k,593 background: t.card,594 border: `1px solid ${t.border}`,595 // Not a drop shadow: on a white page under a white pill it reads as596 // a grey smear, and the reference has a hairline and nothing else.597 boxShadow: "none",598 fontSize: LABEL.size * s * k,599 fontWeight: 400,600 lineHeight: 1,601 whiteSpace: "nowrap",602 color: ink0,603 textRendering: "geometricPrecision",604 }}605 >606 <span607 style={{608 backgroundImage: `linear-gradient(90deg, ${ink0} 0%, ${ink1} 100%)`,609 WebkitBackgroundClip: "text",610 backgroundClip: "text",611 color: "transparent",612 paddingBottom: 0.14 * LABEL.size * s * k,613 }}614 >615 {filled ? to : from} {noun}616 </span>617 </div>618 </div>619 </AbsoluteFill>620 );621}622623export default CountGrid;









