Title Sweep
An end title lit by a gradient that travels through the letters, cuts in the dark, and a second title takes over
Source
components/kuchi/title-sweep/index.tsx
1"use client";23import { loadFont } from "@remotion/google-fonts/Inter";4import { useEffect, useId, useMemo, useState } from "react";5import {6 AbsoluteFill,7 continueRender,8 delayRender,9 getRemotionEnvironment,10 useCurrentFrame,11 useVideoConfig,12} from "remotion";13import { resolveFont } from "@/lib/kuchi-ui";14import { DEFAULT_STOPS, SWEEP_FIRST, SWEEP_LAST, type SweepStop } from "./data";1516/**17 * Title Sweep: a centred title lit by a warm gradient that travels across it.18 * The type never fades. It arrives and leaves because the gradient's dark ends19 * pass over it, so a title goes dark from one side and the next one is cut in20 * while the frame is black. The whole shot pulls back on one slow clock.21 *22 * The defaults are measured off a 1920x1080 reference at 30fps. The sweep is one23 * rigid gradient moved by a measured table: fast at both ends, slow through the24 * pale band.25 */2627const { fontFamily: INTER } = loadFont("normal", {28 weights: ["300", "400", "500", "600", "700", "800"],29 subsets: ["latin"],30});3132const STAGE_W = 1920;33const STAGE_H = 1080;34/** Widest a title may get, as a fraction of the stage. */35const MAX_LINE = 0.9;36/** A title longer than this many em stretches the sweep so it still crosses it. */37const REF_LINE = 7.6;38/** Frames between one title's cut and the next. */39export const TITLE_FRAMES = 36;40/** Scale lost per frame by the pull-back. */41const PULL_BACK = 0.00119;42/** The text baseline sits this far below the stage centre, em. */43const BASELINE = 0.267;4445export interface TitleSweepProps {46 /** Shown in order, each cut in on black. One title is fine. */47 titles?: string[];48 /** A family from `fonts.ts`, or a CSS family you have loaded yourself. */49 fontFamily?: string;50 /** Type size, px on a 1920 stage. Long copy shrinks from here to fit. */51 fontSize?: number;52 fontWeight?: number;53 /** Letter spacing, em. */54 tracking?: number;55 /** Extra space between words, em. */56 wordSpacing?: number;57 /** Horizontal offset per title, px on a 1920 stage. */58 nudge?: number[];59 /** Size multiplier per title, for a line set a touch larger than the last. */60 titleScale?: number[];61 /** The gradient, in em from its centre. Start and end it on `background`. */62 stops?: SweepStop[];63 background?: string;64 speed?: number;65}6667/** Piecewise-linear lookup in a per-frame table, held at the ends. */68export const sample = (table: readonly number[], at: number) => {69 const i = Math.min(Math.max(Math.floor(at), 0), table.length - 1);70 const from = table[i] ?? 0;71 const to = table[i + 1] ?? from;72 return from + (to - from) * Math.min(1, Math.max(0, at - i));73};7475/** Which title is on screen at `f`, and how long it has been up. */76export const titleAt = (f: number, count: number) => {77 const index = Math.min(78 Math.floor(Math.max(0, f) / TITLE_FRAMES),79 Math.max(0, count - 1),80 );81 return { index, age: Math.max(0, f) - index * TITLE_FRAMES };82};8384/** The last of several titles holds warm; any other title is swept off. */85export const sweepFor = (index: number, count: number) =>86 count > 1 && index === count - 1 ? SWEEP_LAST : SWEEP_FIRST;8788/** Scale of the slow pull-back at `f`. */89export const pullBack = (f: number) => Math.max(0.5, 1 - PULL_BACK * f);9091/** Scale that keeps a line of `lineWidth` on a stage of `stageWidth`. */92export const fitLine = (lineWidth: number, stageWidth: number) =>93 Math.min(1, (MAX_LINE * stageWidth) / Math.max(1, lineWidth));9495export const TitleSweep = ({96 titles = ["kuchi Pro", "kuchi.kalarislabs.com"],97 fontFamily = "Inter",98 fontSize = 110.96,99 fontWeight = 500,100 tracking = -0.0165,101 wordSpacing = -0.0769,102 nudge,103 titleScale,104 stops = DEFAULT_STOPS,105 background = "#000000",106 speed = 1,107}: TitleSweepProps) => {108 const f = useCurrentFrame() * speed;109 const { width, height } = useVideoConfig();110 const id = useId().replace(/:/g, "");111 const family = resolveFont(fontFamily) ?? INTER;112113 // A width measured before the face lands is a width for the fallback, so the114 // fit waits on it; a render holds its first frame until that has re-rendered.115 const [fontTick, setFontTick] = useState(0);116 const [handle] = useState(() =>117 getRemotionEnvironment().isRendering118 ? delayRender("title-sweep: loading the face")119 : null,120 );121 const fontSpec = `${fontWeight} 100px ${family}`;122 useEffect(() => {123 let alive = true;124 document.fonts125 .load(fontSpec)126 .catch(() => [])127 .then(() => {128 if (alive) setFontTick((n) => n + 1);129 });130 return () => {131 alive = false;132 };133 }, [fontSpec]);134 useEffect(() => {135 if (fontTick > 0 && handle != null) continueRender(handle);136 }, [fontTick, handle]);137138 const { index, age } = titleAt(f, titles.length);139 const text = titles[index] ?? "";140141 // Advance width in em, tracking and word spacing included.142 const lineEm = useMemo(() => {143 void fontTick;144 const ctx =145 typeof document === "undefined"146 ? null147 : document.createElement("canvas").getContext("2d");148 if (!ctx) return text.length * 0.55;149 ctx.font = fontSpec;150 const spaces = text.split(" ").length - 1;151 return (152 ctx.measureText(text).width / 100 +153 text.length * tracking +154 spaces * wordSpacing155 );156 }, [fontTick, fontSpec, text, tracking, wordSpacing]);157158 const fit = fitLine(lineEm * fontSize, STAGE_W);159 const size = fontSize * fit * (titleScale?.[index] ?? 1);160 const reach = Math.max(1, lineEm / REF_LINE) * size;161 const cx = STAGE_W / 2 + (nudge?.[index] ?? 0);162 const centre =163 STAGE_W / 2 + sample(sweepFor(index, titles.length), age) * reach;164 const first = stops[0]?.at ?? 0;165 const span = (stops[stops.length - 1]?.at ?? 1) - first || 1;166 const zoom = pullBack(f);167 const paint = `${id}sweep`;168169 return (170 <AbsoluteFill style={{ background }}>171 <svg172 viewBox={`0 0 ${STAGE_W} ${STAGE_H}`}173 width={width}174 height={height}175 style={{ position: "absolute", inset: 0, overflow: "hidden" }}176 >177 <defs>178 <linearGradient179 id={paint}180 gradientUnits="userSpaceOnUse"181 x1={centre + first * reach}182 x2={centre + (first + span) * reach}183 y1={0}184 y2={0}185 >186 {stops.map((s) => (187 <stop188 key={s.at}189 offset={(s.at - first) / span}190 stopColor={s.color}191 />192 ))}193 </linearGradient>194 </defs>195 <g transform={`translate(960 540) scale(${zoom}) translate(-960 -540)`}>196 <text197 x={cx}198 y={STAGE_H / 2 + BASELINE * size}199 textAnchor="middle"200 fill={`url(#${paint})`}201 style={{202 fontFamily: family,203 fontWeight,204 fontSize: size,205 letterSpacing: `${tracking}em`,206 wordSpacing: `${wordSpacing}em`,207 textRendering: "geometricPrecision",208 }}209 >210 {text}211 </text>212 </g>213 </svg>214 </AbsoluteFill>215 );216};217218export default TitleSweep;
components/kuchi/title-sweep/data.ts
1export interface SweepStop {2 /** Position in em from the gradient's centre. Positive leads the sweep. */3 at: number;4 /** A concrete colour: this paints in a render. */5 color: string;6}78/** Measured off the reference: black, deep red, orange, a pale band, and back. */9export const DEFAULT_STOPS: SweepStop[] = [10 { at: -17.52, color: "#000000" },11 { at: -16.727, color: "#030200" },12 { at: -15.934, color: "#0d0600" },13 { at: -15.141, color: "#160a00" },14 { at: -14.348, color: "#210c01" },15 { at: -13.482, color: "#2d1001" },16 { at: -12.689, color: "#431502" },17 { at: -11.896, color: "#581902" },18 { at: -11.103, color: "#631d08" },19 { at: -10.31, color: "#88300f" },20 { at: -9.517, color: "#9e3f18" },21 { at: -8.724, color: "#ce5c22" },22 { at: -7.931, color: "#d86629" },23 { at: -7.066, color: "#eb7535" },24 { at: -6.273, color: "#e3793d" },25 { at: -5.479, color: "#db7f47" },26 { at: -4.686, color: "#e6935b" },27 { at: -3.893, color: "#f0a771" },28 { at: -3.1, color: "#f4b784" },29 { at: -2.307, color: "#f1bd8d" },30 { at: -1.514, color: "#eebd8f" },31 { at: -0.721, color: "#e3ae7f" },32 { at: 0.144, color: "#e4a471" },33 { at: 0.937, color: "#ef9c62" },34 { at: 1.73, color: "#eb8c4f" },35 { at: 2.523, color: "#e77f42" },36 { at: 3.317, color: "#e37537" },37 { at: 4.11, color: "#dc6d31" },38 { at: 4.903, color: "#d5652b" },39 { at: 5.696, color: "#d16127" },40 { at: 6.489, color: "#af4c1e" },41 { at: 7.354, color: "#943917" },42 { at: 8.147, color: "#7a2810" },43 { at: 8.94, color: "#671e09" },44 { at: 9.733, color: "#561905" },45 { at: 10.526, color: "#461604" },46 { at: 11.319, color: "#3d1404" },47 { at: 12.112, color: "#2a0f02" },48 { at: 12.906, color: "#1f0d01" },49 { at: 13.771, color: "#130901" },50 { at: 14.564, color: "#0d0601" },51 { at: 15.357, color: "#070400" },52 { at: 16.15, color: "#030100" },53 { at: 16.943, color: "#000000" },54];5556/** Gradient centre per frame of a title, in em from the stage centre. Swept off. */57export const SWEEP_FIRST = [58 -6.625, -6.007, -5.192, -4.403, -3.78, -3.342, -2.969, -2.554, -2.033, -1.439,59 -0.862, -0.383, -0.01, 0.344, 0.662, 0.895, 1.061, 1.148, 1.198, 1.326, 1.55,60 1.781, 2.079, 2.506, 3.078, 3.821, 4.705, 5.657, 6.652, 7.781, 9.099, 10.546,61 12.024, 13.444, 14.647, 15.396,62];6364/** The same for the last of several titles, which never leaves. */65export const SWEEP_LAST = [66 -17.907, -17.152, -16.058, -14.785, -13.36, -11.904, -10.494, -9.072, -7.668,67 -6.398, -5.281, -4.343, -3.632, -3.026, -2.412, -1.792, -1.193, -0.646,68 -0.196, 0.192, 0.518, 0.747, 0.918, 1.008, 1.048, 1.148, 1.359, 1.621, 1.879,69 2.148, 2.495, 2.889, 3.31, 3.745, 4.168, 4.589, 5.014, 5.441, 5.859, 6.193,70];









