Word Pullback
The camera pulls back from a big word to reveal "The cinematic toolkit for every launch".
Source
components/kuchi/word-pullback.tsx
1"use client";23import { loadFont as loadSerif } from "@remotion/google-fonts/InstrumentSerif";4import { loadFont as loadSans } from "@remotion/google-fonts/InterTight";5import { loadFont as loadManrope } from "@remotion/google-fonts/Manrope";6import {7 type CSSProperties,8 useEffect,9 useMemo,10 useRef,11 useState,12} from "react";13import {14 AbsoluteFill,15 continueRender,16 delayRender,17 Easing,18 interpolate,19 spring,20 useCurrentFrame,21 useVideoConfig,22} from "remotion";23import type { GradientParams } from "@/lib/kuchi/gsh/gradient-engine";24import { GshGround, gshParams } from "@/lib/kuchi/gsh/gsh-ground";25import type { GshPresetId } from "@/lib/kuchi/gsh/presets";2627const sans = loadSans("normal", {28 weights: ["300", "400"],29 subsets: ["latin"],30});31const serif = loadSerif("italic", { weights: ["400"], subsets: ["latin"] });32const manrope = loadManrope("normal", {33 weights: ["600"],34 subsets: ["latin"],35});363738function KuchiLogo({ fontSize, color }: { fontSize: number; color: string }) {39 return (40 <div41 style={{42 display: "flex",43 alignItems: "baseline",44 color,45 fontFamily: manrope.fontFamily,46 fontWeight: 600,47 fontSize,48 letterSpacing: "-0.025em",49 lineHeight: 1,50 }}51 >52 <span>Acme</span>53 </div>54 );55}5657export interface WordPullbackProps {58 before?: string;59 accentWord?: string;60 after?: string;61 accent?: string;62 ink?: string;63 /** Solid fill in place of the gradient ground; "transparent" drops it. */64 background?: string;65 /** Typeface for the line around the accent word. Load it yourself; Inter Tight by default. */66 fontFamily?: string;67 /** Weight of the line around the accent word. */68 fontWeight?: number;69 /** Frames between words as the rest of the line rises. */70 stagger?: number;71 /** Replaces the acme mark above the line. */72 logo?: React.ReactNode;73 /** Size of the line and logo, around the frame's center. */74 scale?: number;75 /** Frames to hold the first frame before anything moves. */76 delay?: number;77 speed?: number;78 theme?: "dark" | "light";79 ground?: GshPresetId;80 groundParams?: Partial<GradientParams>;81}8283const LIGHT_GROUND: Partial<GradientParams> = {84 kind: "mist",85 background: "#f4efe8",86 colors: ["#ffe2cc", "#f7ad82", "#eec2cf", "#f4efe8"],87 speed: 0.8,88 loopSeconds: 14,89 scale: 0.85,90 warp: 0.4,91 blur: 0.7,92 grain: 0.22,93 seed: 12,94};9596const THEMES = {97 dark: {98 ink: "#f6efe8",99 accent: "#ffc79a",100 logo: "#f6efe8",101 ground: "hearth" as GshPresetId | undefined,102 groundParams: undefined as Partial<GradientParams> | undefined,103 groundOffset: 5,104 veil: "6,3,3",105 veilFrom: 0.72,106 veilTo: 0.12,107 },108 light: {109 ink: "#1b1512",110 accent: "#b3441c",111 logo: "#1b1512",112 ground: undefined as GshPresetId | undefined,113 groundParams: LIGHT_GROUND as Partial<GradientParams> | undefined,114 groundOffset: 3,115 veil: "244,239,232",116 veilFrom: 0.75,117 veilTo: 0.05,118 },119};120121const BEAT = {122 typeEnd: 27,123 pull1: 30,124 pull1Len: 18,125 line2: 45,126 wordStep: 3,127 rise: 12,128 pull2: 72,129 pull2Len: 15,130 logo: 78,131 flood: 72,132 floodLen: 24,133};134135const TRACK = -0.03;136const SERIF_SCALE = 1.18;137const FONT_PX = 200;138const RISE = Easing.bezier(0.22, 1, 0.36, 1);139const CAMERA = Easing.bezier(0.87, 0, 0.13, 1);140const TYPE = Easing.bezier(0.65, 0, 0.35, 1);141const FLOOD = Easing.bezier(0.32, 0.72, 0, 1);142const CHIP = { mass: 0.5, damping: 12, stiffness: 200 };143const CLAMP = {144 extrapolateLeft: "clamp",145 extrapolateRight: "clamp",146} as const;147148interface Measure {149 sans: (s: string) => number;150 serif: (s: string) => number;151}152153function buildMeasure(154 _version: number,155 fontFamily: string,156 fontWeight: number,157): Measure {158 const ctx = document.createElement("canvas").getContext("2d");159 const cache: Record<string, number> = {};160 const get = (font: string, track: number, s: string) => {161 const key = `${font}|${s}`;162 if (cache[key] === undefined) {163 if (!ctx) return s.length * 0.5;164 ctx.font = font;165 cache[key] =166 ctx.measureText(s).width / FONT_PX + track * Array.from(s).length;167 }168 return cache[key];169 };170 return {171 sans: (s) => get(`${fontWeight} ${FONT_PX}px "${fontFamily}"`, TRACK, s),172 serif: (s) =>173 get(`italic 400 ${FONT_PX}px "${serif.fontFamily}"`, -0.01, s) *174 SERIF_SCALE,175 };176}177178function useMeasure(fontFamily: string, fontWeight: number) {179 const [version, setVersion] = useState(0);180 const [handle] = useState(() => delayRender("word-pullback: measuring"));181 const released = useRef(false);182 useEffect(() => {183 let live = true;184 const bump = () => {185 if (live) setVersion((v) => v + 1);186 };187 document.fonts.addEventListener("loadingdone", bump);188 Promise.all([189 sans.waitUntilDone(),190 serif.waitUntilDone(),191 manrope.waitUntilDone(),192 document.fonts.load(`${fontWeight} 100px "${fontFamily}"`),193 document.fonts.load(`italic 400 100px "${serif.fontFamily}"`),194 document.fonts.load(`600 100px "${manrope.fontFamily}"`),195 ])196 .catch(() => undefined)197 .then(() => {198 bump();199 if (!released.current) {200 released.current = true;201 continueRender(handle);202 }203 });204 return () => {205 live = false;206 document.fonts.removeEventListener("loadingdone", bump);207 };208 }, [handle, fontFamily, fontWeight]);209 return useMemo(210 () => buildMeasure(version, fontFamily, fontWeight),211 [version, fontFamily, fontWeight],212 );213}214215interface Token {216 text: string;217 serif: boolean;218 w: number;219}220221/** The foreground at `scale`, around the frame's center; nothing extra at 1. */222function ScaleLayer({223 scale,224 children,225}: {226 scale: number;227 children: React.ReactNode;228}) {229 if (scale === 1) return children;230 return (231 <AbsoluteFill style={{ transform: `scale(${scale})` }}>232 {children}233 </AbsoluteFill>234 );235}236237export function WordPullback({238 before = "The",239 accentWord = "cinematic",240 after = "toolkit for every launch",241 accent,242 ink,243 background,244 fontFamily = sans.fontFamily,245 fontWeight = 300,246 stagger = BEAT.wordStep,247 logo,248 scale = 1,249 delay = 0,250 speed = 1,251 theme = "dark",252 ground,253 groundParams,254}: WordPullbackProps) {255 const frame = Math.max(0, useCurrentFrame() - delay) * speed;256 const { width: W, height: H, fps, durationInFrames } = useVideoConfig();257 const c = THEMES[theme];258 const inkC = ink ?? c.ink;259 const hi = accent ?? c.accent;260 const preset = ground ?? (groundParams ? undefined : c.ground);261 const gParams = groundParams ?? (ground ? undefined : c.groundParams);262 const bg = background ?? gshParams(preset, gParams).background;263 const m = useMeasure(fontFamily, fontWeight);264 const portrait = H > W * 1.1;265266 const flood = interpolate(267 frame,268 [BEAT.flood, BEAT.flood + BEAT.floodLen],269 [0, 1],270 { ...CLAMP, easing: FLOOD },271 );272 const veil = c.veilFrom + (c.veilTo - c.veilFrom) * flood;273274 let content: React.ReactNode = <div />;275 if (m) {276 const cx = W / 2;277 const cy = H / 2;278 const marginX = W * (portrait ? 0.08 : 0.1);279 const room = W - marginX * 2;280 const beforeWords = before.split(" ").filter(Boolean);281 const tokens: Token[] = [282 ...beforeWords.map((s) => ({ text: s, serif: false, w: m.sans(s) })),283 { text: accentWord, serif: true, w: m.serif(accentWord) },284 ...after285 .split(" ")286 .filter(Boolean)287 .map((s) => ({ text: s, serif: false, w: m.sans(s) })),288 ];289 const accentIdx = beforeWords.length;290 const sp = m.sans(" ");291 const totalEm =292 tokens.reduce((a, t) => a + t.w, 0) + sp * (tokens.length - 1);293 const longest = Math.max(...tokens.map((t) => t.w));294295 const k = Math.min(296 tokens.length,297 portrait298 ? Math.max(2, Math.ceil(totalEm / 5.2))299 : totalEm > 16300 ? 3301 : totalEm > 8302 ? 2303 : 1,304 );305 const widthOf = (a: number, b: number) =>306 tokens.slice(a, b).reduce((acc, t) => acc + t.w, 0) + sp * (b - a - 1);307 const best: number[][] = [];308 const cut: number[][] = [];309 for (let j = 0; j <= k; j++) {310 best.push(new Array(tokens.length + 1).fill(Number.POSITIVE_INFINITY));311 cut.push(new Array(tokens.length + 1).fill(0));312 }313 best[0][0] = 0;314 for (let j = 1; j <= k; j++) {315 for (let b = 1; b <= tokens.length; b++) {316 for (let a = j - 1; a < b; a++) {317 const v = Math.max(best[j - 1][a], widthOf(a, b));318 if (v < best[j][b]) {319 best[j][b] = v;320 cut[j][b] = a;321 }322 }323 }324 }325 const lines: number[][] = [];326 let end = tokens.length;327 for (let j = k; j >= 1; j--) {328 const a = cut[j][end];329 lines.unshift(Array.from({ length: end - a }, (_, q) => a + q));330 end = a;331 }332 const maxLineEm = best[k][tokens.length];333 const T = portrait334 ? Math.min(W * 0.16, room / Math.max(maxLineEm, longest))335 : Math.min(H * 0.17, room / Math.max(maxLineEm, longest));336337 const lineH = T * 1.12;338 const logoSize = T * 0.42;339 const logoGap = T * 0.5;340 const blockH = logoSize + logoGap + lines.length * lineH;341 const top = cy - blockH / 2;342 const lineY = (li: number) =>343 top + logoSize + logoGap + li * lineH + lineH / 2;344 const lineWidth = (ln: number[]) =>345 ln.reduce((a, i) => a + tokens[i].w, 0) * T + sp * T * (ln.length - 1);346347 let accentX = cx;348 let accentY = cy;349 let accentLine = 0;350 const placed: { i: number; x: number; y: number; li: number }[] = [];351 lines.forEach((ln, li) => {352 let x = cx - lineWidth(ln) / 2;353 ln.forEach((i) => {354 placed.push({ i, x, y: lineY(li), li });355 if (i === accentIdx) {356 accentX = x + (tokens[i].w * T) / 2;357 accentY = lineY(li);358 accentLine = li;359 }360 x += tokens[i].w * T + sp * T;361 });362 });363364 const accentPx = tokens[accentIdx].w * T;365 const z0 = (W * (portrait ? 1.25 : 1.15)) / accentPx;366 const maxLinePx = Math.max(...lines.map(lineWidth));367 const z1 = Math.max(368 1.12,369 Math.min(Math.sqrt(z0) * 1.05, (W * 0.94) / maxLinePx),370 );371372 const typeEnd = Math.max(373 BEAT.typeEnd - 6,374 Math.min(BEAT.typeEnd + 6, accentWord.length * 3),375 );376 const typed = interpolate(frame, [-9, typeEnd], [0, 1], {377 ...CLAMP,378 easing: TYPE,379 });380 const typedN = Math.max(1, Math.ceil(typed * accentWord.length));381 const typedPx = m.serif(accentWord.slice(0, typedN)) * T;382 const accentLeft = accentX - accentPx / 2;383384 const cam = (f: number) => {385 const p1 = interpolate(386 f,387 [BEAT.pull1, BEAT.pull1 + BEAT.pull1Len],388 [0, 1],389 { ...CLAMP, easing: CAMERA },390 );391 const p2 = interpolate(392 f,393 [BEAT.pull2, BEAT.pull2 + BEAT.pull2Len],394 [0, 1],395 { ...CLAMP, easing: CAMERA },396 );397 const dolly = interpolate(f, [0, BEAT.pull1 + 6], [0, 1], CLAMP);398 const tP = interpolate(f, [-9, typeEnd], [0, 1], {399 ...CLAMP,400 easing: TYPE,401 });402 const lz0 = Math.log(z0 * (1 - 0.08 * dolly));403 const lz1 = Math.log(404 z1 *405 (1 -406 0.04 *407 interpolate(408 f,409 [BEAT.pull1 + BEAT.pull1Len, BEAT.pull2],410 [0, 1],411 CLAMP,412 )),413 );414 const lnZ = lz0 + (lz1 - lz0) * p1 + (0 - lz1) * p2;415 const f0x = accentLeft + accentPx * (0.18 + 0.5 * tP);416 const f0y = accentY;417 const f1x = cx;418 const f1y = cy + (logoSize + logoGap) / 2;419 const fx = f0x + (f1x - f0x) * p1 + (cx - f1x) * p2;420 const fy = f0y + (f1y - f0y) * p1 + (cy - f1y) * p2;421 return { lnZ, fx, fy };422 };423424 const C = cam(frame);425 const Cp = cam(frame - 1);426 const vel = Math.abs(C.lnZ - Cp.lnZ);427 const screenBlur = Math.min(8, vel * 30);428 const push = interpolate(429 frame,430 [BEAT.pull2 + BEAT.pull2Len, durationInFrames],431 [1, 1.03],432 CLAMP,433 );434 const Z = Math.exp(C.lnZ) * push;435 const depth = Math.max(0, Math.min(1, (Z - 1) / Math.max(0.001, z0 - 1)));436437 const reveal = Math.min(1, typedPx / Math.max(1, accentPx) + 0.015);438439 const tokenNodes = placed.map(({ i, x, y, li }) => {440 const k = tokens[i];441 const isAccent = i === accentIdx;442 let enterAt = -99;443 if (!isAccent && i > accentIdx) {444 if (li === accentLine) {445 enterAt = typeEnd - 3 + (i - accentIdx - 1) * stagger;446 } else {447 const firstOnLine = lines[li][0];448 enterAt =449 BEAT.line2 +450 (li - accentLine - 1) * 6 +451 (i - firstOnLine) * stagger;452 }453 }454 const e = interpolate(frame, [enterAt, enterAt + BEAT.rise], [0, 1], {455 ...CLAMP,456 easing: RISE,457 });458 const dist = Math.abs(i - accentIdx);459 const localBlur = isAccent460 ? 0461 : (depth * Math.min(2, dist) * 2.6) / Math.max(1, Z) +462 (1 - e) * (5 / Math.max(1, Z));463 const style: CSSProperties = {464 position: "absolute",465 left: x,466 top: y,467 transform: "translateY(-50%)",468 whiteSpace: "pre",469 lineHeight: 1,470 color: isAccent ? hi : inkC,471 fontFamily: isAccent ? serif.fontFamily : fontFamily,472 fontStyle: isAccent ? "italic" : "normal",473 fontWeight: isAccent ? 400 : fontWeight,474 fontSize: isAccent ? T * SERIF_SCALE : T,475 letterSpacing: isAccent ? "-0.01em" : `${TRACK}em`,476 clipPath: isAccent477 ? `inset(-30% ${((1 - reveal) * 100).toFixed(2)}% -30% -10%)`478 : "inset(-30% -10% -12% -10%)",479 marginTop: isAccent ? T * 0.03 : 0,480 opacity: isAccent ? 1 : 0.62 + 0.38 * (1 - depth),481 filter:482 localBlur > 0.02 ? `blur(${localBlur.toFixed(3)}px)` : undefined,483 };484 return (485 <div key={`t${i}`} style={style}>486 <span487 style={{488 display: "inline-block",489 transform: `translateY(${((1 - e) * 0.8).toFixed(4)}em)`,490 }}491 >492 {k.text}493 </span>494 </div>495 );496 });497498 const logoIn = spring({ frame: frame - BEAT.logo, fps, config: CHIP });499 const logoE = Math.min(1, Math.max(0, logoIn));500 const caretOn =501 frame < typeEnd + 4 && Math.floor(frame / 8) % 2 === 0 ? 1 : 0;502 const caretX = accentLeft + typedPx + T * 0.04;503504 const layer = (scale: number, opacity: number, key: string) => (505 <div506 key={key}507 style={{508 position: "absolute",509 left: 0,510 top: 0,511 width: W,512 height: H,513 transformOrigin: "0 0",514 transform: `translate(${cx}px, ${cy}px) scale(${(Z * scale).toFixed(5)}) translate(${(-C.fx).toFixed(3)}px, ${(-C.fy).toFixed(3)}px)`,515 opacity,516 }}517 >518 {tokenNodes}519 <div520 style={{521 position: "absolute",522 left: caretX,523 top: accentY - T * 0.48,524 width: Math.max(1.5, T * 0.025),525 height: T * 0.96,526 background: hi,527 opacity: caretOn * (frame < typeEnd + 4 ? 1 : 0),528 }}529 />530 <div531 style={{532 position: "absolute",533 left: 0,534 width: W,535 top,536 height: logoSize * 1.2,537 display: "flex",538 justifyContent: "center",539 }}540 >541 <div542 style={{543 transform: `translateY(${((1 - logoIn) * logoSize * 0.9).toFixed(2)}px) scale(${(0.86 + 0.14 * logoIn).toFixed(4)})`,544 opacity: logoE,545 filter:546 logoE < 1547 ? `blur(${((1 - logoE) * 4).toFixed(2)}px)`548 : undefined,549 }}550 >551 {logo ?? <KuchiLogo fontSize={logoSize} color={c.logo} />}552 </div>553 </div>554 </div>555 );556557 const ghosts = screenBlur > 0.8 ? [1.04, 1.09] : [];558 content = (559 <AbsoluteFill560 style={{561 filter:562 screenBlur > 0.1 ? `blur(${screenBlur.toFixed(2)}px)` : undefined,563 }}564 >565 {ghosts.map((g, gi) => layer(g, 0.2 - gi * 0.08, `ghost${gi}`))}566 {layer(1, 1, "main")}567 </AbsoluteFill>568 );569 }570571 return (572 <AbsoluteFill style={{ background: bg, overflow: "hidden" }}>573 {background === undefined ? (574 <>575 <GshGround576 preset={preset}577 params={gParams}578 timeOffset={c.groundOffset}579 timeScale={0.6}580 style={{581 transform: `scale(${(1.12 - 0.12 * flood).toFixed(4)}) translateY(${((1 - flood) * H * 0.06).toFixed(2)}px)`,582 }}583 />584 <AbsoluteFill585 style={{586 background: `radial-gradient(${portrait ? "95% 60%" : "75% 80%"} at 50% 46%, rgba(${c.veil},${(veil + 0.25 * (1 - veil)).toFixed(3)}) 0%, rgba(${c.veil},${veil.toFixed(3)}) 100%)`,587 }}588 />589 </>590 ) : (591 <AbsoluteFill592 style={{593 background: background === "transparent" ? undefined : background,594 }}595 />596 )}597 <ScaleLayer scale={scale}>{content}</ScaleLayer>598 </AbsoluteFill>599 );600}601602export default WordPullback;









