Drop Line
Words cut past one card at a time, then the last word sinks out from under the dot of its own i
Source
components/kuchi/drop-line/index.tsx
1"use client";23import { loadFont } from "@remotion/google-fonts/Inter";4import type { ReactNode } from "react";5import { useId } from "react";6import { AbsoluteFill, useCurrentFrame, useVideoConfig } from "remotion";7import { type KuchiTheme, useKuchiTheme } from "@/lib/kuchi-ui";8import { BASELINE, DOT_LINE, DROP, FONT_SIZE, HOLDS, RISE } from "./data";910const { fontFamily: inter } = loadFont("normal", { weights: ["700"] });1112const STAGE_W = 1920;13const STAGE_H = 1080;14/** Words may fill this much of the stage width before they scale down to fit. */15const MAX_WIDTH = STAGE_W * 0.9;16const DOTTED = /[ij]/;1718/**19 * Drop Line: words cut in place, the first rising into position, and on the last20 * card the word sinks out from under its own dot. The dot holds its position, so21 * the sentence ends on one mark left hanging. A last word with no "i" or "j" just22 * drops whole.23 */24export interface DropLineProps {25 /** One word per card; the last one drops. */26 words?: string[];27 fontFamily?: string;28 fontWeight?: number;29 /** Type size on a 1920x1080 stage. */30 fontSize?: number;31 /** Size per word on that stage, for faces that set a card differently. */32 wordSize?: number[];33 /** Horizontal nudge per word, in stage px, for optical centring. */34 wordNudge?: number[];35 /** Tracking per word in em. */36 wordTracking?: number[];37 /** Per-letter offsets per word, in em. */38 wordKerning?: number[][];39 /** The card. Defaults to the theme foreground. */40 ink?: string;41 /** The type. Defaults to the theme background. */42 paper?: string;43 speed?: number;44 theme?: Partial<KuchiTheme>;45 mode?: "light" | "dark";46}4748/** Sample a measured table at a fractional frame; holds both ends. */49export const sample = (table: readonly number[], at: number) => {50 const x = Math.max(0, Math.min(at, table.length - 1));51 const i = Math.floor(x);52 const a = table[i] ?? 0;53 return a + ((table[i + 1] ?? a) - a) * (x - i);54};5556/** Frame each word cuts in, the frame the last card ends, and when its drop table starts. */57export const timeline = (count: number) => {58 const starts = Array.from({ length: count }, (_, i) =>59 i === 0 ? 0 : HOLDS.first + (i - 1) * HOLDS.word,60 );61 const end = HOLDS.first + Math.max(0, count - 2) * HOLDS.word + HOLDS.last;62 return { starts, end, drop: end - DROP.length };63};6465/** Which card is up at frame `f`. */66export const cardAt = (starts: readonly number[], f: number) =>67 starts.reduce((at, s, i) => (f >= s ? i : at), 0);6869let measureCtx: CanvasRenderingContext2D | null | undefined;7071const fontMeasure = (font: string) => {72 if (typeof document === "undefined") return null;73 if (measureCtx === undefined) {74 measureCtx = document.createElement("canvas").getContext("2d");75 }76 if (measureCtx) measureCtx.font = font;77 return measureCtx ?? null;78};7980/**81 * A line that can sit a fraction of a pixel. Chrome rounds a baseline to a whole82 * device pixel, which turns a slow drop into a staircase, so the type is painted83 * on the two rows around `y` and added by the fraction between them.84 */85const Line: React.FC<{86 y: number;87 base: number;88 px: number;89 children: ReactNode;90}> = ({ y, base, px, children }) => {91 const row = Math.floor(y * px);92 const f = y * px - row;93 return (94 <g style={{ isolation: "isolate" }}>95 {[96 { r: row, w: 1 - f },97 { r: row + 1, w: f },98 ].map(({ r, w }) => (99 <g100 key={r}101 transform={`translate(0 ${(r + 1e-3) / px - base})`}102 style={{ opacity: w, mixBlendMode: "plus-lighter" }}103 >104 {children}105 </g>106 ))}107 </g>108 );109};110111export const DropLine: React.FC<DropLineProps> = ({112 words = ["then", "ships", "the", "film"],113 fontFamily = inter,114 fontWeight = 700,115 fontSize = FONT_SIZE,116 wordSize,117 wordNudge,118 wordTracking,119 wordKerning,120 ink,121 paper,122 speed = 1,123 theme,124 mode,125}) => {126 const t = useKuchiTheme(theme, mode);127 const uid = useId().replace(/:/g, "");128 const f = useCurrentFrame() * speed;129 const { width, height } = useVideoConfig();130 const px = Math.max(width / STAGE_W, height / STAGE_H);131 const card = ink ?? t.foreground;132 const type = paper ?? t.background;133134 const line = timeline(words.length);135 const index = cardAt(line.starts, f);136 const word = words[index] ?? "";137 const isLast = index === words.length - 1;138 const chars = Array.from(word);139 const tracking = wordTracking?.[index] ?? -0.015;140141 const wanted = wordSize?.[index] ?? fontSize;142 const ctx = fontMeasure(`${fontWeight} ${wanted}px ${fontFamily}`);143 const measured =144 (ctx ? ctx.measureText(word).width : chars.length * wanted * 0.6) +145 (chars.length - 1) * tracking * wanted;146 const fit = Math.min(1, MAX_WIDTH / Math.max(1, measured));147 const size = wanted * fit;148149 // Keep the type centred on the stage as `fontSize` moves away from the measured one.150 const rest = STAGE_H / 2 + (BASELINE - STAGE_H / 2) * (fontSize / FONT_SIZE);151 const fall = isLast ? sample(DROP, f - line.drop) : 0;152 const y = rest + (index === 0 ? sample(RISE, f) : 0);153154 // The cut between a dot and its stem: just above the x-height, on any face.155 const dots = isLast && DOTTED.test(word);156 const xHeight = ctx?.measureText("x").actualBoundingBoxAscent;157 const dotTop = ctx?.measureText("i").actualBoundingBoxAscent;158 const dotLine =159 xHeight && dotTop160 ? rest - (xHeight + (dotTop - xHeight) * 0.15) * fit161 : rest - DOT_LINE * size;162163 const letters = (show: (ch: string) => boolean) => (164 <text165 x={STAGE_W / 2 + (wordNudge?.[index] ?? 0)}166 y={rest}167 textAnchor="middle"168 fill={type}169 style={{170 fontFamily,171 fontWeight,172 fontSize: size,173 letterSpacing: `${tracking}em`,174 textRendering: "geometricPrecision",175 }}176 >177 {chars.map((ch, i) => (178 <tspan179 // biome-ignore lint/suspicious/noArrayIndexKey: letters of a fixed string180 key={i}181 dx={`${wordKerning?.[index]?.[i] ?? 0}em`}182 fillOpacity={show(ch) ? 1 : 0}183 >184 {ch}185 </tspan>186 ))}187 </text>188 );189190 return (191 <AbsoluteFill style={{ background: card, overflow: "hidden" }}>192 <svg193 viewBox={`0 0 ${STAGE_W} ${STAGE_H}`}194 width="100%"195 height="100%"196 preserveAspectRatio="xMidYMid slice"197 style={{ position: "absolute", inset: 0 }}198 >199 <title>{words.join(" ")}</title>200 <defs>201 <clipPath id={`${uid}a`}>202 <rect width={STAGE_W} height={dotLine} />203 </clipPath>204 <clipPath id={`${uid}b`}>205 <rect y={dotLine} width={STAGE_W} height={STAGE_H * 2} />206 </clipPath>207 </defs>208 <Line y={y + fall} base={rest} px={px}>209 {dots ? (210 <>211 {letters((ch) => !DOTTED.test(ch))}212 <g clipPath={`url(#${uid}b)`}>213 {letters((ch) => DOTTED.test(ch))}214 </g>215 </>216 ) : (217 letters(() => true)218 )}219 </Line>220 {dots && (221 <Line y={y} base={rest} px={px}>222 <g clipPath={`url(#${uid}a)`}>{letters((ch) => DOTTED.test(ch))}</g>223 </Line>224 )}225 </svg>226 </AbsoluteFill>227 );228};229230export default DropLine;
components/kuchi/drop-line/data.ts
1/**2 * Every number here was measured off a 1920x1080 reference, one sample per frame.3 * Sizes and offsets are px on that stage.4 */56export const FONT_SIZE = 130;7/** Where the type sits at rest. */8export const BASELINE = 574.5;910/** Frames each card holds: the first, every word between, the last. */11export const HOLDS = { first: 9, word: 7, last: 15 } as const;1213/** The first word lands low and rises into place: px below rest, per frame. */14export const RISE = [70, 43, 22.8, 13, 7.2, 3.7, 1.55, 0.45, 0] as const;1516/** The last card, frame by frame: how far the word has sunk. The dot stays put. */17export const DROP = [18 0, 0, 0, 0, 0.38, 1.25, 2.54, 4.4, 6.91, 10.27, 14.57, 20.23, 27.57, 37.53,19 51.55,20] as const;2122/** Between the x-height and the dot, in em above the baseline, where no face is told. */23export const DOT_LINE = 0.585;









