Rotary Dial
A knurled dial turns to 10 while its arc heats up and 10x render speed counts up.
Source
components/kuchi/rotary-dial.tsx
1"use client";23import { loadFont } from "@remotion/google-fonts/SpaceGrotesk";4import type React from "react";5import {6 AbsoluteFill,7 Easing,8 interpolate,9 spring,10 useCurrentFrame,11 useVideoConfig,12} from "remotion";13import type { GradientParams } from "@/lib/kuchi/gsh/gradient-engine";14import { GshGround } from "@/lib/kuchi/gsh/gsh-ground";15import type { GshPresetId } from "@/lib/kuchi/gsh/presets";1617const { fontFamily: sans } = loadFont("normal", {18 weights: ["300", "400"],19 subsets: ["latin"],20});2122export interface RotaryDialProps {23 from?: number;24 to?: number;25 unit?: string;26 label?: string;27 accent?: string;28 /** Frames the knob takes to turn from `from` to `to`. */29 turn?: number;30 /** Minor ticks per labelled step on the scale. */31 minorTicks?: number;32 /** Text color of the value and the reached end label; overrides the theme. */33 ink?: string;34 /** Solid fill in place of the gradient ground; "transparent" drops it. */35 background?: string;36 /** Typeface for the copy. Load it yourself; Space Grotesk by default. */37 fontFamily?: string;38 /** Size of the foreground around the frame's center. */39 scale?: number;40 /** Frames to hold the first frame before anything moves. */41 delay?: number;42 theme?: "dark" | "light";43 ground?: GshPresetId;44 groundParams?: Partial<GradientParams>;45 speed?: number;46}4748const ENTER = Easing.bezier(0.32, 0.72, 0, 1);49const MOVE = Easing.bezier(0.65, 0, 0.35, 1);50const CLAMP = {51 extrapolateLeft: "clamp",52 extrapolateRight: "clamp",53} as const;5455const BEATS = {56 label: 6,57 value: 12,58 grab: 18,59 turn: 24,60 land: 72,61} as const;6263const HEAVY = { mass: 1, damping: 26, stiffness: 120 };64const CHIP = { mass: 0.5, damping: 12, stiffness: 200 };6566const THEMES = {67 dark: {68 fg: "#F3F2EF",69 muted: "rgba(243,242,239,0.56)",70 lit: "#9DC2FF",71 tick: "rgba(255,255,255,0.2)",72 scrim: "4,6,12",73 scrimFrom: 0.86,74 scrimTo: 0.4,75 collar: "#0E0F13",76 collarLip: "rgba(255,255,255,0.09)",77 knurl: ["#17181C", "#34353B"],78 metal: { mid: "#3E3F45", lo: "#27282C", hi: "#7C7D85", hi2: "#5A5B62" },79 chamfer: ["rgba(255,255,255,0.4)", "rgba(0,0,0,0.6)"],80 shadow: "0,0,0",81 shadowA: 0.8,82 },83 light: {84 fg: "#141413",85 muted: "rgba(20,20,19,0.52)",86 lit: "",87 tick: "rgba(30,26,20,0.2)",88 scrim: "244,243,239",89 scrimFrom: 0.88,90 scrimTo: 0.36,91 collar: "#DEDBD3",92 collarLip: "rgba(255,255,255,0.95)",93 knurl: ["#8F8E92", "#C9C8CC"],94 metal: { mid: "#C4C4C8", lo: "#A5A5AA", hi: "#F4F4F6", hi2: "#DCDCE0" },95 chamfer: ["rgba(255,255,255,0.95)", "rgba(40,36,30,0.35)"],96 shadow: "48,40,28",97 shadowA: 0.36,98 },99} as const;100101function hexToRgb(hex: string) {102 const h = hex.replace("#", "");103 const n = Number.parseInt(104 h.length === 3105 ? h106 .split("")107 .map((c) => c + c)108 .join("")109 : h,110 16,111 );112 return `${(n >> 16) & 255},${(n >> 8) & 255},${n & 255}`;113}114115function niceStep(range: number) {116 const raw = range / 9;117 const pow = 10 ** Math.floor(Math.log10(raw));118 const n = raw / pow;119 const k = n <= 1 ? 1 : n <= 2 ? 2 : n <= 2.5 ? 2.5 : n <= 5 ? 5 : 10;120 return k * pow;121}122123function formatLabel(v: number) {124 return Number.isInteger(v) ? String(v) : v.toFixed(1);125}126127function Odometer({128 value,129 decimals,130 size,131}: {132 value: number;133 decimals: number;134 size: number;135}) {136 const m = 10 ** decimals;137 const scaled = value * m;138 const k = Math.floor(scaled + 1e-6);139 const frac = Math.max(0, scaled - k);140 const r = interpolate(frac, [0.45, 1], [0, 1], { ...CLAMP, easing: MOVE });141 const fmt = (n: number) => {142 const s = String(Math.abs(n)).padStart(decimals + 1, "0");143 return decimals ? `${s.slice(0, -decimals)}.${s.slice(-decimals)}` : s;144 };145 const a = fmt(k);146 const b = fmt(k + 1);147 const len = Math.max(a.length, b.length);148 const pa = a.padStart(len, " ");149 const pb = b.padStart(len, " ");150 const colW = 0.62 * size;151 const cols: React.ReactNode[] = [];152 for (let i = 0; i < len; i++) {153 const ca = pa[i];154 const cb = pb[i];155 const isDot = ca === "." || cb === ".";156 const w = isDot157 ? 0.28 * size158 : ca === " "159 ? colW * r160 : cb === " "161 ? colW * (1 - r)162 : colW;163 const rolling = ca !== cb;164 const blur = Math.sin(r * Math.PI) * size * 0.03;165 cols.push(166 <span167 key={i}168 style={{169 position: "relative",170 display: "inline-block",171 width: w,172 height: size,173 textAlign: "center",174 }}175 >176 {rolling ? (177 <>178 <span179 style={{180 position: "absolute",181 left: -0.2 * size,182 right: -0.2 * size,183 top: 0,184 transform: `translateY(${-r * size * 0.42}px)`,185 opacity: (1 - r) ** 2,186 filter: blur > 0.2 ? `blur(${blur}px)` : undefined,187 }}188 >189 {ca}190 </span>191 <span192 style={{193 position: "absolute",194 left: -0.2 * size,195 right: -0.2 * size,196 top: 0,197 transform: `translateY(${(1 - r) * size * 0.42}px)`,198 opacity: r ** 1.5,199 filter: blur > 0.2 ? `blur(${blur}px)` : undefined,200 }}201 >202 {cb}203 </span>204 </>205 ) : (206 <span207 style={{208 position: "absolute",209 left: -0.2 * size,210 right: -0.2 * size,211 top: 0,212 }}213 >214 {ca}215 </span>216 )}217 </span>,218 );219 }220 return <>{cols}</>;221}222223/** The foreground at `scale`, around the frame's center; nothing extra at 1. */224function ScaleLayer({225 scale,226 children,227}: {228 scale: number;229 children: React.ReactNode;230}) {231 if (scale === 1) return children;232 return (233 <AbsoluteFill style={{ transform: `scale(${scale})` }}>234 {children}235 </AbsoluteFill>236 );237}238239export function RotaryDial({240 from = 1,241 to = 10,242 unit = "×",243 label = "Render speed",244 accent = "#3B5BFD",245 turn = BEATS.land - BEATS.turn,246 minorTicks = 5,247 ink: inkColor,248 background,249 fontFamily = sans,250 scale = 1,251 delay = 0,252 theme = "dark",253 ground,254 groundParams,255 speed = 1,256}: RotaryDialProps) {257 const t = Math.max(0, useCurrentFrame() - delay) * speed;258 const { width: VW, height: VH, fps } = useVideoConfig();259 const tall = VH > VW;260 const u = tall ? VW / 760 : Math.min(VW / 1280, VH / 720);261 const p = THEMES[theme];262 const fg = inkColor ?? p.fg;263 // The grab ramp eases off over the last 6 frames of the turn, so it needs more than 6.264 const land = BEATS.turn + Math.max(7, turn);265 const dark = theme === "dark";266 const acc = hexToRgb(accent);267 const lit = p.lit || accent;268 const resolvedGround: GshPresetId | undefined =269 ground ?? (groundParams ? undefined : dark ? "earthshine" : "paper-crease");270271 const range = to - from;272 const span = Math.abs(range) || 1;273 const step = niceStep(span);274 const decimals = span < 5 ? 1 : 0;275 const lo = Math.min(from, to);276 const hi = Math.max(from, to);277278 const A0 = -135;279 const A1 = 135;280 const prog = (f: number) => {281 const move = interpolate(f, [BEATS.turn, land], [0, 1.025], {282 ...CLAMP,283 easing: MOVE,284 });285 const detent = spring({ frame: f - land, fps, config: CHIP });286 return move - 0.025 * Math.min(1, detent);287 };288 const k = prog(t);289 const kc = Math.min(1, k);290 const angle = A0 + (A1 - A0) * k;291 const angVel = Math.abs(prog(t + 0.5) - prog(t - 0.5)) * (A1 - A0);292 const value = Math.min(hi, Math.max(lo, from + range * kc));293 const rest = Math.min(1, angVel / 8);294295 const grab = interpolate(296 t,297 [BEATS.grab, BEATS.turn, land - 6, land],298 [0, 1, 1, 0],299 { ...CLAMP, easing: MOVE },300 );301 const click = spring({ frame: t - land, fps, config: CHIP });302 const clickPunch = Math.sin(Math.min(1, click) * Math.PI) * 0.025;303 const knobScale = 1 - grab * 0.03 + clickPunch;304305 const R = (tall ? 190 : 140) * u;306 const rCollar = R + 12 * u;307 const rFace = R - 17 * u;308 const rTick = R + 32 * u;309 const rLabel = R + 66 * u;310 const box = (rLabel + 30 * u) * 2;311312 const ticks: React.ReactNode[] = [];313 const labels: React.ReactNode[] = [];314 const minorPer = minorTicks;315 const count = Math.round(span / step) * minorPer;316 for (let i = 0; i <= count; i++) {317 const frac = i / count;318 const a = A0 + (A1 - A0) * frac;319 const major = i % minorPer === 0;320 const behind = Math.min(angle, A1) - a;321 const on = interpolate(behind, [-1.2, 0.6], [0, 1], CLAMP);322 const flash = on * rest * Math.exp(-Math.max(0, behind) / 22);323 const len = (major ? 20 : 11) * u * (1 + flash * 0.3);324 const wid = (major ? 3 : 2) * u;325 ticks.push(326 <div327 key={i}328 style={{329 position: "absolute",330 left: box / 2 - wid / 2,331 top: box / 2 - rTick - len,332 width: wid,333 height: len,334 borderRadius: wid,335 transformOrigin: `50% ${rTick + len}px`,336 transform: `rotate(${a}deg)`,337 background: on > 0.01 ? lit : p.tick,338 opacity: on > 0.01 ? 0.5 + 0.5 * on : 1,339 }}340 />,341 );342 if (i === 0 || i === count) {343 const v = from + (range * i) / count;344 const rad = (a * Math.PI) / 180;345 const x = box / 2 + Math.sin(rad) * rLabel;346 const y = box / 2 - Math.cos(rad) * rLabel;347 labels.push(348 <div349 key={`l${i}`}350 style={{351 position: "absolute",352 left: x - 40 * u,353 top: y - 12 * u + 14 * u,354 width: 80 * u,355 height: 24 * u,356 lineHeight: `${24 * u}px`,357 textAlign: "center",358 fontFamily,359 fontWeight: 400,360 fontSize: 18 * u,361 fontVariantNumeric: "tabular-nums",362 color: on > 0.5 ? fg : p.muted,363 }}364 >365 {formatLabel(v)}366 {unit}367 </div>,368 );369 }370 }371372 const m = p.metal;373 const circle = (r: number): React.CSSProperties => ({374 position: "absolute",375 left: box / 2 - r,376 top: box / 2 - r,377 width: r * 2,378 height: r * 2,379 borderRadius: "50%",380 });381 const ridge = 3;382383 const cam = spring({384 frame: t,385 fps,386 config: HEAVY,387 durationInFrames: 54,388 });389 const push = interpolate(t, [land, land + 60], [0, 1], {390 ...CLAMP,391 easing: MOVE,392 });393 const camK = interpolate(cam, [0, 1], [1.08, 1]) + push * 0.02;394 const light = interpolate(t, [BEATS.turn, land + 6], [0, 1], {395 ...CLAMP,396 easing: ENTER,397 });398 const scrim = interpolate(light, [0, 1], [p.scrimFrom, p.scrimTo]);399 const bloom = interpolate(t, [land, land + 4, land + 40], [0, 1, 0.55], {400 ...CLAMP,401 easing: ENTER,402 });403404 const word = (start: number) =>405 interpolate(t, [start, start + 14], [0, 1], { ...CLAMP, easing: ENTER });406 const ink = (v: number): React.CSSProperties => ({407 opacity: v,408 transform: `translateY(${(1 - v) * 0.22}em)`,409 filter: v < 1 ? `blur(${(1 - v) * 6 * u}px)` : undefined,410 });411 const wLabel = word(BEATS.label);412 const wValue = word(BEATS.value);413 const numSize = (tall ? 168 : 150) * u;414415 const dial = (416 <div417 style={{418 position: "relative",419 width: box,420 height: box,421 flexShrink: 0,422 }}423 >424 {ticks}425 {labels}426 <div427 style={{428 ...circle(rCollar),429 background: p.collar,430 boxShadow: [431 `inset 0 ${3 * u}px ${6 * u}px rgba(${p.shadow},${p.shadowA})`,432 `inset 0 ${-1.2 * u}px 0 ${p.collarLip}`,433 `0 0 ${70 * u}px ${-6 * u}px rgba(${acc},${0.5 * bloom})`,434 ].join(", "),435 }}436 />437 <div438 style={{439 ...circle(rCollar),440 transform: `scale(${knobScale})`,441 }}442 >443 <div444 style={{445 position: "absolute",446 left: rCollar - R,447 top: rCollar - R,448 width: R * 2,449 height: R * 2,450 borderRadius: "50%",451 boxShadow: [452 `0 ${(16 - grab * 6) * u}px ${(30 - grab * 8) * u}px rgba(${p.shadow},${p.shadowA * 0.7})`,453 `0 ${3 * u}px ${6 * u}px rgba(${p.shadow},${p.shadowA * 0.6})`,454 ].join(", "),455 overflow: "hidden",456 }}457 >458 <div459 style={{460 position: "absolute",461 inset: 0,462 background: `repeating-conic-gradient(from ${angle}deg, ${p.knurl[0]} 0deg ${ridge / 2}deg, ${p.knurl[1]} ${ridge / 2}deg ${ridge}deg)`,463 filter: rest > 0.05 ? `blur(${rest * 1.6 * u}px)` : undefined,464 opacity: 1 - rest * 0.35,465 }}466 />467 <div468 style={{469 position: "absolute",470 inset: 0,471 background:472 "linear-gradient(180deg, rgba(255,255,255,0.3) 0%, rgba(255,255,255,0.04) 40%, rgba(0,0,0,0.35) 78%, rgba(0,0,0,0.55) 100%)",473 }}474 />475 </div>476 <div477 style={{478 position: "absolute",479 left: rCollar - rFace - 3 * u,480 top: rCollar - rFace - 3 * u,481 width: (rFace + 3 * u) * 2,482 height: (rFace + 3 * u) * 2,483 borderRadius: "50%",484 background: `linear-gradient(180deg, ${p.chamfer[0]}, ${p.chamfer[1]})`,485 }}486 />487 <div488 style={{489 position: "absolute",490 left: rCollar - rFace,491 top: rCollar - rFace,492 width: rFace * 2,493 height: rFace * 2,494 borderRadius: "50%",495 background: [496 `repeating-radial-gradient(circle at 50% 50%, rgba(255,255,255,0.035) 0 ${0.8 * u}px, rgba(0,0,0,0.04) ${0.8 * u}px ${1.7 * u}px)`,497 "radial-gradient(circle at 50% 42%, rgba(255,255,255,0.06), transparent 60%)",498 `conic-gradient(from 0deg, ${m.mid} 0deg, ${m.lo} 62deg, ${m.mid} 108deg, ${m.hi2} 150deg, ${m.mid} 192deg, ${m.lo} 244deg, ${m.mid} 290deg, ${m.hi} 328deg, ${m.mid} 360deg)`,499 ].join(", "),500 boxShadow: `inset 0 ${1.5 * u}px ${2 * u}px rgba(0,0,0,0.35)`,501 }}502 />503 <div504 style={{505 position: "absolute",506 left: rCollar - rFace,507 top: rCollar - rFace,508 width: rFace * 2,509 height: rFace * 2,510 transform: `rotate(${angle}deg)`,511 }}512 >513 <div514 style={{515 position: "absolute",516 left: rFace - 3.5 * u,517 top: 12 * u,518 width: 7 * u,519 height: 40 * u,520 borderRadius: 4 * u,521 background: lit,522 boxShadow: `inset 0 ${1.5 * u}px ${2 * u}px rgba(0,0,0,0.35), 0 0 ${10 * u * bloom}px rgba(${acc},${0.6 * bloom})`,523 }}524 />525 </div>526 </div>527 </div>528 );529530 const readout = (531 <div532 style={{533 display: "flex",534 flexDirection: "column",535 alignItems: tall ? "center" : "flex-start",536 gap: 4 * u,537 minWidth: tall ? undefined : 300 * u,538 }}539 >540 <div541 style={{542 ...ink(wLabel),543 fontFamily,544 fontWeight: 400,545 fontSize: (tall ? 34 : 28) * u,546 letterSpacing: "-0.01em",547 color: p.muted,548 paddingLeft: tall ? 0 : 6 * u,549 }}550 >551 {label}552 </div>553 <div554 style={{555 ...ink(wValue),556 display: "flex",557 alignItems: "baseline",558 fontFamily,559 fontWeight: 300,560 fontSize: numSize,561 lineHeight: `${numSize}px`,562 letterSpacing: "-0.04em",563 fontVariantNumeric: "tabular-nums",564 color: fg,565 whiteSpace: "nowrap",566 }}567 >568 <Odometer value={value} decimals={decimals} size={numSize} />569 <span570 style={{571 color: lit,572 fontSize: numSize * 0.6,573 lineHeight: `${numSize}px`,574 marginLeft: 0.06 * numSize,575 }}576 >577 {unit}578 </span>579 </div>580 </div>581 );582583 return (584 <AbsoluteFill585 style={{586 overflow: "hidden",587 background: background ?? (dark ? "#03050b" : "#eeece8"),588 }}589 >590 {background === undefined ? (591 <>592 <GshGround593 preset={resolvedGround}594 params={groundParams}595 timeOffset={dark ? 3 : 6}596 timeScale={0.5}597 />598 <AbsoluteFill599 style={{600 background: `radial-gradient(70% 60% at 50% 45%, rgba(${p.scrim},${scrim}) 0%, rgba(${p.scrim},${Math.min(1, scrim + 0.12)}) 100%)`,601 }}602 />603 </>604 ) : null}605 <ScaleLayer scale={scale}>606 <AbsoluteFill607 style={{608 alignItems: "center",609 justifyContent: "center",610 transform: `scale(${camK})`,611 }}612 >613 <div614 style={{615 display: "flex",616 flexDirection: tall ? "column" : "row",617 alignItems: "center",618 gap: (tall ? 40 : 40) * u,619 transform: tall ? `translateY(${-150 * u}px)` : undefined,620 }}621 >622 {dial}623 {readout}624 </div>625 </AbsoluteFill>626 </ScaleLayer>627 </AbsoluteFill>628 );629}









