Text

Title Sweep

An end title lit by a gradient that travels through the letters, cuts in the dark, and a second title takes over

Title Sweep preview, rendered with Remotion.

Source

components/kuchi/title-sweep/index.tsx
"use client";
import { loadFont } from "@remotion/google-fonts/Inter";
import { useEffect, useId, useMemo, useState } from "react";
import {
AbsoluteFill,
continueRender,
delayRender,
getRemotionEnvironment,
useCurrentFrame,
useVideoConfig,
} from "remotion";
import { resolveFont } from "@/lib/kuchi-ui";
import { DEFAULT_STOPS, SWEEP_FIRST, SWEEP_LAST, type SweepStop } from "./data";
/**
* Title Sweep: a centred title lit by a warm gradient that travels across it.
* The type never fades. It arrives and leaves because the gradient's dark ends
* pass over it, so a title goes dark from one side and the next one is cut in
* while the frame is black. The whole shot pulls back on one slow clock.
*
* The defaults are measured off a 1920x1080 reference at 30fps. The sweep is one
* rigid gradient moved by a measured table: fast at both ends, slow through the
* pale band.
*/
const { fontFamily: INTER } = loadFont("normal", {
weights: ["300", "400", "500", "600", "700", "800"],
subsets: ["latin"],
});
const STAGE_W = 1920;
const STAGE_H = 1080;
/** Widest a title may get, as a fraction of the stage. */
const MAX_LINE = 0.9;
/** A title longer than this many em stretches the sweep so it still crosses it. */
const REF_LINE = 7.6;
/** Frames between one title's cut and the next. */
export const TITLE_FRAMES = 36;
/** Scale lost per frame by the pull-back. */
const PULL_BACK = 0.00119;
/** The text baseline sits this far below the stage centre, em. */
const BASELINE = 0.267;
export interface TitleSweepProps {
/** Shown in order, each cut in on black. One title is fine. */
titles?: string[];
/** A family from `fonts.ts`, or a CSS family you have loaded yourself. */
fontFamily?: string;
/** Type size, px on a 1920 stage. Long copy shrinks from here to fit. */
fontSize?: number;
fontWeight?: number;
/** Letter spacing, em. */
tracking?: number;
/** Extra space between words, em. */
wordSpacing?: number;
/** Horizontal offset per title, px on a 1920 stage. */
nudge?: number[];
/** Size multiplier per title, for a line set a touch larger than the last. */
titleScale?: number[];
/** The gradient, in em from its centre. Start and end it on `background`. */
stops?: SweepStop[];
background?: string;
speed?: number;
}
/** Piecewise-linear lookup in a per-frame table, held at the ends. */
export const sample = (table: readonly number[], at: number) => {
const i = Math.min(Math.max(Math.floor(at), 0), table.length - 1);
const from = table[i] ?? 0;
const to = table[i + 1] ?? from;
return from + (to - from) * Math.min(1, Math.max(0, at - i));
};
/** Which title is on screen at `f`, and how long it has been up. */
export const titleAt = (f: number, count: number) => {
const index = Math.min(
Math.floor(Math.max(0, f) / TITLE_FRAMES),
Math.max(0, count - 1),
);
return { index, age: Math.max(0, f) - index * TITLE_FRAMES };
};
/** The last of several titles holds warm; any other title is swept off. */
export const sweepFor = (index: number, count: number) =>
count > 1 && index === count - 1 ? SWEEP_LAST : SWEEP_FIRST;
/** Scale of the slow pull-back at `f`. */
export const pullBack = (f: number) => Math.max(0.5, 1 - PULL_BACK * f);
/** Scale that keeps a line of `lineWidth` on a stage of `stageWidth`. */
export const fitLine = (lineWidth: number, stageWidth: number) =>
Math.min(1, (MAX_LINE * stageWidth) / Math.max(1, lineWidth));
export const TitleSweep = ({
titles = ["kuchi Pro", "kuchi.kalarislabs.com"],
fontFamily = "Inter",
fontSize = 110.96,
fontWeight = 500,
tracking = -0.0165,
wordSpacing = -0.0769,
nudge,
titleScale,
stops = DEFAULT_STOPS,
background = "#000000",
speed = 1,
}: TitleSweepProps) => {
const f = useCurrentFrame() * speed;
const { width, height } = useVideoConfig();
const id = useId().replace(/:/g, "");
const family = resolveFont(fontFamily) ?? INTER;
// A width measured before the face lands is a width for the fallback, so the
// fit waits on it; a render holds its first frame until that has re-rendered.
const [fontTick, setFontTick] = useState(0);
const [handle] = useState(() =>
getRemotionEnvironment().isRendering
? delayRender("title-sweep: loading the face")
: null,
);
const fontSpec = `${fontWeight} 100px ${family}`;
useEffect(() => {
let alive = true;
document.fonts
.load(fontSpec)
.catch(() => [])
.then(() => {
if (alive) setFontTick((n) => n + 1);
});
return () => {
alive = false;
};
}, [fontSpec]);
useEffect(() => {
if (fontTick > 0 && handle != null) continueRender(handle);
}, [fontTick, handle]);
const { index, age } = titleAt(f, titles.length);
const text = titles[index] ?? "";
// Advance width in em, tracking and word spacing included.
const lineEm = useMemo(() => {
void fontTick;
const ctx =
typeof document === "undefined"
? null
: document.createElement("canvas").getContext("2d");
if (!ctx) return text.length * 0.55;
ctx.font = fontSpec;
const spaces = text.split(" ").length - 1;
return (
ctx.measureText(text).width / 100 +
text.length * tracking +
spaces * wordSpacing
);
}, [fontTick, fontSpec, text, tracking, wordSpacing]);
const fit = fitLine(lineEm * fontSize, STAGE_W);
const size = fontSize * fit * (titleScale?.[index] ?? 1);
const reach = Math.max(1, lineEm / REF_LINE) * size;
const cx = STAGE_W / 2 + (nudge?.[index] ?? 0);
const centre =
STAGE_W / 2 + sample(sweepFor(index, titles.length), age) * reach;
const first = stops[0]?.at ?? 0;
const span = (stops[stops.length - 1]?.at ?? 1) - first || 1;
const zoom = pullBack(f);
const paint = `${id}sweep`;
return (
<AbsoluteFill style={{ background }}>
<svg
viewBox={`0 0 ${STAGE_W} ${STAGE_H}`}
width={width}
height={height}
style={{ position: "absolute", inset: 0, overflow: "hidden" }}
>
<defs>
<linearGradient
id={paint}
gradientUnits="userSpaceOnUse"
x1={centre + first * reach}
x2={centre + (first + span) * reach}
y1={0}
y2={0}
>
{stops.map((s) => (
<stop
key={s.at}
offset={(s.at - first) / span}
stopColor={s.color}
/>
))}
</linearGradient>
</defs>
<g transform={`translate(960 540) scale(${zoom}) translate(-960 -540)`}>
<text
x={cx}
y={STAGE_H / 2 + BASELINE * size}
textAnchor="middle"
fill={`url(#${paint})`}
style={{
fontFamily: family,
fontWeight,
fontSize: size,
letterSpacing: `${tracking}em`,
wordSpacing: `${wordSpacing}em`,
textRendering: "geometricPrecision",
}}
>
{text}
</text>
</g>
</svg>
</AbsoluteFill>
);
};
export default TitleSweep;
components/kuchi/title-sweep/data.ts
export interface SweepStop {
/** Position in em from the gradient's centre. Positive leads the sweep. */
at: number;
/** A concrete colour: this paints in a render. */
color: string;
}
/** Measured off the reference: black, deep red, orange, a pale band, and back. */
export const DEFAULT_STOPS: SweepStop[] = [
{ at: -17.52, color: "#000000" },
{ at: -16.727, color: "#030200" },
{ at: -15.934, color: "#0d0600" },
{ at: -15.141, color: "#160a00" },
{ at: -14.348, color: "#210c01" },
{ at: -13.482, color: "#2d1001" },
{ at: -12.689, color: "#431502" },
{ at: -11.896, color: "#581902" },
{ at: -11.103, color: "#631d08" },
{ at: -10.31, color: "#88300f" },
{ at: -9.517, color: "#9e3f18" },
{ at: -8.724, color: "#ce5c22" },
{ at: -7.931, color: "#d86629" },
{ at: -7.066, color: "#eb7535" },
{ at: -6.273, color: "#e3793d" },
{ at: -5.479, color: "#db7f47" },
{ at: -4.686, color: "#e6935b" },
{ at: -3.893, color: "#f0a771" },
{ at: -3.1, color: "#f4b784" },
{ at: -2.307, color: "#f1bd8d" },
{ at: -1.514, color: "#eebd8f" },
{ at: -0.721, color: "#e3ae7f" },
{ at: 0.144, color: "#e4a471" },
{ at: 0.937, color: "#ef9c62" },
{ at: 1.73, color: "#eb8c4f" },
{ at: 2.523, color: "#e77f42" },
{ at: 3.317, color: "#e37537" },
{ at: 4.11, color: "#dc6d31" },
{ at: 4.903, color: "#d5652b" },
{ at: 5.696, color: "#d16127" },
{ at: 6.489, color: "#af4c1e" },
{ at: 7.354, color: "#943917" },
{ at: 8.147, color: "#7a2810" },
{ at: 8.94, color: "#671e09" },
{ at: 9.733, color: "#561905" },
{ at: 10.526, color: "#461604" },
{ at: 11.319, color: "#3d1404" },
{ at: 12.112, color: "#2a0f02" },
{ at: 12.906, color: "#1f0d01" },
{ at: 13.771, color: "#130901" },
{ at: 14.564, color: "#0d0601" },
{ at: 15.357, color: "#070400" },
{ at: 16.15, color: "#030100" },
{ at: 16.943, color: "#000000" },
];
/** Gradient centre per frame of a title, in em from the stage centre. Swept off. */
export const SWEEP_FIRST = [
-6.625, -6.007, -5.192, -4.403, -3.78, -3.342, -2.969, -2.554, -2.033, -1.439,
-0.862, -0.383, -0.01, 0.344, 0.662, 0.895, 1.061, 1.148, 1.198, 1.326, 1.55,
1.781, 2.079, 2.506, 3.078, 3.821, 4.705, 5.657, 6.652, 7.781, 9.099, 10.546,
12.024, 13.444, 14.647, 15.396,
];
/** The same for the last of several titles, which never leaves. */
export const SWEEP_LAST = [
-17.907, -17.152, -16.058, -14.785, -13.36, -11.904, -10.494, -9.072, -7.668,
-6.398, -5.281, -4.343, -3.632, -3.026, -2.412, -1.792, -1.193, -0.646,
-0.196, 0.192, 0.518, 0.747, 0.918, 1.008, 1.048, 1.148, 1.359, 1.621, 1.879,
2.148, 2.495, 2.889, 3.31, 3.745, 4.168, 4.589, 5.014, 5.441, 5.859, 6.193,
];