Speedrun Timer
A speedrun split table fills row by row and ends on a gold 'WORLD RECORD' time.
Source
components/kuchi/speedrun-timer.tsx
1"use client";23import { loadFont } from "@remotion/google-fonts/Geist";4import { loadFont as loadMono } from "@remotion/google-fonts/GeistMono";5import { loadFont as loadManrope } from "@remotion/google-fonts/Manrope";6import type React from "react";7import {8 AbsoluteFill,9 Easing,10 interpolate,11 spring,12 useCurrentFrame,13 useVideoConfig,14} from "remotion";15import type { GradientParams } from "@/lib/kuchi/gsh/gradient-engine";16import { GshGround } from "@/lib/kuchi/gsh/gsh-ground";17import type { GshPresetId } from "@/lib/kuchi/gsh/presets";1819const { fontFamily: geist } = loadFont("normal", {20 weights: ["300", "400"],21 subsets: ["latin"],22});23const { fontFamily: mono } = loadMono("normal", {24 weights: ["400"],25 subsets: ["latin"],26});27const manrope = loadManrope("normal", {28 weights: ["600"],29 subsets: ["latin"],30});313233function KuchiLogo({ fontSize, color }: { fontSize: number; color: string }) {34 return (35 <div36 style={{37 display: "flex",38 alignItems: "baseline",39 color,40 fontFamily: manrope.fontFamily,41 fontWeight: 600,42 fontSize,43 letterSpacing: "-0.025em",44 lineHeight: 1,45 }}46 >47 <span>Acme</span>48 </div>49 );50}5152export interface SpeedrunSplit {53 name: string;54 /** Cumulative run time at this split, in seconds. */55 time: number;56 /** Cumulative personal-best time at this split, in seconds. */57 pb: number;58}5960export interface SpeedrunTimerProps {61 /** Category line in the header, after the logo. */62 category?: string;63 /** The splits, in order. The last one is the finish and goes gold. */64 splits?: SpeedrunSplit[];65 /** The banner that lands when the run finishes. */66 banner?: string;67 /** Colour of the current-split marker. */68 accent?: string;69 /** Text color of the splits and timer; overrides the theme ink. */70 ink?: string;71 /** Solid fill in place of the gradient ground; "transparent" drops it. */72 background?: string;73 /** Typeface for the splits, timer and banner. Load it yourself; Geist by default. */74 fontFamily?: string;75 /** Label above the personal-best time. */76 pbLabel?: string;77 /** Corner radius of the splits panel, in px at 1280×720. */78 radius?: number;79 /** Replaces the acme mark in the header. */80 logo?: React.ReactNode;81 /** Size of the timer and splits, around the frame's center. */82 scale?: number;83 /** Frames to hold the first frame before anything moves. */84 delay?: number;85 theme?: "dark" | "light";86 /** gradients.sh preset id for the ground. */87 ground?: GshPresetId;88 /** Overrides for the ground shader. */89 groundParams?: Partial<GradientParams>;90 speed?: number;91}9293const DEFAULT_SPLITS: SpeedrunSplit[] = [94 { name: "Script", time: 521, pb: 563 },95 { name: "Edit", time: 1635, pb: 1703 },96 { name: "Captions", time: 2162, pb: 2143 },97 { name: "Render", time: 2910, pb: 3041 },98 { name: "Ship", time: 3552, pb: 3737 },99];100101const BEATS = {102 rowIn: 0,103 rowGap: 3,104 run: 12,105 splits: [30, 50, 66, 84, 102],106 banner: 106,107 rise: 102,108} as const;109110const LIGHT = Easing.bezier(0.22, 1, 0.36, 1);111const ENTER = Easing.bezier(0.32, 0.72, 0, 1);112const HEAVY = { mass: 1, damping: 26, stiffness: 120 };113const CARD = { mass: 0.8, damping: 18, stiffness: 160 };114const CHIP = { mass: 0.5, damping: 12, stiffness: 200 };115const CLAMP = {116 extrapolateLeft: "clamp",117 extrapolateRight: "clamp",118} as const;119120const THEMES = {121 dark: {122 stage: "#070302",123 ink: "#F6F1EA",124 dim: "#F6F1EA80",125 faint: "#F6F1EA52",126 rule: "#FFFFFF12",127 band: "#FFFFFF0F",128 panel: "rgba(14, 9, 7, 0.62)",129 panelShadow:130 "inset 0 1px 0 rgba(255,255,255,0.07), 0 0 0 1px rgba(255,255,255,0.08), 0 2px 6px rgba(0,0,0,0.3), 0 24px 60px -18px rgba(0,0,0,0.65)",131 ahead: "#7DDBA3",132 behind: "#FF8A7A",133 gold: "#FFCF7A",134 flash: "#FFB36B",135 flashBlend: "screen",136 flashAlpha: 0.55,137 preset: "kiln" as GshPresetId,138 params: {139 colors: ["#4A0802", "#C92E0C", "#F2802E", "#FFB25E"],140 } as Partial<GradientParams>,141 veilFrom: 0.72,142 veilTo: 0.42,143 },144 light: {145 stage: "#F3EEE7",146 ink: "#1A120C",147 dim: "#1A120C8C",148 faint: "#1A120C5C",149 rule: "#0000000F",150 band: "#0000000A",151 panel: "rgba(255, 252, 248, 0.72)",152 panelShadow:153 "0 0 0 1px rgba(0,0,0,0.06), 0 1px 3px rgba(0,0,0,0.05), 0 20px 50px -20px rgba(60,30,10,0.25)",154 ahead: "#16884A",155 behind: "#C93A2B",156 gold: "#A86A00",157 flash: "#FFC07A",158 flashBlend: "multiply",159 flashAlpha: 0.45,160 preset: "paper-crease" as GshPresetId,161 params: {162 colors: ["#FF9A4A", "#FFC89A", "#F7E4D2", "#6A2A10"],163 background: "#F3EEE7",164 } as Partial<GradientParams>,165 veilFrom: 0.6,166 veilTo: 0.15,167 },168} as const;169170function ramp(t: number, from: number, len: number) {171 return interpolate(t, [from, from + len], [0, 1], CLAMP);172}173174function kick(d: number) {175 if (d < 0) return 0;176 if (d < 2) return d / 2;177 const k = Math.max(0, 1 - (d - 2) / 8);178 return k * k;179}180181function shake(d: number, amp: number, seed: number) {182 if (d < 0 || d > 9) return { x: 0, y: 0 };183 const k = (1 - d / 9) ** 2;184 return {185 x: amp * k * Math.sin(d * 2.7 + seed),186 y: amp * 0.7 * k * Math.cos(d * 3.3 + seed * 1.7),187 };188}189190function clockParts(sec: number) {191 const s = Math.max(0, Math.floor(sec));192 const h = Math.floor(s / 3600);193 const m = Math.floor((s % 3600) / 60);194 const r = s % 60;195 return {196 head: `${h}:${String(m).padStart(2, "0")}`,197 tail: `:${String(r).padStart(2, "0")}`,198 };199}200201function clock(sec: number) {202 const p = clockParts(sec);203 return p.head + p.tail;204}205206function delta(sec: number) {207 const a = Math.abs(Math.round(sec));208 const sign = sec < 0 ? "−" : "+";209 return `${sign}${Math.floor(a / 60)}:${String(a % 60).padStart(2, "0")}`;210}211212function Tabular({ text }: { text: string }) {213 return (214 <>215 {text.split("").map((ch, i) => (216 <span217 key={i}218 style={{219 display: "inline-block",220 width: ch === ":" ? "0.3em" : "0.6em",221 textAlign: "center",222 }}223 >224 {ch}225 </span>226 ))}227 </>228 );229}230231/** The foreground at `scale`, around the frame's center; nothing extra at 1. */232function ScaleLayer({233 scale,234 children,235}: {236 scale: number;237 children: React.ReactNode;238}) {239 if (scale === 1) return children;240 return (241 <AbsoluteFill style={{ transform: `scale(${scale})` }}>242 {children}243 </AbsoluteFill>244 );245}246247export function SpeedrunTimer({248 category = "Launch video · Any%",249 splits = DEFAULT_SPLITS,250 banner = "World record",251 accent = "#3B5BFD",252 ink,253 background,254 fontFamily = geist,255 pbLabel = "Personal best",256 radius: panelRadius = 18,257 logo,258 scale = 1,259 delay = 0,260 theme = "dark",261 ground,262 groundParams,263 speed = 1,264}: SpeedrunTimerProps) {265 const t = Math.max(0, useCurrentFrame() - delay) * speed;266 const { width: W, height: H, fps } = useVideoConfig();267 const c = THEMES[theme];268 const fg = ink ?? c.ink;269 const stage =270 background === undefined271 ? c.stage272 : background === "transparent"273 ? "transparent"274 : background;275 const tall = H > W;276 const u = Math.min(W / 1280, H / 720);277278 const n = splits.length;279 const last = BEATS.splits[BEATS.splits.length - 1];280 const frames = splits.map((_, i) => BEATS.splits[i] ?? last + 18 * (i - 4));281 const finishAt = frames[n - 1];282 const done = t >= finishAt;283284 const runTime = interpolate(285 t,286 [BEATS.run, ...frames],287 [0, ...splits.map((s) => s.time)],288 CLAMP,289 );290291 let bump = 0;292 frames.forEach((f, i) => {293 bump = Math.max(bump, (i === n - 1 ? 1.3 : 0.3) * kick(t - f));294 });295 const hit = shake(t - finishAt, 9 * u, 1.3);296 const camScale = 1 + 0.025 * bump;297 const flash = done ? Math.max(0, 1 - (t - finishAt) / 4) ** 2 : 0;298299 let tint = 0;300 let tintColor: string = fg;301 frames.forEach((f, i) => {302 const d = t - f;303 if (d >= 0 && d < 10 && i < n - 1) {304 tint = (1 - d / 10) ** 2;305 tintColor = splits[i].time <= splits[i].pb ? c.ahead : c.behind;306 }307 });308 const timerColor = done ? c.gold : fg;309310 const wr = spring({ frame: t - BEATS.banner, fps, config: CARD });311 const blinkOff =312 (t >= BEATS.banner + 6 && t < BEATS.banner + 8) ||313 (t >= BEATS.banner + 11 && t < BEATS.banner + 13);314 const wrOpacity =315 t < BEATS.banner ? 0 : blinkOff ? 0 : ramp(t, BEATS.banner, 2);316317 const rise = LIGHT(ramp(t, BEATS.rise, 28));318 const veil = c.veilFrom + (c.veilTo - c.veilFrom) * rise;319 const groundShift = ((tall ? 0.1 : 0.08) + (1 - rise) * 0.12) * H;320321 const L = tall322 ? {323 pad: 0.07 * W,324 headerTop: 0.07 * H,325 panelTop: 0.135 * H,326 panelW: 0.86 * W,327 rowH: 0.052 * H,328 rowFs: 0.024 * H,329 deltaFs: 0.019 * H,330 labelFs: 0.016 * H,331 logoFs: 0.022 * H,332 timerFs: 0.215 * W,333 timerTop: 0.555 * H,334 bannerFs: 0.072 * W,335 bannerTop: 0.49 * H,336 timeColW: 0.22 * W,337 deltaColW: 0.17 * W,338 }339 : {340 pad: 0.0625 * W,341 headerTop: 0.07 * H,342 panelTop: 0.15 * H,343 panelW: 0.56 * W,344 rowH: 0.074 * H,345 rowFs: 0.036 * H,346 deltaFs: 0.029 * H,347 labelFs: 0.024 * H,348 logoFs: 0.036 * H,349 timerFs: 0.3 * H,350 timerTop: 0.7 * H,351 bannerFs: 0.07 * H,352 bannerTop: 0.58 * H,353 timeColW: 0.12 * W,354 deltaColW: 0.1 * W,355 };356 const inset = 8 * u;357 const radius = panelRadius * u;358359 let bandIndex = 0;360 frames.forEach((f, i) => {361 if (i < n - 1) bandIndex += spring({ frame: t - f, fps, config: CARD });362 });363 const bandOpacity =364 ramp(t, BEATS.run - 4, 6) * (1 - LIGHT(ramp(t, finishAt, 6)));365366 const monoLabel: React.CSSProperties = {367 fontFamily: mono,368 fontWeight: 400,369 fontSize: L.labelFs,370 letterSpacing: "0.04em",371 textTransform: "uppercase",372 color: c.dim,373 lineHeight: 1,374 };375376 const panelIn = spring({ frame: t - BEATS.rowIn, fps, config: HEAVY });377 const pbParts = clockParts(splits[n - 1]?.pb ?? 0);378 const pbPop = spring({ frame: t - finishAt - 2, fps, config: CHIP });379 const timerText = clock(runTime);380381 return (382 <AbsoluteFill style={{ background: stage, overflow: "hidden" }}>383 {background === undefined ? (384 <>385 <AbsoluteFill386 style={{387 top: -0.3 * H,388 height: 1.55 * H,389 transform: `translateY(${groundShift - 0.25 * H}px) scale(1.06)`,390 }}391 >392 <GshGround393 preset={ground ?? c.preset}394 params={groundParams ?? c.params}395 timeOffset={5}396 timeScale={0.4}397 />398 </AbsoluteFill>399 <AbsoluteFill style={{ background: c.stage, opacity: veil }} />400 </>401 ) : null}402403 <ScaleLayer scale={scale}>404 <div405 style={{406 position: "absolute",407 inset: 0,408 transform: `translate(${hit.x}px, ${hit.y}px) scale(${camScale})`,409 transformOrigin: "60% 70%",410 }}411 >412 <div413 style={{414 position: "absolute",415 left: L.pad,416 right: L.pad,417 top: L.headerTop,418 display: "flex",419 alignItems: "center",420 justifyContent: "space-between",421 opacity: ENTER(ramp(t, 0, 10)),422 }}423 >424 <div style={{ display: "flex", alignItems: "center", gap: 16 * u }}>425 {logo ?? <KuchiLogo fontSize={L.logoFs} color={fg} />}426 <span style={monoLabel}>{category}</span>427 </div>428 </div>429430 {tall ? null : (431 <div432 style={{433 position: "absolute",434 right: L.pad,435 top: L.panelTop + inset + 0.012 * H,436 display: "flex",437 flexDirection: "column",438 alignItems: "flex-end",439 gap: 0.022 * H,440 opacity: ENTER(ramp(t, 4, 12)),441 transform: `translateY(${(1 - panelIn) * 0.04 * H}px)`,442 }}443 >444 <span style={monoLabel}>{pbLabel}</span>445 <span446 style={{447 fontFamily,448 fontWeight: 300,449 fontSize: 0.1 * H,450 lineHeight: 0.8,451 letterSpacing: "-0.03em",452 fontVariantNumeric: "tabular-nums",453 color: done ? c.faint : fg,454 textDecoration: done ? "line-through" : undefined,455 textDecorationThickness: 2 * u,456 }}457 >458 {pbParts.head}459 {pbParts.tail}460 </span>461 <span462 style={{463 fontFamily: mono,464 fontWeight: 400,465 fontSize: 0.036 * H,466 lineHeight: 1,467 fontVariantNumeric: "tabular-nums",468 color: c.gold,469 opacity: done ? ramp(t, finishAt + 2, 3) : 0,470 transform: `scale(${1.3 - 0.3 * pbPop})`,471 transformOrigin: "100% 50%",472 }}473 >474 {delta((splits[n - 1]?.time ?? 0) - (splits[n - 1]?.pb ?? 0))}475 </span>476 </div>477 )}478479 <div480 style={{481 position: "absolute",482 left: L.pad,483 top: L.panelTop,484 width: L.panelW,485 padding: inset,486 borderRadius: radius,487 background: c.panel,488 boxShadow: c.panelShadow,489 backdropFilter: "blur(18px)",490 WebkitBackdropFilter: "blur(18px)",491 opacity: ramp(t, 0, 4),492 transform: `translateY(${(1 - panelIn) * 0.05 * H}px)`,493 }}494 >495 <div style={{ position: "relative", height: n * L.rowH }}>496 <div497 style={{498 position: "absolute",499 left: 0,500 right: 0,501 top: bandIndex * L.rowH,502 height: L.rowH,503 borderRadius: radius - inset,504 background: c.band,505 opacity: bandOpacity,506 }}507 >508 <div509 style={{510 position: "absolute",511 left: 9 * u,512 top: "28%",513 bottom: "28%",514 width: 4 * u,515 borderRadius: 2 * u,516 background: accent,517 }}518 />519 </div>520 {splits.map((s, i) => {521 const at = frames[i];522 const isLast = i === n - 1;523 const completed = t >= at;524 const inK = spring({525 frame: t - BEATS.rowIn - 2 - i * BEATS.rowGap,526 fps,527 config: CARD,528 });529 const pop = spring({ frame: t - at, fps, config: CHIP });530 const ahead = s.time <= s.pb;531 const deltaColor = isLast ? c.gold : ahead ? c.ahead : c.behind;532 const rowFlash = completed533 ? Math.max(0, 1 - (t - at) / 12) ** 2534 : 0;535 const rowInk = isLast && completed ? c.gold : fg;536 const parts = clockParts(completed ? s.time : s.pb);537 return (538 <div539 key={s.name}540 style={{541 position: "absolute",542 left: 0,543 right: 0,544 top: i * L.rowH,545 height: L.rowH,546 display: "flex",547 alignItems: "center",548 padding: `0 ${22 * u}px 0 ${26 * u}px`,549 borderTop: i === 0 ? undefined : `1px solid ${c.rule}`,550 opacity: ramp(t, BEATS.rowIn + 2 + i * BEATS.rowGap, 4),551 transform: `translateY(${(1 - inK) * 0.4 * L.rowH}px)`,552 }}553 >554 <div555 style={{556 position: "absolute",557 inset: 0,558 borderRadius: radius - inset,559 background: deltaColor,560 opacity: 0.16 * rowFlash,561 }}562 />563 <div564 style={{565 flex: 1,566 fontFamily,567 fontWeight: 400,568 fontSize: L.rowFs,569 letterSpacing: "-0.01em",570 color: rowInk,571 lineHeight: 1,572 }}573 >574 {s.name}575 </div>576 <div577 style={{578 width: L.deltaColW,579 textAlign: "right",580 fontFamily: mono,581 fontWeight: 400,582 fontSize: L.deltaFs,583 fontVariantNumeric: "tabular-nums",584 color: deltaColor,585 lineHeight: 1,586 opacity: completed ? ramp(t, at, 2) : 0,587 transform: `scale(${1.3 - 0.3 * pop})`,588 transformOrigin: "100% 50%",589 }}590 >591 {delta(s.time - s.pb)}592 </div>593 <div594 style={{595 width: L.timeColW,596 textAlign: "right",597 fontFamily,598 fontWeight: 400,599 fontSize: L.rowFs,600 fontVariantNumeric: "tabular-nums",601 color: completed ? rowInk : c.faint,602 lineHeight: 1,603 }}604 >605 {parts.head}606 <span style={{ opacity: completed ? 0.55 : 1 }}>607 {parts.tail}608 </span>609 </div>610 </div>611 );612 })}613 </div>614 </div>615616 <div617 style={{618 position: "absolute",619 right: L.pad,620 top: L.bannerTop,621 fontFamily,622 fontWeight: 300,623 fontSize: L.bannerFs,624 lineHeight: 1,625 letterSpacing: "0.01em",626 textTransform: "uppercase",627 whiteSpace: "nowrap",628 color: c.gold,629 opacity: wrOpacity,630 transform: `scale(${1.35 - 0.35 * wr})`,631 transformOrigin: "100% 100%",632 filter:633 wr < 0.97634 ? `blur(${Math.max(0, 1 - wr) * 10 * u}px)`635 : undefined,636 }}637 >638 {banner}639 </div>640641 <div642 style={{643 position: "absolute",644 right: L.pad - 0.02 * L.timerFs,645 top: L.timerTop,646 fontFamily,647 fontWeight: 300,648 fontSize: L.timerFs,649 lineHeight: 0.8,650 letterSpacing: "-0.03em",651 color: t < BEATS.run ? c.dim : timerColor,652 whiteSpace: "nowrap",653 }}654 >655 <Tabular text={timerText} />656 {tint > 0.01 ? (657 <div658 style={{659 position: "absolute",660 inset: 0,661 color: tintColor,662 opacity: tint,663 }}664 >665 <Tabular text={timerText} />666 </div>667 ) : null}668 </div>669 </div>670 </ScaleLayer>671672 {flash > 0.01 ? (673 <AbsoluteFill674 style={{675 background: c.flash,676 mixBlendMode: c.flashBlend,677 opacity: c.flashAlpha * flash,678 }}679 />680 ) : null}681 </AbsoluteFill>682 );683}









