Toggle on
A large switch flips on and lights the stage as the Auto-captions label turns On.
Source
components/kuchi/toggle-on.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 ToggleOnProps {23 label?: string;24 state?: string;25 accent?: string;26 /** Weight of the label and state. */27 fontWeight?: number;28 /** Frames between the label and the state word. */29 stagger?: number;30 /** Text color of the label; overrides the theme. */31 ink?: string;32 /** Solid fill in place of the gradient ground; "transparent" drops it. */33 background?: string;34 /** Typeface for the label. Load it yourself; Space Grotesk by default. */35 fontFamily?: string;36 /** Size of the foreground around the frame's center. */37 scale?: number;38 /** Frames to hold the first frame before anything moves. */39 delay?: number;40 theme?: "dark" | "light";41 ground?: GshPresetId;42 groundParams?: Partial<GradientParams>;43 speed?: number;44}4546const ENTER = Easing.bezier(0.32, 0.72, 0, 1);47const MOVE = Easing.bezier(0.65, 0, 0.35, 1);48const CLAMP = {49 extrapolateLeft: "clamp",50 extrapolateRight: "clamp",51} as const;5253const BEATS = {54 press: 12,55 flip: 24,56 light: 26,57 label: 34,58 state: 42,59} as const;6061const THEMES = {62 dark: {63 fg: "#F3F2EF",64 stateInk: "#9DC2FF",65 scrim: "4,6,12",66 scrimFrom: 0.86,67 scrimTo: 0.34,68 track: "#121317",69 trackTop: "rgba(0,0,0,0.7)",70 trackLip: "rgba(255,255,255,0.12)",71 bezel: "#191A1F",72 rim: "rgba(255,255,255,0.09)",73 knob: ["#FFFFFF", "#F1F0ED", "#D4D3CE"],74 shadow: "0,0,0",75 shadowA: 0.75,76 },77 light: {78 fg: "#141413",79 stateInk: "",80 scrim: "244,243,239",81 scrimFrom: 0.88,82 scrimTo: 0.32,83 track: "#E0DED7",84 trackTop: "rgba(70,58,38,0.28)",85 trackLip: "rgba(255,255,255,0.95)",86 bezel: "#ECEAE4",87 rim: "rgba(255,255,255,0.9)",88 knob: ["#FFFFFF", "#FAF9F6", "#DAD8D1"],89 shadow: "48,40,28",90 shadowA: 0.34,91 },92} as const;9394function hexToRgb(hex: string) {95 const h = hex.replace("#", "");96 const n = Number.parseInt(97 h.length === 398 ? h99 .split("")100 .map((c) => c + c)101 .join("")102 : h,103 16,104 );105 return `${(n >> 16) & 255},${(n >> 8) & 255},${n & 255}`;106}107108/** The foreground at `scale`, around the frame's center; nothing extra at 1. */109function ScaleLayer({110 scale,111 children,112}: {113 scale: number;114 children: React.ReactNode;115}) {116 if (scale === 1) return children;117 return (118 <AbsoluteFill style={{ transform: `scale(${scale})` }}>119 {children}120 </AbsoluteFill>121 );122}123124export function ToggleOn({125 label = "Auto-captions",126 state = "On",127 accent = "#3B5BFD",128 fontWeight = 400,129 stagger = BEATS.state - BEATS.label,130 ink: inkColor,131 background,132 fontFamily = sans,133 scale = 1,134 delay = 0,135 theme = "dark",136 ground,137 groundParams,138 speed = 1,139}: ToggleOnProps) {140 const t = Math.max(0, useCurrentFrame() - delay) * speed;141 const { width: VW, height: VH, fps } = useVideoConfig();142 const tall = VH > VW;143 const u = tall ? VW / 760 : Math.min(VW / 1280, VH / 720);144 const p = THEMES[theme];145 const fg = inkColor ?? p.fg;146 const dark = theme === "dark";147 const acc = hexToRgb(accent);148 const stateInk = p.stateInk || accent;149 const resolvedGround: GshPresetId | undefined =150 ground ?? (groundParams ? undefined : dark ? "earthshine" : "paper-crease");151152 const W = 330 * u;153 const H = 192 * u;154 const pad = 13 * u;155 const d = H - pad * 2;156 const travel = W - pad * 2 - d;157158 const press = interpolate(t, [BEATS.press, BEATS.flip], [0, 1], {159 ...CLAMP,160 easing: ENTER,161 });162 const pos = spring({163 frame: t - BEATS.flip,164 fps,165 config: { mass: 0.8, damping: 18, stiffness: 160 },166 });167 const relax = spring({168 frame: t - BEATS.flip - 6,169 fps,170 config: { mass: 0.5, damping: 12, stiffness: 200 },171 });172 const stretch = d * 0.2 * press * (1 - Math.min(1, relax));173 const posC = Math.min(1, pos);174 const over = Math.max(0, pos - 1);175 const knobLeft = pad + posC * (travel - stretch) + over * travel;176 const knobRight = Math.min(177 W - pad,178 pad + posC * travel + d + stretch * (1 - posC),179 );180 const knobW = knobRight - knobLeft;181182 const fillEnd = knobLeft + knobW * (0.5 + 0.5 * posC) + pad * 1.2 * posC;183 const fillOn = interpolate(pos, [0, 0.05], [0, 1], CLAMP);184 const fillStart = pad * 0.6 * (1 - Math.min(1, pos * 3));185186 const cam = spring({187 frame: t,188 fps,189 config: { mass: 1, damping: 26, stiffness: 120 },190 durationInFrames: 54,191 });192 const camK = interpolate(cam, [0, 1], [1.1, 1]);193 const lit = interpolate(t, [BEATS.light, BEATS.light + 30], [0, 1], {194 ...CLAMP,195 easing: ENTER,196 });197 const scrim = interpolate(lit, [0, 1], [p.scrimFrom, p.scrimTo]);198 const open = interpolate(t, [BEATS.flip + 2, BEATS.flip + 26], [0, 1], {199 ...CLAMP,200 easing: MOVE,201 });202203 const word = (start: number) =>204 interpolate(t, [start, start + 14], [0, 1], { ...CLAMP, easing: ENTER });205 const wLabel = word(BEATS.label);206 const wState = word(BEATS.label + stagger);207 const fontSize = (tall ? 92 : 76) * u;208209 const ink = (v: number) => ({210 display: "inline-block",211 opacity: v,212 transform: `translateY(${(1 - v) * 0.22}em)`,213 filter: v < 1 ? `blur(${(1 - v) * 6 * u}px)` : undefined,214 });215216 const knobShadow = [217 `0 0 0 ${0.6 * u}px rgba(${p.shadow},${p.shadowA * 0.35})`,218 `0 ${2 * u}px ${3 * u}px rgba(${p.shadow},${p.shadowA * 0.5})`,219 `0 ${10 * u}px ${20 * u}px rgba(${p.shadow},${p.shadowA * 0.45})`,220 `inset 0 ${-3 * u}px ${2 * u}px rgba(0,0,0,0.07)`,221 `inset 0 ${1.5 * u}px 0 rgba(255,255,255,1)`,222 ].join(", ");223224 const toggle = (225 <div226 style={{227 position: "relative",228 width: W + 14 * u,229 height: H + 14 * u,230 borderRadius: (H + 14 * u) / 2,231 background: p.bezel,232 boxShadow: [233 `inset 0 ${-1 * u}px 0 ${p.rim}`,234 `inset 0 ${1 * u}px ${1.5 * u}px rgba(${p.shadow},${p.shadowA * 0.25})`,235 `0 ${24 * u}px ${48 * u}px -${12 * u}px rgba(${p.shadow},${p.shadowA * 0.55})`,236 `0 0 ${60 * u}px ${-10 * u}px rgba(${acc},${0.45 * fillOn * lit})`,237 ].join(", "),238 flexShrink: 0,239 }}240 >241 <div242 style={{243 position: "absolute",244 left: 7 * u,245 top: 7 * u,246 width: W,247 height: H,248 borderRadius: H / 2,249 overflow: "hidden",250 background: p.track,251 }}252 >253 <div254 style={{255 position: "absolute",256 left: fillStart,257 top: 0,258 width: fillEnd - fillStart,259 height: H,260 borderRadius: H / 2,261 opacity: fillOn,262 background: `linear-gradient(180deg, rgba(255,255,255,0.18) 0%, rgba(255,255,255,0) 46%, rgba(0,0,0,0.14) 100%), ${accent}`,263 }}264 />265 <div266 style={{267 position: "absolute",268 inset: 0,269 borderRadius: H / 2,270 boxShadow: `inset 0 ${5 * u}px ${9 * u}px ${p.trackTop}, inset 0 ${1.5 * u}px ${2 * u}px ${p.trackTop}, inset 0 ${-1.2 * u}px 0 ${p.trackLip}`,271 }}272 />273 <div274 style={{275 position: "absolute",276 left: knobLeft,277 top: pad,278 width: knobW,279 height: d,280 borderRadius: d / 2,281 background: `radial-gradient(ellipse 58% 34% at 50% 16%, rgba(255,255,255,0.95), transparent 80%), linear-gradient(180deg, ${p.knob[0]} 0%, ${p.knob[1]} 48%, ${p.knob[2]} 100%)`,282 boxShadow: knobShadow,283 }}284 />285 </div>286 </div>287 );288289 const labelEl = (290 <div291 style={{292 whiteSpace: "nowrap",293 fontFamily,294 fontWeight,295 fontSize,296 lineHeight: 1.08,297 letterSpacing: "-0.035em",298 color: fg,299 textAlign: tall ? "center" : "left",300 }}301 >302 <span style={ink(wLabel)}>{label}</span>303 {state ? (304 <>305 {tall ? <br /> : " "}306 <span style={{ ...ink(wState), color: stateInk }}>{state}</span>307 </>308 ) : null}309 </div>310 );311312 return (313 <AbsoluteFill314 style={{315 overflow: "hidden",316 background: background ?? (dark ? "#03050b" : "#eeece8"),317 }}318 >319 {background === undefined ? (320 <>321 <GshGround322 preset={resolvedGround}323 params={groundParams}324 timeOffset={dark ? 3 : 6}325 timeScale={0.5}326 />327 <AbsoluteFill328 style={{329 background: `radial-gradient(70% 60% at 50% 45%, rgba(${p.scrim},${scrim}) 0%, rgba(${p.scrim},${Math.min(1, scrim + 0.12)}) 100%)`,330 }}331 />332 </>333 ) : null}334 <ScaleLayer scale={scale}>335 <AbsoluteFill336 style={{337 alignItems: "center",338 justifyContent: "center",339 transform: `scale(${camK})`,340 }}341 >342 {tall ? (343 <div344 style={{345 display: "flex",346 flexDirection: "column",347 alignItems: "center",348 gap: 72 * u,349 transform: `translateY(${(1 - open) * 90 * u}px)`,350 }}351 >352 {toggle}353 {labelEl}354 </div>355 ) : (356 <div style={{ display: "flex", alignItems: "center" }}>357 {toggle}358 <div359 style={{ display: "grid", gridTemplateColumns: `${open}fr` }}360 >361 <div style={{ minWidth: 0, paddingLeft: 56 * u }}>362 {labelEl}363 </div>364 </div>365 </div>366 )}367 </AbsoluteFill>368 </ScaleLayer>369 </AbsoluteFill>370 );371}









