Milestone Roll
A counter races through milestones to a 1,000,000 poster over a gradient ground.
Source
components/kuchi/milestone-roll.tsx
1"use client";23import { loadFont as loadMono } from "@remotion/google-fonts/GeistMono";4import { loadFont as loadSerif } from "@remotion/google-fonts/InstrumentSerif";5import { loadFont as loadManrope } from "@remotion/google-fonts/Manrope";6import { loadFont } from "@remotion/google-fonts/SpaceGrotesk";7import type React from "react";8import {9 AbsoluteFill,10 Easing,11 interpolate,12 spring,13 useCurrentFrame,14 useVideoConfig,15} from "remotion";16import type { GradientParams } from "@/lib/kuchi/gsh/gradient-engine";17import { GshGround } from "@/lib/kuchi/gsh/gsh-ground";18import type { GshPresetId } from "@/lib/kuchi/gsh/presets";1920const { fontFamily: grotesk } = loadFont("normal", {21 weights: ["300", "400"],22 subsets: ["latin"],23});24const { fontFamily: mono } = loadMono("normal", {25 weights: ["400"],26 subsets: ["latin"],27});28const { fontFamily: serifItalic } = loadSerif("italic", {29 weights: ["400"],30 subsets: ["latin"],31});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 MilestoneRollProps {58 /** Milestones the counter races through, ascending. The last one is the poster. */59 milestones?: number[];60 /** What is being counted, set in the serif italic on the poster and in the top label. */61 unit?: string;62 /** Kept for the shared draft API; the colour comes from the halo ground. */63 accent?: string;64 /** Text color of the numbers, labels and progress bar; overrides the theme ink. */65 ink?: string;66 /** Solid fill in place of the gradient ground; "transparent" drops it. */67 background?: string;68 /** Typeface for the numbers. Load it yourself; Space Grotesk by default. */69 fontFamily?: string;70 /** Frames the counter races to each milestone. */71 run?: number;72 /** Frames each milestone holds before the next race. */73 hold?: number;74 /** Replaces the acme mark. */75 logo?: React.ReactNode;76 /** Size of the counter and chrome, around the frame's center. */77 scale?: number;78 /** Frames to hold the first frame before anything moves. */79 delay?: number;80 theme?: "dark" | "light";81 /** gradients.sh preset id for the ground. */82 ground?: GshPresetId;83 /** Overrides for the ground shader. */84 groundParams?: Partial<GradientParams>;85 speed?: number;86}8788const RUN = 15;89const HOLD = 14;90const OUT = 6;91const FIRST = -2;9293const RACE = Easing.bezier(0.55, 0, 1, 0.45);94const MOVE = Easing.bezier(0.83, 0, 0.17, 1);95const ENTER = Easing.bezier(0.32, 0.72, 0, 1);96const CAMERA = Easing.bezier(0.87, 0, 0.13, 1);97const HEAVY = { mass: 1, damping: 26, stiffness: 120 };98const CARD = { mass: 0.8, damping: 18, stiffness: 160 };99const MORPH = { damping: 200 };100const CLAMP = {101 extrapolateLeft: "clamp",102 extrapolateRight: "clamp",103} as const;104105const THEMES = {106 dark: {107 stage: "#030306",108 ink: "#F7F3EE",109 flash: "#FFF6E8",110 flashAlpha: 0.7,111 preset: "iris" as GshPresetId,112 params: { background: "#030306" } as Partial<GradientParams>,113 veil: [0.5, 0.38, 0.24, 0.12],114 final: 0.08,115 ring: [0.8, 1.05, 1.35, 1.7],116 finalRing: 2.5,117 },118 light: {119 stage: "#F4F0EA",120 ink: "#16121A",121 flash: "#FFFFFF",122 flashAlpha: 0.8,123 preset: "iris" as GshPresetId,124 params: {125 background: "#F4F0EA",126 colors: ["#FFFFFF", "#FFB25A", "#EF4F86", "#4058FF"],127 } as Partial<GradientParams>,128 veil: [0.5, 0.4, 0.28, 0.16],129 final: 0.1,130 ring: [0.8, 1.05, 1.35, 1.7],131 finalRing: 2.5,132 },133} as const;134135function ramp(t: number, from: number, len: number) {136 return interpolate(t, [from, from + len], [0, 1], CLAMP);137}138139function kick(d: number) {140 if (d < 0) return 0;141 if (d < 2) return d / 2;142 const k = Math.max(0, 1 - (d - 2) / 8);143 return k * k;144}145146function shake(d: number, amp: number, seed: number) {147 if (d < 0 || d > 7) return { x: 0, y: 0 };148 const k = (1 - d / 7) ** 2;149 return {150 x: amp * k * Math.sin(d * 2.7 + seed),151 y: amp * 0.7 * k * Math.cos(d * 3.3 + seed * 1.7),152 };153}154155function abbreviate(n: number) {156 if (n >= 1e6) return `${+(n / 1e6).toFixed(1)}M`;157 if (n >= 1e3) return `${+(n / 1e3).toFixed(1)}K`;158 return `${n}`;159}160161function withCommas(n: number) {162 return Math.round(n)163 .toString()164 .replace(/\B(?=(\d{3})+(?!\d))/g, ",");165}166167/** The foreground at `scale`, around the frame's center; nothing extra at 1. */168function ScaleLayer({169 scale,170 children,171}: {172 scale: number;173 children: React.ReactNode;174}) {175 if (scale === 1) return children;176 return (177 <AbsoluteFill style={{ transform: `scale(${scale})` }}>178 {children}179 </AbsoluteFill>180 );181}182183export function MilestoneRoll({184 milestones = [1000, 10000, 100000, 1000000],185 unit = "renders",186 ink,187 background,188 fontFamily = grotesk,189 run = RUN,190 hold = HOLD,191 logo,192 scale = 1,193 delay = 0,194 theme = "dark",195 ground,196 groundParams,197 speed = 1,198}: MilestoneRollProps) {199 const t = Math.max(0, useCurrentFrame() - delay) * speed;200 const { width: W, height: H, fps } = useVideoConfig();201 const c = THEMES[theme];202 const fg = ink ?? c.ink;203 const stage =204 background === undefined205 ? c.stage206 : background === "transparent"207 ? "transparent"208 : background;209 const seg = run + hold + OUT;210 const tall = H > W;211 const base = tall ? W * 0.62 : H;212 const u = Math.min(W / 1280, H / 720);213214 const n = milestones.length;215 const hitAt = (i: number) => FIRST + i * seg + run;216 const finalAt = hitAt(n - 1);217218 let hit = -1;219 for (let i = 0; i < n; i++) if (t >= hitAt(i)) hit = i;220 const isFinal = hit === n - 1;221222 const step = (arr: readonly number[], i: number) =>223 arr[Math.max(0, Math.min(arr.length - 1, i))];224 let ring: number = c.ring[0];225 let veil: number = c.veil[0];226 for (let i = 0; i < n; i++) {227 const k = spring({ frame: t - hitAt(i), fps, config: HEAVY });228 const toRing = i === n - 1 ? c.finalRing : step(c.ring, i + 1);229 const toVeil = i === n - 1 ? c.final : step(c.veil, i + 1);230 ring += (toRing - step(c.ring, i)) * k;231 veil += (toVeil - step(c.veil, i)) * Math.min(1, k);232 }233 const breathe = interpolate(t, [0, 180], [0, 1], CLAMP);234 const flash = hit >= 0 ? Math.max(0, 1 - (t - hitAt(hit)) / 4) ** 2 : 0;235236 const runIdx = hit + 1;237 const runStart = FIRST + runIdx * seg;238 const from = runIdx === 0 ? 0 : milestones[runIdx - 1];239 const to = milestones[Math.min(runIdx, n - 1)];240 const race = RACE(ramp(t, runStart, run));241 const value = from + (to - from) * race;242243 const h = hit >= 0 && !isFinal ? hitAt(hit) : -1e9;244 const pPunch = spring({ frame: Math.min(t - h, 60), fps, config: CARD });245 const push = MOVE(ramp(t, h + hold, OUT));246 const pushDist = 0.8 * H;247 const inHit = hit >= 0 && !isFinal && t < h + hold + OUT;248249 let bump = 0;250 let sx = 0;251 let sy = 0;252 for (let i = 0; i < n; i++) {253 const d = t - hitAt(i) - 3;254 bump = Math.max(bump, (i === n - 1 ? 1.3 : 0.9) * kick(d));255 const s = shake(d, (i === n - 1 ? 9 : 6) * u, i * 1.9);256 sx += s.x;257 sy += s.y;258 }259 const camScale = 1 + 0.03 * bump;260261 const runFs = 0.28 * base;262 const hugeFs = 0.56 * base;263 const finalText = withCommas(milestones[n - 1]);264 const digits = finalText.replace(/,/g, "").length;265 const commas = finalText.length - digits;266 const finalFs = Math.min(267 0.3 * base,268 (0.84 * W) / (digits * 0.6 + commas * 0.26),269 );270271 const numberStyle = {272 fontFamily,273 fontWeight: 300,274 fontVariantNumeric: "tabular-nums",275 lineHeight: 1,276 whiteSpace: "nowrap",277 } as const;278279 const pFinal = spring({ frame: t - finalAt, fps, config: CARD });280 const pUnit = spring({ frame: t - finalAt - 6, fps, config: MORPH });281 const pLogo = spring({ frame: t - finalAt - 12, fps, config: MORPH });282 const creep = interpolate(t, [finalAt + 4, finalAt + 70], [1, 1.035], {283 ...CLAMP,284 easing: CAMERA,285 });286287 const chromeOut = isFinal ? 1 - ENTER(ramp(t, finalAt, 6)) : 1;288 const runFill = isFinal ? 1 : inHit ? 1 : race;289 const margin = (tall ? 0.07 : 0.05) * W;290 const labelFs = (tall ? 0.017 : 0.025) * H;291292 return (293 <AbsoluteFill style={{ background: stage, overflow: "hidden" }}>294 {background === undefined ? (295 <>296 <AbsoluteFill297 style={{298 transform: `scale(${ring * (1 + 0.04 * breathe)}) rotate(${-8 * breathe}deg)`,299 }}300 >301 <GshGround302 preset={ground ?? c.preset}303 params={groundParams ?? c.params}304 timeOffset={2}305 timeScale={0.5}306 style={{ width: "100%", height: "100%" }}307 />308 </AbsoluteFill>309 <AbsoluteFill style={{ background: c.stage, opacity: veil }} />310 </>311 ) : null}312313 <ScaleLayer scale={scale}>314 <div315 style={{316 position: "absolute",317 inset: 0,318 transform: `translate(${sx}px, ${sy}px) scale(${camScale})`,319 transformOrigin: "50% 50%",320 color: fg,321 }}322 >323 <div324 style={{325 position: "absolute",326 left: margin,327 right: margin,328 top: (tall ? 0.06 : 0.07) * H,329 display: "flex",330 justifyContent: "space-between",331 alignItems: "center",332 fontFamily: mono,333 fontWeight: 400,334 fontSize: labelFs,335 letterSpacing: "0.06em",336 textTransform: "uppercase",337 opacity: chromeOut * ENTER(ramp(t, 0, 10)),338 }}339 >340 <div341 style={{342 display: "flex",343 alignItems: "center",344 gap: 0.5 * labelFs,345 }}346 >347 <div348 style={{349 width: 0.4 * labelFs,350 height: 0.4 * labelFs,351 borderRadius: labelFs,352 background: fg,353 }}354 />355 {unit}356 </div>357 <div style={{ display: "flex", gap: 1.4 * labelFs }}>358 {milestones.map((m, i) => (359 <div360 key={m}361 style={{362 opacity: i <= hit ? 1 : 0.4,363 fontVariantNumeric: "tabular-nums",364 }}365 >366 {abbreviate(m)}367 </div>368 ))}369 </div>370 </div>371372 <div373 style={{374 position: "absolute",375 left: margin,376 right: margin,377 bottom: (tall ? 0.06 : 0.08) * H,378 height: Math.max(2, 2 * u),379 opacity: chromeOut * ENTER(ramp(t, 0, 10)),380 }}381 >382 <div383 style={{384 position: "absolute",385 inset: 0,386 background: fg,387 opacity: 0.2,388 }}389 />390 <div391 style={{392 position: "absolute",393 left: 0,394 top: 0,395 bottom: 0,396 width: "100%",397 transform: `scaleX(${runFill})`,398 transformOrigin: "0% 50%",399 background: fg,400 }}401 />402 </div>403404 {!isFinal && (!inHit || t >= h + hold) ? (405 <div406 style={{407 position: "absolute",408 inset: 0,409 display: "flex",410 alignItems: "center",411 justifyContent: "center",412 transform: `translateY(${inHit ? (1 - push) * pushDist : 0}px)`,413 }}414 >415 <div416 style={{417 ...numberStyle,418 fontSize: runFs,419 letterSpacing: "-0.04em",420 }}421 >422 {withCommas(inHit ? milestones[hit] : value)}423 </div>424 </div>425 ) : null}426427 {inHit ? (428 <div429 style={{430 position: "absolute",431 inset: 0,432 display: "flex",433 alignItems: "center",434 justifyContent: "center",435 transform: `translateY(${-push * pushDist}px) scale(${1.3 - 0.3 * pPunch})`,436 filter:437 pPunch < 0.9438 ? `blur(${Math.max(0, 1 - pPunch) ** 2 * 14 * u}px)`439 : undefined,440 }}441 >442 <div443 style={{444 ...numberStyle,445 fontSize: hugeFs,446 letterSpacing: "-0.055em",447 paddingRight: 0.02 * hugeFs,448 }}449 >450 {abbreviate(milestones[hit])}451 </div>452 </div>453 ) : null}454455 {isFinal ? (456 <div457 style={{458 position: "absolute",459 inset: 0,460 display: "flex",461 flexDirection: "column",462 alignItems: "center",463 justifyContent: "center",464 transform: `scale(${creep})`,465 }}466 >467 <div468 style={{469 ...numberStyle,470 fontSize: finalFs,471 letterSpacing: "-0.05em",472 transform: `scale(${1.25 - 0.25 * pFinal})`,473 filter:474 pFinal < 0.9475 ? `blur(${Math.max(0, 1 - pFinal) ** 2 * 14 * u}px)`476 : undefined,477 opacity: ramp(t, finalAt - 1, 2),478 }}479 >480 {finalText}481 </div>482 <div483 style={{484 fontFamily: serifItalic,485 fontStyle: "italic",486 fontWeight: 400,487 fontSize: 0.16 * base,488 lineHeight: 1,489 letterSpacing: "-0.02em",490 marginTop: 0.005 * base,491 opacity: ramp(t, finalAt + 6, 4),492 transform: `translateY(${(1 - pUnit) * 0.06 * base}px)`,493 }}494 >495 {unit}496 </div>497 </div>498 ) : null}499 {isFinal ? (500 <div501 style={{502 position: "absolute",503 left: 0,504 right: 0,505 bottom: (tall ? 0.07 : 0.075) * H,506 display: "flex",507 justifyContent: "center",508 opacity: ramp(t, finalAt + 12, 5),509 transform: `translateY(${(1 - pLogo) * 0.04 * H}px)`,510 }}511 >512 {logo ?? (513 <KuchiLogo fontSize={(tall ? 0.024 : 0.05) * H} color={fg} />514 )}515 </div>516 ) : null}517 </div>518 </ScaleLayer>519520 {flash > 0.01 ? (521 <AbsoluteFill522 style={{ background: c.flash, opacity: flash * c.flashAlpha }}523 />524 ) : null}525 </AbsoluteFill>526 );527}









