Word Wheel
Rotating words text: a slot machine word wheel that spins and stops on one word. A column of words that spins like a reel and stops on one of them.
Source
components/kuchi/word-wheel.tsx
1"use client";23import { loadFont as loadSans } from "@remotion/google-fonts/Jost";4import { AbsoluteFill, useCurrentFrame, useVideoConfig } from "remotion";5import {6 mixOklch,7 resolveFont,8 type KuchiTheme,9 useKuchiTheme,10} from "@/lib/kuchi-ui";1112/**13 * A column of words that spins like a reel and stops on one of them.14 *15 * A fixed lead-in — "kuchi for" — holds the left of the line, and the word16 * that finishes the sentence lives in a slot to its right. The slot is one row17 * of a taller column: the words above and below it are visible, smaller and18 * grey, fading out toward the top and bottom of the frame. The column rolls,19 * decelerates over about a second and three quarters, and comes to rest with20 * one word at full size and in ink.21 *22 * ## Everything here was measured off a recording, frame by frame23 *24 * The numbers below are not taste. They were recovered from a 2.1 second,25 * 638 × 354 screen capture by thresholding the ink in each of its 80 frames:26 *27 * - the row pitch is exactly **34px**, and every row's baseline — including28 * the oversized selected one — sits on that grid to the pixel. That is the29 * tell that the scale pivots on the baseline, which is also the only pivot30 * that does not make scaled type climb the pixel grid (see below).31 * - the selected word is **1.665×** the others. Measured on the same word in32 * both states, twice: "Growth" 97.15/58.28 = 1.6671, "Educators"33 * 128.55/77.35 = 1.6618.34 * - the reel travels **9 rows**, and the whole curve is in `ROLL` — it is not35 * a bezier and not a spring. The best cubic-bezier fit is 1.8px RMS and36 * 5.5px off at worst, the best spring 4.3px RMS. A fifth of a row is a37 * visibly different frame, so the measurement ships instead of a curve.38 *39 * ## The selection lags the slot, and that is the whole feel of it40 *41 * A row becomes the selected one when it is nearest the slot — when its42 * baseline is within half a pitch. But it does not *arrive* at full size the43 * instant it crosses: the reference shows the same row at |d| = 17px reading44 * 0.30 on the way in and 0.685 on the way out. That hysteresis is a transition45 * lagging a class change, and it is why the fast part of the roll never looks46 * like a row of identical pops — each word is still growing when the next one47 * takes over. `SWAP` is that lag.48 *49 * ## The face is Jost, and that was measured too50 *51 * The reference is set in a licensed brand face, so the question was which52 * available one has its *proportions*. Fingerprinted on two space-free words at53 * matched cap height — "Framer" and "Growth", in cap units — the reference reads54 * 4.025 and 4.353. Jost reads 4.153 and 4.265; Outfit 4.491 and 4.529; Poppins55 * 5.201 and 5.180. Jost is within 3%, everything else is 10% or worse, and a56 * word 10% wide is a word that no longer ends where the sentence needs it to.57 *58 * ## Two fixed left edges59 *60 * The selected word's ink starts 30.5px left of everyone else's, so it butts up61 * against the lead-in and completes the sentence while the column itself stays62 * indented and out of the way. Both edges are constant across every word and63 * every frame — the reel is left-aligned twice over, not centred once.64 */6566const { fontFamily: SANS } = loadSans("normal", {67 weights: ["400", "500", "600"],68 subsets: ["latin"],69});7071/* ─────────────────────────────────────────────────────────────────────────72 The scene, in the reference's own 638 × 354 pixels73 ───────────────────────────────────────────────────────────────────────── */7475export const REF_W = 638;76export const REF_H = 354;7778/** Row-to-row distance. Measured on eight consecutive baselines: 34.0 each. */79export const PITCH = 34;80/** The slot's baseline — where the sentence sits. */81export const BASE_Y = 183.8;82/** Left of the line's box. The reference's lead-in ink starts at 203.55. */83export const LINE_X = 201.98;84/**85 * The space between the end of the lead-in and the selected word.86 *87 * Measured as a gap rather than as the word's x, and that distinction is the88 * whole reason this scene survives having its copy changed: the reference's word89 * sits at 345.29 only because "Framer for" happens to end at 336.56. Pin the90 * word to 345 and a shorter lead-in leaves a hole, a longer one runs the two91 * together — which is exactly what "kuchi forLaunches" did before this was a92 * gap.93 */94export const GAP = 8.01;95/**96 * How far right the column sits from the word that completes the sentence.97 *98 * The reel is indented and the selected word is not: it steps left out of the99 * column to finish the line, and steps back when it loses the slot. Both edges100 * are constant across every word and every frame of the reference — the reel is101 * left-aligned twice over, not centred once.102 */103export const INDENT = 30.87;104105/** Type size of the selected word. Cap height measured at 22.32px. */106export const SIZE = 30.63;107/** Box top → baseline, at `lineHeight: 1`. A face metric, measured, not guessed. */108export const ASC = 25.43;109/** Unselected scale. The reciprocal of the measured 1.665. */110export const SMALL = 1 / 1.665;111112/** Rows travelled. The reel comes back round to the word it started on. */113export const SPIN = 9;114115/**116 * How long a row takes to grow into the slot, in seconds, and its curve.117 *118 * Measured from the frame a row crosses half a pitch: 0.90 at 38ms, and inside119 * the noise floor of a width measurement thereafter. A settle worth one frame120 * is a settle; this one is worth two.121 */122export const SWAP = 0.07;123124/**125 * The reel's own deceleration, as (seconds, share of the travel).126 *127 * Recovered from the recording by taking the phase of the 34px row grid in each128 * frame's vertical ink profile — which reads the offset to a hundredth of a129 * pixel off all eleven visible rows at once, rather than tracking one word and130 * inheriting its glyphs. Frames the capture sampled twice are dropped: they are131 * the recorder's duplicates, not the page's, and keeping them would freeze the132 * render for a frame in exactly the places the reference is moving fastest.133 */134export const ROLL: readonly (readonly [number, number])[] = [135 [0.1583, 0.00033],136 [0.175, 0.02175],137 [0.2083, 0.05298],138 [0.2417, 0.08995],139 [0.275, 0.13203],140 [0.3083, 0.18194],141 [0.3417, 0.23034],142 [0.3833, 0.28119],143 [0.4167, 0.32889],144 [0.45, 0.37627],145 [0.4833, 0.42203],146 [0.5167, 0.46322],147 [0.5583, 0.50415],148 [0.575, 0.54044],149 [0.6083, 0.57445],150 [0.6417, 0.6073],151 [0.675, 0.63751],152 [0.7083, 0.66516],153 [0.7417, 0.69074],154 [0.775, 0.71485],155 [0.8083, 0.73897],156 [0.8417, 0.75963],157 [0.875, 0.77896],158 [0.9083, 0.79751],159 [0.9417, 0.81422],160 [0.975, 0.83177],161 [1.0167, 0.84767],162 [1.05, 0.86162],163 [1.0833, 0.87394],164 [1.1167, 0.88608],165 [1.1417, 0.89735],166 [1.175, 0.9077],167 [1.2083, 0.91734],168 [1.2417, 0.92633],169 [1.275, 0.93528],170 [1.3083, 0.94438],171 [1.35, 0.95087],172 [1.3833, 0.95819],173 [1.4417, 0.96961],174 [1.475, 0.97442],175 [1.5083, 0.97863],176 [1.5417, 0.98239],177 [1.575, 0.98571],178 [1.6083, 0.98903],179 [1.6417, 0.99161],180 [1.6833, 0.99381],181 [1.7167, 0.99569],182 [1.75, 0.99732],183 [1.7833, 0.99858],184 [1.8083, 0.99926],185 [1.8417, 1],186];187188/**189 * How far a row is faded, by its distance from the slot as a share of the frame.190 *191 * Read as an envelope: the brightest any pixel of an unselected row ever got at192 * each height, across all 80 frames. Taking the maximum over the whole reel is193 * what makes it a property of the *position* rather than of whichever word194 * happened to be sitting there — a word with no ascenders is dimmer at the same195 * height, and one sample would have read that as a dip in the fade.196 *197 * It does not reach zero: the column is cut off by the frame, not by the fade.198 */199export const FADE: readonly (readonly [number, number])[] = [200 [0, 1],201 [0.085, 0.97],202 [0.155, 0.89],203 [0.22, 0.744],204 [0.288, 0.518],205 [0.356, 0.357],206 [0.424, 0.25],207 [0.492, 0.185],208];209210type Table = readonly (readonly [number, number])[];211212/**213 * Linear read of a rising table, in either direction.214 *215 * `at = 0` reads y from x — the reel's travel at a second. `at = 1` reads x from216 * y — the second the reel reaches a share of its travel, which is what tells a217 * row when it takes the slot. Both tables here are sorted and monotone in both218 * columns, so one walk serves both and there is no second function to keep in219 * step with the first.220 */221export function read(table: Table, v: number, at: 0 | 1 = 0): number {222 const to = at === 0 ? 1 : 0;223 const first = table[0];224 const last = table[table.length - 1];225 if (!first || !last) return 0;226 if (v <= first[at]) return first[to];227 if (v >= last[at]) return last[to];228 for (let i = 1; i < table.length; i++) {229 const hi = table[i];230 const lo = table[i - 1];231 if (hi && lo && v <= hi[at]) {232 return lo[to] + ((hi[to] - lo[to]) * (v - lo[at])) / (hi[at] - lo[at]);233 }234 }235 return last[to];236}237238/**239 * The second the reel has travelled `p` of its distance.240 *241 * Outside the roll it answers with an infinity rather than an endpoint: a row242 * the reel never reaches must never take the slot, and a row it started past243 * must have held it from the first frame. Clamping instead would hand both of244 * those a real time and make the scene swap words it never touches.245 */246function rollReaches(p: number): number {247 const first = ROLL[0];248 const last = ROLL[ROLL.length - 1];249 if (!first || !last) return Number.POSITIVE_INFINITY;250 if (p <= first[1]) return Number.NEGATIVE_INFINITY;251 if (p > last[1]) return Number.POSITIVE_INFINITY;252 return read(ROLL, p, 1);253}254255/** The swap's curve: a moderate decelerate, clamped. */256function swell(t: number): number {257 if (t <= 0) return 0;258 if (t >= SWAP) return 1;259 const u = t / SWAP;260 return 1 - (1 - u) ** 3;261}262263export interface WordWheelProps {264 /** The part of the line that never changes. */265 headline?: string;266 /** The reel, in order. It cycles, so the column is never empty. */267 words?: string;268 /** Rows travelled. Left at the list's length the reel comes full circle. */269 spin?: number;270 theme?: Partial<KuchiTheme>;271 mode?: "light" | "dark";272 fontFamily?: string;273}274275export function WordWheel({276 headline = "kuchi for",277 words = "Launches, Founders, Indie hackers, Agencies, Designers, Startups, Dev tools, Solo builders, Changelogs",278 spin,279 theme,280 mode = "light",281 fontFamily = "Default",282}: WordWheelProps) {283 const frame = useCurrentFrame();284 const { fps, width, height } = useVideoConfig();285 const t = useKuchiTheme(theme, mode);286 const face = resolveFont(fontFamily) ?? SANS;287288 const reel = words289 .split(",")290 .map((w) => w.trim())291 .filter(Boolean);292 const rows = reel.length || 1;293 const travel = (spin ?? rows) * PITCH;294295 /**296 * One unit of reference pixel, and the offset that centres the layout.297 *298 * The scene is laid out in the recording's own pixels and scaled by height, so299 * a render at the reference's dimensions is the reference's dimensions and300 * nothing is resampled in the middle of a comparison.301 */302 const u = height / REF_H;303 const ox = (width - REF_W * u) / 2;304305 const now = frame / fps;306 const s = read(ROLL, now) * travel;307308 const first = Math.ceil((s - BASE_Y - PITCH) / PITCH);309 const last = Math.floor((s - BASE_Y + REF_H + PITCH) / PITCH);310311 const ink = t.foreground;312 const dim = t.mutedForeground;313314 return (315 <AbsoluteFill style={{ backgroundColor: t.background }}>316 <div317 style={{318 position: "absolute",319 left: ox,320 top: 0,321 width: REF_W * u,322 height: REF_H * u,323 fontFamily: face,324 fontWeight: 500,325 // Hinting re-snaps every stem as the scale slides, and the letterforms326 // boil. This turns it off — the type reads a shade softer, which is the327 // outline as it actually is.328 textRendering: "geometricPrecision",329 whiteSpace: "nowrap",330 }}331 >332 <div333 style={{334 position: "absolute",335 left: LINE_X * u,336 top: (BASE_Y - ASC) * u,337 fontSize: SIZE * u,338 lineHeight: 1,339 color: ink,340 }}341 >342 {headline}343 {/*344 A zero-sized inline-block sits on the baseline at the end of the345 lead-in's own advance, whatever that lead-in says. The reel hangs off346 it, so the sentence stays a sentence when the copy changes — and the347 anchor never needs measuring, because the browser has already done348 the layout by the time it paints.349 */}350 <span351 style={{ position: "relative", display: "inline-block", width: 0 }}352 >353 {Array.from({ length: last - first + 1 }, (_, n) => {354 const i = first + n;355 const y = BASE_Y + PITCH * i - s;356 const d = y - BASE_Y;357358 // A row owns the slot while it is nearest to it, and grows into it359 // over SWAP rather than on the frame it crosses.360 const p =361 swell(now - rollReaches((PITCH * (i - 0.5)) / travel)) -362 swell(now - rollReaches((PITCH * (i + 0.5)) / travel));363364 const scale = SMALL + (1 - SMALL) * p;365 return (366 <div367 key={i}368 style={{369 position: "absolute",370 left: (GAP + INDENT) * u,371 top: (y - ASC - BASE_Y) * u,372 color: p > 0 ? mixOklch(dim, ink, p) : dim,373 opacity: read(FADE, Math.abs(d) / REF_H),374 // The baseline is the only pivot a browser will not round:375 // it quantises a glyph's origin to a whole device pixel376 // vertically, so a scale that moves the baseline makes the377 // word climb the grid in jumps. Pivot on it and there is378 // nothing to snap.379 transform: `translateX(${-INDENT * p * u}px) scale(${scale})`,380 transformOrigin: `0px ${ASC * u}px`,381 }}382 >383 {reel[((i % rows) + rows) % rows]}384 </div>385 );386 })}387 </span>388 </div>389 </div>390 </AbsoluteFill>391 );392}









