Font Shuffle
Launch title animation: the line decodes out of scrambled glyphs set in the wrong typeface, flashes the accent, settles to ink, then the product name cycles through eleven typefaces (mono, light, humanist, pixel, script, slab, condensed, italic, blackletter, wide, bold grotesk) while the line zooms a step closer on each swap. Every beat is a held state measured off a recording frame by frame — timing, masks, layout, zoom and colour.
Source
components/kuchi/font-shuffle.tsx
1"use client";23import {4 type CSSProperties,5 type ReactNode,6 type RefObject,7 useLayoutEffect,8 useRef,9 useState,10} from "react";11import {12 AbsoluteFill,13 continueRender,14 delayRender,15 useCurrentFrame,16} from "remotion";17import { type KuchiTheme, useKuchiTheme } from "@/lib/kuchi-ui";1819/**20 * A launch title that decodes and then can't decide what it's wearing.21 * Scrambled glyphs from the wrong typeface flicker into place, the line22 * lands in the accent, settles to ink and holds, then the second half23 * cycles through a run of typefaces while the whole line steps closer.24 *25 * ## Everything here was measured off a recording, frame by frame26 *27 * A 2.03s, 638 × 358 screen capture (68 frames, 60fps then 30fps) was28 * decoded in BT.709 and every state was fitted by rendering candidate type in29 * Chrome, box-downsampling from 4×, blurring by the capture's own σ 0.65px30 * (measured on the pixel face's square edges) and least-squaring against it:31 *32 * - the capture is a sequence of HELD states, not tweens. Every change lands33 * on a 30fps frame boundary, and between changes consecutive frames34 * differ only by codec noise. `BEATS` below is that timeline.35 * - the decode is the real line set in a Baskerville, with a per-character36 * visibility mask and oversized foreign glyphs (`b`, `e`, `a` …) that37 * persist from one beat to the next. Masks and extras were read glyph by38 * glyph and re-fitted for position, size and weight.39 * - the purple is one colour, `#9b8ed3`, measured on both purple beats.40 * - the line is centred (x ≈ 317.5 on every beat, the capture is 1.5px off41 * centre) and every font swap is also a zoom about the frame's middle:42 * ×1.028, 1.086, 1.111, 1.152, 1.249 on the serif's own size.43 *44 * ## The type is the closest free match, not the original45 *46 * The recording's faces are not on Google Fonts. Each was fingerprinted47 * against every Google family in its category and the best free match taken:48 * Source Serif 4 (opsz 16) for the line, Libre Baskerville for the decode,49 * Fragment Mono, Hind, Coral Pixels and Mona Sans for the swaps. Geometry and50 * timing are the recording's; glyph shapes are as close as free type gets.51 * The six faces between the pixel and the grotesk are an extension, not52 * measured: Pacifico, Zilla Slab, Bebas Neue, Playfair Display italic,53 * UnifrakturMaguntia and Unbounded.54 */5556/* ─────────────────────────────────────────────────────────────────────────57 The scene, in the reference's own 638 × 358 pixels and 30fps frames58 ───────────────────────────────────────────────────────────────────────── */5960export const REF_W = 638;61export const REF_H = 358;6263const CDN = "https://cdn.jsdelivr.net/npm";6465/** Every face the scene draws in, loaded as one `FontFace` each. */66export const FACES = {67 serif: {68 family: "Font Shuffle Serif",69 src: `${CDN}/@fontsource-variable/source-serif-4@5.3.0/files/source-serif-4-latin-opsz-normal.woff2`,70 weight: "200 900",71 settings: "\"opsz\" 16",72 },73 glitch: {74 family: "Font Shuffle Glitch",75 src: `${CDN}/@fontsource-variable/libre-baskerville@5.3.0/files/libre-baskerville-latin-wght-normal.woff2`,76 weight: "400 700",77 },78 mono: {79 family: "Font Shuffle Mono",80 src: `${CDN}/@fontsource-variable/source-code-pro@5.3.0/files/source-code-pro-latin-wght-normal.woff2`,81 weight: "200 900",82 },83 light: {84 family: "Font Shuffle Light",85 src: `${CDN}/@fontsource-variable/inter@5.3.0/files/inter-latin-wght-normal.woff2`,86 weight: "100 900",87 },88 humanist: {89 family: "Font Shuffle Humanist",90 src: `${CDN}/@fontsource/hind@5.3.0/files/hind-latin-400-normal.woff2`,91 weight: "400",92 },93 pixel: {94 family: "Font Shuffle Pixel",95 src: `${CDN}/@fontsource/vt323@5.3.0/files/vt323-latin-400-normal.woff2`,96 weight: "400",97 },98 grotesk: {99 family: "Font Shuffle Grotesk",100 src: `${CDN}/@fontsource-variable/mona-sans@5.3.0/files/mona-sans-latin-wght-normal.woff2`,101 weight: "200 900",102 },103 // The run added after the recording's five: script, slab, condensed, italic,104 // blackletter and wide. Each one is as far from its neighbours as type gets.105 script: {106 family: "Font Shuffle Script",107 src: `${CDN}/@fontsource/pacifico@5.2.5/files/pacifico-latin-400-normal.woff2`,108 weight: "400",109 },110 slab: {111 family: "Font Shuffle Slab",112 src: `${CDN}/@fontsource/zilla-slab@5.2.5/files/zilla-slab-latin-600-normal.woff2`,113 weight: "600",114 },115 condensed: {116 family: "Font Shuffle Condensed",117 src: `${CDN}/@fontsource/bebas-neue@5.2.5/files/bebas-neue-latin-400-normal.woff2`,118 weight: "400",119 },120 italic: {121 family: "Font Shuffle Italic",122 src: `${CDN}/@fontsource-variable/playfair-display@5.2.5/files/playfair-display-latin-wght-italic.woff2`,123 weight: "400 900",124 },125 blackletter: {126 family: "Font Shuffle Blackletter",127 src: `${CDN}/@fontsource/unifrakturmaguntia@5.2.5/files/unifrakturmaguntia-latin-400-normal.woff2`,128 weight: "400",129 },130 wide: {131 family: "Font Shuffle Wide",132 src: `${CDN}/@fontsource-variable/unbounded@5.2.5/files/unbounded-latin-wght-normal.woff2`,133 weight: "200 900",134 },135} as const;136137export type FaceKey = keyof typeof FACES;138139/** One run of type: face, size and weight in reference px, tracking, word gap. */140export interface Run {141 face: FaceKey;142 size: number;143 weight: number;144 /** letter-spacing, px */145 track: number;146 /** word-spacing added to the face's own space, px */147 space?: number;148}149150/** A foreign glyph laid over the decode, anchored to a character of the line. */151export interface Extra {152 ch: string;153 /** Index into the reference line it sits on. */154 at: number;155 /** Offset from that character's start, px. */156 dx: number;157 base: number;158 size: number;159 weight: number;160}161162export type Beat =163 | {164 kind: "glitch";165 run: Run;166 /** Centre of the line's advance box. */167 cx: number;168 base: number;169 /** Indices of the reference line that are drawn. */170 show: number[];171 extras: Extra[];172 }173 | {174 kind: "pair";175 lead: Run;176 subject: Run;177 leadBase: number;178 subjectBase: number;179 /** Between the end of the lead's advance and the subject's start. */180 gap: number;181 cx: number;182 accent: boolean;183 /** Indices of `lead + " " + subject` that are held back (drawn clear). */184 hide?: number[];185 };186187/** The reference line the masks and anchors were read on: 24 characters. */188const REF_LEN = 24;189190/** The frame each beat starts on (30fps), and the beat. Read off the capture. */191export const BEATS: [number, Beat][] = [192 [193 0,194 {195 kind: "glitch",196 run: { face: "glitch", size: 28.25, weight: 416, track: -0.94 },197 cx: 317.597,198 base: 186.43,199 show: [2, 4, 7, 10, 14, 17, 19, 22],200 extras: [201 { ch: "i", at: 1, dx: -2.643, base: 189.25, size: 33.95, weight: 700 },202 { ch: "l", at: 1, dx: 7.527, base: 189.55, size: 35.55, weight: 691 },203 { ch: "e", at: 5, dx: 4.176, base: 189.59, size: 36.56, weight: 670 },204 { ch: "b", at: 11, dx: -1.19, base: 189.73, size: 36.46, weight: 400 },205 { ch: "a", at: 20, dx: -3.586, base: 190.12, size: 35.92, weight: 641 },206 { ch: "e", at: 23, dx: 22.728, base: 189.5, size: 36.53, weight: 673 },207 ],208 },209 ],210 [211 2,212 {213 kind: "glitch",214 run: { face: "glitch", size: 28.8, weight: 401, track: -0.12 },215 cx: 317.722,216 base: 186.78,217 show: [0, 1, 2, 4, 7, 9, 10, 13, 14, 15, 17, 19, 22, 23],218 extras: [219 { ch: "b", at: 11, dx: -1.886, base: 191.01, size: 40.27, weight: 523 },220 { ch: "e", at: 23, dx: 26.711, base: 190.38, size: 39.71, weight: 635 },221 ],222 },223 ],224 [225 4,226 {227 kind: "pair",228 lead: { face: "serif", size: 38.31, weight: 489, track: -1.144 },229 subject: { face: "serif", size: 38.31, weight: 489, track: -1.144 },230 leadBase: 188.26,231 subjectBase: 188.26,232 gap: 7.719,233 cx: 317.543,234 accent: true,235 hide: [3, 6, 16],236 },237 ],238 [239 6,240 {241 kind: "pair",242 lead: { face: "serif", size: 41.98, weight: 502, track: -1.325 },243 subject: { face: "serif", size: 41.98, weight: 502, track: -1.325 },244 leadBase: 189.35,245 subjectBase: 189.35,246 gap: 8.375,247 cx: 317.475,248 accent: true,249 },250 ],251 [252 7,253 {254 kind: "pair",255 lead: { face: "serif", size: 42, weight: 573, track: -1.4 },256 subject: { face: "serif", size: 42, weight: 573, track: -1.4, space: 1.263 },257 leadBase: 189.29,258 subjectBase: 189.29,259 gap: 9.951,260 cx: 317.943,261 accent: false,262 },263 ],264 [265 32,266 {267 kind: "pair",268 lead: { face: "serif", size: 43.48, weight: 511, track: -1.273 },269 subject: { face: "mono", size: 39.96, weight: 354, track: -2.741, space: 0.765 },270 leadBase: 189.603,271 subjectBase: 189.296,272 gap: 6.927,273 cx: 317.275,274 accent: false,275 },276 ],277 [278 35,279 {280 kind: "pair",281 lead: { face: "serif", size: 45.898, weight: 546, track: -1.673 },282 subject: { face: "light", size: 42.488, weight: 392, track: -0.819, space: 5.88 },283 leadBase: 190.225,284 subjectBase: 189.492,285 gap: 11.558,286 cx: 316.424,287 accent: false,288 },289 ],290 [291 39,292 {293 kind: "pair",294 lead: { face: "serif", size: 46.968, weight: 509, track: -1.21 },295 subject: { face: "humanist", size: 46.707, weight: 400, track: 0.936, space: -0.358 },296 leadBase: 190.563,297 subjectBase: 190.696,298 gap: 12.875,299 cx: 315.741,300 accent: false,301 },302 ],303 [304 43,305 {306 kind: "pair",307 lead: { face: "serif", size: 48.696, weight: 518, track: -1.197 },308 subject: { face: "pixel", size: 67.469, weight: 400, track: -2.468, space: -4.337 },309 leadBase: 190.919,310 subjectBase: 191.087,311 gap: 5.67,312 cx: 316.477,313 accent: false,314 },315 ],316 [317 47,318 {319 kind: "pair",320 lead: { face: "serif", size: 49.285, weight: 524, track: -1.26 },321 subject: { face: "script", size: 45.342, weight: 400, track: 0.0 },322 leadBase: 191.071,323 subjectBase: 191.071,324 gap: 11,325 cx: 316.4,326 accent: false,327 },328 ],329 [330 51,331 {332 kind: "pair",333 lead: { face: "serif", size: 49.875, weight: 530, track: -1.32 },334 subject: { face: "slab", size: 53.865, weight: 600, track: -0.8 },335 leadBase: 191.223,336 subjectBase: 191.223,337 gap: 11,338 cx: 316.4,339 accent: false,340 },341 ],342 [343 54,344 {345 kind: "pair",346 lead: { face: "serif", size: 50.464, weight: 536, track: -1.38 },347 subject: { face: "condensed", size: 60.557, weight: 400, track: 0.6 },348 leadBase: 191.375,349 subjectBase: 191.375,350 gap: 11,351 cx: 316.4,352 accent: false,353 },354 ],355 [356 58,357 {358 kind: "pair",359 lead: { face: "serif", size: 51.054, weight: 541, track: -1.44 },360 subject: { face: "italic", size: 53.096, weight: 560, track: -0.8 },361 leadBase: 191.528,362 subjectBase: 191.528,363 gap: 11,364 cx: 316.4,365 accent: false,366 },367 ],368 [369 61,370 {371 kind: "pair",372 lead: { face: "serif", size: 51.643, weight: 547, track: -1.5 },373 subject: { face: "blackletter", size: 59.389, weight: 400, track: 0.0 },374 leadBase: 191.68,375 subjectBase: 191.68,376 gap: 11,377 cx: 316.4,378 accent: false,379 },380 ],381 [382 65,383 {384 kind: "pair",385 lead: { face: "serif", size: 52.233, weight: 553, track: -1.56 },386 subject: { face: "wide", size: 41.786, weight: 520, track: -1.4 },387 leadBase: 191.832,388 subjectBase: 191.832,389 gap: 11,390 cx: 316.4,391 accent: false,392 },393 ],394 [395 69,396 {397 kind: "pair",398 lead: { face: "serif", size: 52.822, weight: 559, track: -1.594 },399 subject: { face: "grotesk", size: 51.994, weight: 575, track: -1.689, space: 0.327 },400 leadBase: 191.984,401 subjectBase: 192.782,402 gap: 11.638,403 cx: 316.37,404 accent: false,405 },406 ],407];408409/**410 * Frames 0–43 are the recording's, with its 1.2s hold before the first swap411 * cut to 0.8s; 47–65 are six more faces at the same three-to-four-frame412 * cadence and the same zoom per step (lead size and baseline interpolated413 * between the pixel and grotesk beats). The grotesk is still the landing,414 * held 1.5s.415 */416export const SCENE_FRAMES = 115;417418/**419 * A reference index for character `i` of an `n`-character line. The masks and420 * anchors were read on the 24-character original; a longer or shorter line421 * samples them proportionally, so the decode keeps its density and rhythm.422 */423function refIndex(i: number, n: number): number {424 return n <= 1 ? 0 : Math.round((i * (REF_LEN - 1)) / (n - 1));425}426function ownIndex(r: number, n: number): number {427 return n <= 1 ? 0 : Math.round((r * (n - 1)) / (REF_LEN - 1));428}429430/* ─────────────────────────────────────────────────────────────────────────431 Fonts and measurement432 ───────────────────────────────────────────────────────────────────────── */433434let facesLoad: Promise<unknown> | null = null;435function loadFaces(): Promise<unknown> {436 if (facesLoad) return facesLoad;437 if (typeof FontFace === "undefined") {438 facesLoad = Promise.resolve();439 return facesLoad;440 }441 facesLoad = Promise.all(442 // `keys().map`, not `Object.values` (ES2017): stock Remotion types ES2015.443 (Object.keys(FACES) as (keyof typeof FACES)[])444 .map((k) => FACES[k])445 .filter((f) => f.src)446 .map((f) => {447 const face = new FontFace(f.family, `url(${f.src}) format("woff2")`, {448 weight: f.weight,449 });450 document.fonts.add(face);451 // A face that fails to load falls back to the next in the stack —452 // the scene still plays, in the wrong clothes.453 return face.load().catch(() => undefined);454 }),455 );456 return facesLoad;457}458459function runStyle(run: Run): CSSProperties {460 const face = FACES[run.face];461 return {462 fontFamily: `"${face.family}", serif`,463 fontSize: run.size,464 fontWeight: run.weight,465 letterSpacing: run.track,466 wordSpacing: run.space ?? 0,467 fontVariationSettings: "settings" in face ? face.settings : undefined,468 whiteSpace: "pre",469 // Unhinted outlines: a held state that re-snaps its stems at another470 // render size is a different picture.471 textRendering: "geometricPrecision",472 };473}474475interface Measured {476 /** Per beat: [lead width, subject width] or [line width, char starts…]. */477 widths: number[][];478}479480/**481 * Every width the layout needs, read once off hidden SVG text in the scene's482 * own units. Canvas cannot pin `opsz`, so it would measure a different cut.483 */484function useMeasure(485 lead: string,486 subject: string,487): [Measured | null, RefObject<SVGGElement | null>] {488 const probe = useRef<SVGGElement>(null);489 const [m, setM] = useState<Measured | null>(null);490 const [handle] = useState(() => delayRender("font-shuffle: measure"));491 useLayoutEffect(() => {492 let live = true;493 loadFaces()494 .then(() => document.fonts.ready)495 .then(() => {496 if (!live || !probe.current) return;497 const groups = probe.current.querySelectorAll<SVGGElement>("g[data-beat]");498 const widths = Array.from(groups, (g) => {499 const texts = g.querySelectorAll("text");500 if (g.dataset.beat === "glitch") {501 const t = texts[0]!;502 const n = t.getNumberOfChars();503 const starts: number[] = [];504 for (let i = 0; i < n; i++) starts.push(t.getStartPositionOfChar(i).x);505 return [t.getComputedTextLength(), ...starts];506 }507 return Array.from(texts, (t) => t.getComputedTextLength());508 });509 setM({ widths });510 continueRender(handle);511 });512 return () => {513 live = false;514 };515 }, [lead, subject, handle]);516 return [m, probe];517}518519/* ─────────────────────────────────────────────────────────────────────────520 The component521 ───────────────────────────────────────────────────────────────────────── */522523export interface FontShuffleProps {524 /** The fixed half of the line, set in the serif throughout. */525 lead?: string;526 /** The half that tries on typefaces. */527 subject?: string;528 /** The colour the line lands in before it settles to ink. */529 accent?: string;530 theme?: Partial<KuchiTheme>;531 mode?: "light" | "dark";532}533534export function FontShuffle({535 lead = "Introducing",536 subject = "kuchi Pro",537 accent = "#9b8ed3",538 theme,539 mode = "light",540}: FontShuffleProps) {541 const frame = useCurrentFrame();542 const t = useKuchiTheme(theme, mode);543 const line = `${lead} ${subject}`;544 const n = line.length;545 const [m, probe] = useMeasure(lead, subject);546547 // The last beat already reached. A loop, not `findLastIndex` (ES2023).548 let index = -1;549 for (let i = BEATS.length - 1; i >= 0; i--) {550 if (frame >= BEATS[i]![0]) {551 index = i;552 break;553 }554 }555 const beat = BEATS[Math.max(0, index)]![1];556 const w = m?.widths[Math.max(0, index)];557558 let content: ReactNode = null;559 if (w && beat.kind === "glitch") {560 const [width, ...starts] = w;561 const x0 = beat.cx - width! / 2;562 const shown = new Set(beat.show);563 content = (564 <g fill={t.foreground}>565 <text x={x0} y={beat.base} style={runStyle(beat.run)}>566 {[...line].map((ch, i) => (567 <tspan key={i} fillOpacity={shown.has(refIndex(i, n)) ? 1 : 0}>568 {ch}569 </tspan>570 ))}571 </text>572 {beat.extras.map((e, k) => {573 const at = Math.min(n - 1, ownIndex(e.at, n));574 return (575 <text576 key={k}577 x={x0 + (starts[at] ?? 0) + e.dx}578 y={e.base}579 style={runStyle({ ...beat.run, size: e.size, weight: e.weight, track: 0 })}580 >581 {e.ch}582 </text>583 );584 })}585 </g>586 );587 } else if (w && beat.kind === "pair") {588 const [wl = 0, ws = 0] = w;589 const x0 = beat.cx - (wl + beat.gap + ws) / 2;590 const hidden = new Set(beat.hide ?? []);591 const chars = (text: string, offset: number) =>592 [...text].map((ch, i) => (593 <tspan594 key={i}595 fillOpacity={hidden.has(refIndex(offset + i, n)) ? 0 : 1}596 >597 {ch}598 </tspan>599 ));600 content = (601 <g fill={beat.accent ? accent : t.foreground}>602 <text x={x0} y={beat.leadBase} style={runStyle(beat.lead)}>603 {chars(lead, 0)}604 </text>605 <text606 x={x0 + wl + beat.gap}607 y={beat.subjectBase}608 style={runStyle(beat.subject)}609 >610 {chars(subject, lead.length + 1)}611 </text>612 </g>613 );614 }615616 return (617 <AbsoluteFill style={{ backgroundColor: t.background }}>618 <svg619 viewBox={`0 0 ${REF_W} ${REF_H}`}620 preserveAspectRatio="xMidYMid meet"621 style={{ width: "100%", height: "100%", overflow: "visible" }}622 aria-label={line}623 role="img"624 >625 {content}626 <g ref={probe} opacity={0} aria-hidden>627 {BEATS.map(([at, b]) =>628 b.kind === "glitch" ? (629 <g key={at} data-beat="glitch">630 <text style={runStyle(b.run)}>{line}</text>631 </g>632 ) : (633 <g key={at} data-beat="pair">634 <text style={runStyle(b.lead)}>{lead}</text>635 <text style={runStyle(b.subject)}>{subject}</text>636 </g>637 ),638 )}639 </g>640 </svg>641 </AbsoluteFill>642 );643}









