Card Fan
Colourful format cards fan out of a stack with a "Your top formats" caption.
Source
components/kuchi/card-fan.tsx
1"use client";23import { loadFont as loadMono } from "@remotion/google-fonts/GeistMono";4import { loadFont as loadSerif } from "@remotion/google-fonts/InstrumentSerif";5import { loadFont } from "@remotion/google-fonts/Onest";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 onest = loadFont("normal", {20 weights: ["300", "400"],21 subsets: ["latin"],22});23const serif = loadSerif("italic", { weights: ["400"], subsets: ["latin"] });24const mono = loadMono("normal", { weights: ["400"], subsets: ["latin"] });2526type Glyph = "launch" | "list" | "play" | "cursor" | "spark";2728export interface FanCard {29 label: string;30 color: string;31 meta?: string;32 glyph?: Glyph;33}3435export interface CardFanProps {36 title?: string;37 accentWord?: number;38 cards?: FanCard[];39 pick?: number;40 caption?: string;41 theme?: "dark" | "light";42 ink?: string;43 ground?: GshPresetId;44 groundParams?: Partial<GradientParams>;45 /** Solid fill in place of the gradient ground; "transparent" drops it. */46 background?: string;47 /** Typeface for the title, caption and card labels. Load it yourself; Onest by default. */48 fontFamily?: string;49 /** Frames between title words as the title rises in. */50 stagger?: number;51 /** Blur, in px, on the cards that step back when one is picked. */52 blur?: number;53 /** Size of the fan and copy, around the frame's center. */54 scale?: number;55 /** Frames to hold the first frame before anything moves. */56 delay?: number;57 speed?: number;58}5960const DEFAULT_CARDS: FanCard[] = [61 {62 label: "Launch",63 color: "#e0583f",64 meta: "24 scenes · 1:12",65 glyph: "launch",66 },67 {68 label: "Changelog",69 color: "#3a9e7c",70 meta: "18 scenes · 0:48",71 glyph: "list",72 },73 {74 label: "Teaser",75 color: "#4a58c4",76 meta: "12 scenes · 0:21",77 glyph: "play",78 },79 {80 label: "Demo",81 color: "#34363d",82 meta: "31 scenes · 2:05",83 glyph: "cursor",84 },85];8687const THEMES = {88 dark: {89 ink: "#eef6f4",90 sub: "rgba(238,246,244,0.6)",91 card: "#141618",92 cardInk: "#f1f4f3",93 cardSub: "rgba(241,244,243,0.52)",94 ring: "inset 0 0 0 1px rgba(255,255,255,0.07), inset 0 1px 0 rgba(255,255,255,0.08)",95 ground: "riptide" as GshPresetId | undefined,96 groundParams: undefined as Partial<GradientParams> | undefined,97 shadowRgb: "0,8,8",98 shadowK: 1,99 },100 light: {101 ink: "#16171c",102 sub: "rgba(22,23,28,0.56)",103 card: "#fcfbf9",104 cardInk: "#16171c",105 cardSub: "rgba(22,23,28,0.5)",106 ring: "inset 0 0 0 1px rgba(0,0,0,0.06)",107 ground: "paper-crease" as GshPresetId | undefined,108 groundParams: undefined as Partial<GradientParams> | undefined,109 shadowRgb: "26,30,60",110 shadowK: 0.5,111 },112};113114const BEATS = [115 { id: "title", at: 0 },116 { id: "drop", at: 4 },117 { id: "fan", at: 18 },118 { id: "pick", at: 60 },119 { id: "caption", at: 72 },120 { id: "end", at: 132 },121] as const;122const beat = (id: (typeof BEATS)[number]["id"]) =>123 BEATS.find((b) => b.id === id)?.at ?? 0;124125const EASE_ENTER = Easing.bezier(0.32, 0.72, 0, 1);126const EASE_CAMERA = Easing.bezier(0.87, 0, 0.13, 1);127const SPRING_CARD = { mass: 0.8, damping: 18, stiffness: 160 };128const SPRING_HEAVY = { mass: 1, damping: 26, stiffness: 120 };129const SPRING_MORPH = { damping: 200 };130const CLAMP = {131 extrapolateLeft: "clamp",132 extrapolateRight: "clamp",133} as const;134135function shade(color: string, pct: number, to: string) {136 return `color-mix(in oklab, ${color} ${100 - pct}%, ${to})`;137}138139function GlyphIcon({ glyph, size }: { glyph: Glyph; size: number }) {140 const paths: Record<Glyph, string> = {141 launch: "M7 17 17 7M9 7h8v8",142 list: "M9 6h11M9 12h11M9 18h11M4.5 6h.01M4.5 12h.01M4.5 18h.01",143 play: "M8 5.5v13l10.5-6.5z",144 cursor: "M5.5 3.5 19 10l-6 2-2 6z",145 spark: "M12 3v5M12 16v5M3 12h5M16 12h5",146 };147 return (148 <svg149 width={size}150 height={size}151 viewBox="0 0 24 24"152 fill="none"153 stroke="currentColor"154 strokeWidth={1.5}155 strokeLinecap="round"156 strokeLinejoin="round"157 aria-hidden158 >159 <path d={paths[glyph]} vectorEffect="non-scaling-stroke" />160 </svg>161 );162}163164/** The foreground at `scale`, around the frame's center; nothing extra at 1. */165function ScaleLayer({166 scale,167 children,168}: {169 scale: number;170 children: React.ReactNode;171}) {172 if (scale === 1) return children;173 return (174 <AbsoluteFill style={{ transform: `scale(${scale})` }}>175 {children}176 </AbsoluteFill>177 );178}179180export function CardFan({181 title = "Your top formats",182 accentWord = 2,183 cards = DEFAULT_CARDS,184 pick = 0,185 caption = "Launch films are 41% of this month’s renders",186 theme = "dark",187 ink,188 ground,189 groundParams,190 background,191 fontFamily = onest.fontFamily,192 stagger = 3,193 blur: blurPx = 3,194 scale: scaleProp = 1,195 delay = 0,196 speed = 1,197}: CardFanProps) {198 const frame = Math.max(0, useCurrentFrame() - delay) * speed;199 const { width: W, height: H, fps } = useVideoConfig();200 const t = THEMES[theme];201 const light = theme === "light";202 const fg = ink ?? t.ink;203 const preset = ground ?? (groundParams ? undefined : t.ground);204 const gParams = groundParams ?? t.groundParams;205 const portrait = H > W;206 const N = Math.max(2, Math.min(6, cards.length));207 const list = cards.slice(0, N);208 const pickIdx = Math.min(Math.max(0, pick), N - 1);209210 const cx = W / 2;211 const cardW = portrait ? W * 0.44 : W * 0.2;212 const pad = cardW * 0.045;213 const coverS = cardW - pad * 2;214 const cardH = pad + coverS + cardW * 0.31;215 const radius = cardW * 0.075;216 const cy = portrait ? H * 0.53 : H * 0.575;217 const spread = portrait ? cardW * 0.3 : cardW * 0.6;218 const titleSize = portrait ? W * 0.092 : W * 0.052;219 const titleY = portrait ? H * 0.15 : H * 0.14;220 const capSize = portrait ? W * 0.04 : H * 0.034;221 const capY = portrait ? H * 0.865 : H * 0.92;222 const pull = portrait ? 0.8 : 0.45;223224 const drop = spring({225 frame: frame - beat("drop"),226 fps,227 config: SPRING_HEAVY,228 });229 const lift = spring({230 frame: frame - beat("pick"),231 fps,232 config: SPRING_CARD,233 });234 const liftSettle = spring({235 frame: frame - beat("pick"),236 fps,237 config: SPRING_MORPH,238 });239 const push = interpolate(frame, [0, beat("end")], [1, 1.035], {240 ...CLAMP,241 easing: EASE_CAMERA,242 });243 const sway = Math.sin(frame / 24) * 2;244245 const titleWords = title.split(" ");246 const capWords = caption.split(" ");247248 const cardState = (j: number) => {249 const m = N - 1 - j;250 const open = spring({251 frame: frame - (beat("fan") + m * 3),252 fps,253 config: SPRING_CARD,254 });255 const isFront = j === N - 1;256 const openX = cx + (j - (N - 1) / 2) * spread;257 const openY = cy + (portrait ? (j - (N - 1) / 2) * cardH * 0.1 : 0);258 const stackX = cx + m * 4;259 const stackY = cy + m * 3;260 let x = stackX + (openX - stackX) * open;261 let y = stackY + (openY - stackY) * open;262 let rz = (-3 - m * 1.5) * (1 - open) + (-9 + j * 2.2) * open;263 let ry = 3 * (1 - open) + (14 + sway) * open;264 let rx = 5 * open;265 let z = 0;266 let scale = 1;267 let blur = 0;268 let dim = 1;269 const fall = 1 - drop;270 y += fall * H * 0.7;271 rz += fall * 10;272 if (j === pickIdx) {273 const tx = x + (cx - x) * pull;274 x += (tx - x) * lift;275 y += -cardH * (portrait ? 0.1 : 0.12) * lift;276 rz += (-2 - rz) * liftSettle;277 ry += (4 - ry) * liftSettle;278 rx *= 1 - liftSettle;279 z = 140 * lift;280 scale = 1 + 0.1 * lift;281 } else {282 const side = j < pickIdx ? -1 : 1;283 x += side * cardW * (portrait ? 0.08 : 0.16) * lift;284 y += cardH * 0.06 * lift;285 z = -60 * lift;286 blur = blurPx * Math.min(1, lift);287 dim = 1 - 0.3 * Math.min(1, lift);288 }289 const hidden = !isFront && frame < beat("drop") + 6;290 return { x, y, rz, ry, rx, z, scale, blur, dim, hidden };291 };292293 const order = list294 .map((_, j) => j)295 .sort((a, b) =>296 a === pickIdx && lift > 0.3297 ? 1298 : b === pickIdx && lift > 0.3299 ? -1300 : a - b,301 );302303 const k = t.shadowK;304 const shadow = `0 1px 2px rgba(${t.shadowRgb},${0.24 * k}), 0 ${cardH * 0.05}px ${cardH * 0.12}px rgba(${t.shadowRgb},${0.26 * k}), 0 ${cardH * 0.16}px ${cardH * 0.4}px rgba(${t.shadowRgb},${0.32 * k})`;305306 return (307 <AbsoluteFill style={{ overflow: "hidden" }}>308 {background === undefined ? (309 <GshGround310 preset={preset}311 params={gParams}312 timeOffset={light ? 2.2 : 3}313 timeScale={0.6}314 />315 ) : (316 <AbsoluteFill317 style={{318 background: background === "transparent" ? undefined : background,319 }}320 />321 )}322 <ScaleLayer scale={scaleProp}>323 <AbsoluteFill style={{ transform: `scale(${push})` }}>324 <AbsoluteFill325 style={{326 perspective: portrait ? 2200 : 1400,327 perspectiveOrigin: `50% ${(cy / H) * 100}%`,328 }}329 >330 {order.map((j) => {331 const c = list[j] as FanCard;332 const s = cardState(j);333 if (s.hidden) return null;334 return (335 <div336 key={`card-${c.label}-${j}`}337 style={{338 position: "absolute",339 left: s.x - cardW / 2,340 top: s.y - cardH / 2,341 width: cardW,342 height: cardH,343 borderRadius: radius,344 transform: `translateZ(${s.z}px) rotateX(${s.rx}deg) rotateY(${s.ry}deg) rotateZ(${s.rz}deg) scale(${s.scale})`,345 filter:346 s.blur > 0.01347 ? `blur(${s.blur}px) brightness(${s.dim})`348 : undefined,349 boxShadow: shadow,350 backgroundColor: t.card,351 fontFamily,352 }}353 >354 <div355 style={{356 position: "absolute",357 left: pad,358 top: pad,359 width: coverS,360 height: coverS,361 borderRadius: radius - pad,362 backgroundImage: `linear-gradient(160deg, ${shade(c.color, 18, "#fff")} 0%, ${c.color} 46%, ${shade(c.color, 38, "#000")} 100%)`,363 boxShadow: "inset 0 0 0 1px rgba(255,255,255,0.12)",364 color: "rgba(255,255,255,0.92)",365 overflow: "hidden",366 }}367 >368 <div369 style={{370 position: "absolute",371 left: coverS * 0.09,372 bottom: coverS * 0.09,373 }}374 >375 <GlyphIcon376 glyph={c.glyph ?? "spark"}377 size={coverS * 0.24}378 />379 </div>380 </div>381 <div382 style={{383 position: "absolute",384 left: pad + cardW * 0.02,385 right: pad,386 top: pad + coverS + cardW * 0.06,387 color: t.cardInk,388 }}389 >390 <div391 style={{392 fontSize: cardW * 0.1,393 fontWeight: 400,394 letterSpacing: "-0.025em",395 lineHeight: 1.1,396 whiteSpace: "nowrap",397 }}398 >399 {c.label}400 </div>401 {c.meta ? (402 <div403 style={{404 marginTop: cardW * 0.025,405 fontFamily: mono.fontFamily,406 fontWeight: 400,407 fontSize: Math.max(cardW * 0.066, H * 0.022),408 letterSpacing: "-0.01em",409 fontVariantNumeric: "tabular-nums",410 whiteSpace: "nowrap",411 color: t.cardSub,412 }}413 >414 {c.meta}415 </div>416 ) : null}417 </div>418 </div>419 );420 })}421 </AbsoluteFill>422 <div423 style={{424 position: "absolute",425 left: 0,426 width: W,427 top: titleY - titleSize * 0.55,428 display: "flex",429 justifyContent: "center",430 alignItems: "baseline",431 columnGap: "0.24em",432 fontFamily,433 fontWeight: 300,434 fontSize: titleSize,435 lineHeight: 1.1,436 letterSpacing: "-0.04em",437 color: fg,438 }}439 >440 {titleWords.map((word, i) => {441 const s = beat("title") + i * stagger;442 const p = interpolate(frame, [s, s + 14], [0, 1], {443 ...CLAMP,444 easing: EASE_ENTER,445 });446 const accent = i === accentWord;447 return (448 <span449 key={`${word}-${i}`}450 style={{451 display: "inline-block",452 overflow: "hidden",453 padding: "0.04em 0.08em 0.18em",454 margin: "-0.04em -0.08em -0.18em",455 }}456 >457 <span458 style={{459 display: "inline-block",460 transform: `translateY(${(1 - p) * 120}%)`,461 fontFamily: accent ? serif.fontFamily : undefined,462 fontStyle: accent ? "italic" : undefined,463 fontWeight: 400,464 fontSize: accent ? "1.12em" : undefined,465 letterSpacing: accent ? "-0.02em" : undefined,466 lineHeight: 1,467 }}468 >469 {word}470 </span>471 </span>472 );473 })}474 </div>475 <div476 style={{477 position: "absolute",478 left: W * 0.08,479 width: W * 0.84,480 top: capY - capSize * 0.6,481 display: "flex",482 flexWrap: "wrap",483 justifyContent: "center",484 columnGap: "0.26em",485 fontFamily,486 fontWeight: 300,487 fontSize: capSize,488 letterSpacing: "-0.01em",489 fontVariantNumeric: "tabular-nums",490 color: t.sub,491 }}492 >493 {capWords.map((word, i) => {494 const s = beat("caption") + i * 2;495 const p = interpolate(frame, [s, s + 12], [0, 1], {496 ...CLAMP,497 easing: EASE_ENTER,498 });499 return (500 <span501 key={`${word}-${i}`}502 style={{503 display: "inline-block",504 overflow: "hidden",505 padding: "0.04em 0.04em 0.18em",506 margin: "-0.04em -0.04em -0.18em",507 }}508 >509 <span510 style={{511 display: "inline-block",512 transform: `translateY(${(1 - p) * 120}%)`,513 color: /\d/.test(word) ? fg : undefined,514 }}515 >516 {word}517 </span>518 </span>519 );520 })}521 </div>522 </AbsoluteFill>523 </ScaleLayer>524 </AbsoluteFill>525 );526}527528export default CardFan;







