Grid Montage
One shot splits into a 2x2 then a 3x3 montage, cutting on the beat.
Source
components/kuchi/grid-montage.tsx
1"use client";23import { loadFont } from "@remotion/google-fonts/InstrumentSans";4import { loadFont as loadSerif } from "@remotion/google-fonts/InstrumentSerif";5import { loadFont as loadManrope } from "@remotion/google-fonts/Manrope";6import type React from "react";7import { useId } from "react";8import {9 AbsoluteFill,10 Easing,11 Img,12 interpolate,13 spring,14 useCurrentFrame,15 useVideoConfig,16} from "remotion";17import type { GradientParams } from "@/lib/kuchi/gsh/gradient-engine";18import { GshGround } from "@/lib/kuchi/gsh/gsh-ground";19import type { GshPresetId } from "@/lib/kuchi/gsh/presets";2021const { fontFamily: sans } = loadFont("normal", {22 weights: ["400"],23 subsets: ["latin"],24});25const display = sans;26const { fontFamily: serif } = loadSerif("italic", {27 weights: ["400"],28 subsets: ["latin"],29});30const manrope = loadManrope("normal", {31 weights: ["500", "600"],32 subsets: ["latin"],33});3435const CLAMP = {36 extrapolateLeft: "clamp",37 extrapolateRight: "clamp",38} as const;3940type Tone = "dark" | "graphite" | "light" | "accent";41type Kind =42 | "title"43 | "pane"44 | "number"45 | "statement"46 | "mark"47 | "clock"48 | "chart"49 | "word";50interface SceneSpec {51 kind: Kind;52 tone: Tone;53}54interface Rect {55 x: number;56 y: number;57 w: number;58 h: number;59}6061function toRgb(color: string): [number, number, number] {62 if (color.startsWith("rgb")) {63 const [r, g, b] = color.match(/[\d.]+/g)?.map(Number) ?? [0, 0, 0];64 return [r, g, b];65 }66 const h = color.replace("#", "");67 const v =68 h.length === 369 ? h70 .split("")71 .map((ch) => ch + ch)72 .join("")73 : h.slice(0, 6);74 const n = Number.parseInt(v, 16);75 return [(n >> 16) & 255, (n >> 8) & 255, n & 255];76}7778function mix(a: string, b: string, t: number) {79 const x = toRgb(a);80 const y = toRgb(b);81 const c = x.map((v, i) => Math.round(v + (y[i] - v) * t));82 return `rgb(${c[0]},${c[1]},${c[2]})`;83}8485function palette(tone: Tone, accent: string, lightStage: boolean) {86 if (tone === "light") {87 return {88 bg: "#F5F4F0",89 fg: "#121214",90 muted: "#6E6C67",91 hair: "rgba(18,18,20,0.10)",92 surface: "#FFFFFF",93 block: "#E6E3DC",94 light: "rgba(255,255,255,0.95)",95 shadow: "rgba(40,36,30,0.16)",96 dot: accent,97 };98 }99 if (tone === "accent") {100 return lightStage101 ? {102 bg: "transparent",103 fg: "#121214",104 muted: "rgba(18,18,20,0.6)",105 hair: "rgba(18,18,20,0.12)",106 surface: "rgba(255,255,255,0.6)",107 block: "rgba(18,18,20,0.08)",108 light: "rgba(255,255,255,0)",109 shadow: "rgba(26,42,158,0.2)",110 dot: accent,111 }112 : {113 bg: "transparent",114 fg: "#F7F7F5",115 muted: "rgba(247,247,245,0.7)",116 hair: "rgba(255,255,255,0.22)",117 surface: "rgba(10,10,11,0.4)",118 block: "rgba(255,255,255,0.16)",119 light: "rgba(255,255,255,0)",120 shadow: "rgba(0,0,0,0.35)",121 dot: accent,122 };123 }124 const graphite = tone === "graphite";125 return {126 bg: graphite ? "#17171A" : "#0A0A0B",127 fg: "#F3F2EF",128 muted: graphite ? "#97969C" : "#8B8A90",129 hair: "rgba(255,255,255,0.10)",130 surface: graphite ? "#1F1F23" : "#151517",131 block: graphite ? "#2B2B30" : "#232326",132 light: graphite ? "rgba(255,255,255,0.07)" : "rgba(255,255,255,0.06)",133 shadow: "rgba(0,0,0,0.6)",134 dot: accent,135 };136}137138type Colors = ReturnType<typeof palette>;139interface Ctx {140 W: number;141 H: number;142 u: number;143 c: Colors;144 accent: string;145 slot?: Rect;146 font: string;147 logo?: React.ReactNode;148}149150const LIGHT_AT: Record<Kind, string> = {151 title: "18% 8%",152 pane: "50% -10%",153 number: "88% 12%",154 statement: "82% 100%",155 mark: "30% 15%",156 clock: "64% 50%",157 chart: "8% 0%",158 word: "22% 0%",159};160161162function KuchiR({ height, color }: { height: number; color: string }) {163 return (164 <svg165 viewBox="0 0 124.06 134.26"166 height={height}167 width={(height * 124.06) / 134.26}168 style={{ display: "block", flexShrink: 0 }}169 aria-hidden170 >171 <circle cx="62.03" cy="67.13" r="56" fill={color} />172 </svg>173 );174}175176function KuchiLogo({ fontSize, color }: { fontSize: number; color: string }) {177 return (178 <div179 style={{180 display: "flex",181 alignItems: "baseline",182 color,183 fontFamily: manrope.fontFamily,184 fontWeight: 600,185 fontSize,186 letterSpacing: "-0.025em",187 lineHeight: 1,188 flexShrink: 0,189 }}190 >191 <span>Acme</span>192 </div>193 );194}195196const abs = (r: Rect): React.CSSProperties => ({197 position: "absolute",198 left: r.x,199 top: r.y,200 width: r.w,201 height: r.h,202});203204const displayType = (size: number, font = display): React.CSSProperties => ({205 fontFamily: font,206 fontWeight: 400,207 fontSize: size,208 letterSpacing: "-0.035em",209 lineHeight: 1,210 fontVariantNumeric: "tabular-nums",211});212213function TitleBody({ x }: { x: Ctx }) {214 const { W, H, u, c, slot } = x;215 const left = slot ? 0.08 * W : 0;216 return (217 <div218 style={{219 position: "absolute",220 left,221 top: 0,222 height: H,223 width: slot ? slot.x - left - 0.03 * W : W,224 display: "flex",225 flexDirection: "column",226 justifyContent: "center",227 alignItems: slot ? "flex-start" : "center",228 gap: 18 * u,229 }}230 >231 <div style={{ fontSize: 22 * u, fontWeight: 400, color: c.muted }}>232 Introducing233 </div>234 {x.logo ?? <KuchiLogo fontSize={132 * u} color={c.fg} />}235 <div236 style={{237 fontSize: 24 * u,238 color: c.muted,239 letterSpacing: "-0.01em",240 textAlign: slot ? "left" : "center",241 }}242 >243 Motion for the films you launch with.244 </div>245 </div>246 );247}248249function PaneBody({ x }: { x: Ctx }) {250 const { W, H, u, c, accent, slot } = x;251 const pane = { x: 0.08 * W, y: 0.1 * H, w: 0.84 * W, h: 0.8 * H };252 const canvas = slot ?? { x: 0.335 * W, y: 0.2 * H, w: 0.36 * W, h: 0.36 * H };253 const side = 0.27 * W;254 const insp = 0.765 * W;255 const bar = 0.075 * H;256 const lightPane = c.surface === "#FFFFFF";257 const tint = mix(accent, c.surface, lightPane ? 0.9 : 0.82);258 const rows = [259 "Hook",260 "Title card",261 "Feature tour",262 "Pricing",263 "Quote",264 "Logo",265 ];266 const fields = [267 ["Duration", "4.0s"],268 ["Easing", "Spring"],269 ["Scale", "1.04"],270 ["Opacity", "100%"],271 ["Blur", "0px"],272 ];273 const clipNames = ["Title", "Shot 02", "Logo"];274 const tracks = [275 [276 [0.3, 0.42],277 [0.43, 0.62],278 ],279 [[0.34, 0.56]],280 [281 [0.29, 0.38],282 [0.39, 0.52],283 [0.53, 0.74],284 ],285 ];286 return (287 <>288 <div289 style={{290 ...abs(pane),291 background: c.surface,292 borderRadius: 16 * u,293 boxShadow: `0 0 0 1px ${c.hair}, 0 ${30 * u}px ${70 * u}px ${c.shadow}`,294 overflow: "hidden",295 }}296 />297 <div298 style={{299 ...abs({ x: pane.x, y: pane.y + bar, w: pane.w, h: 1 }),300 background: c.hair,301 }}302 />303 <div304 style={{305 position: "absolute",306 left: pane.x + 22 * u,307 top: pane.y,308 height: bar,309 display: "flex",310 alignItems: "center",311 gap: 9 * u,312 }}313 >314 {x.logo ?? <KuchiLogo fontSize={18 * u} color={c.fg} />}315 <div style={{ fontSize: 15 * u, color: c.muted, marginLeft: 6 * u }}>316 / Launch film317 </div>318 </div>319 <div320 style={{321 position: "absolute",322 right: W - pane.x - pane.w + 18 * u,323 top: pane.y + bar / 2 - 15 * u,324 height: 30 * u,325 padding: `0 ${14 * u}px`,326 borderRadius: 8 * u,327 background: accent,328 color: "#FFFFFF",329 fontSize: 14 * u,330 fontWeight: 400,331 display: "flex",332 alignItems: "center",333 }}334 >335 Render336 </div>337 <div338 style={{339 ...abs({ x: side, y: pane.y + bar, w: 1, h: pane.h - bar }),340 background: c.hair,341 }}342 />343 <div344 style={{345 ...abs({ x: insp, y: pane.y + bar, w: 1, h: pane.h - bar }),346 background: c.hair,347 }}348 />349 {rows.map((label, i) => {350 const y = pane.y + bar + 26 * u + i * 0.062 * H;351 const on = i === 1;352 return (353 <div key={label}>354 {on ? (355 <div356 style={{357 ...abs({358 x: pane.x + 12 * u,359 y: y - 9 * u,360 w: side - pane.x - 24 * u,361 h: 34 * u,362 }),363 background: tint,364 borderRadius: 8 * u,365 }}366 />367 ) : null}368 <div369 style={{370 ...abs({ x: pane.x + 24 * u, y, w: 14 * u, h: 14 * u }),371 borderRadius: 4 * u,372 boxShadow: `inset 0 0 0 ${1.5 * u}px ${on ? accent : c.muted}`,373 opacity: on ? 1 : 0.6,374 }}375 />376 <div377 style={{378 position: "absolute",379 left: pane.x + 48 * u,380 top: y - 3 * u,381 fontSize: 17 * u,382 lineHeight: 1.2,383 color: on ? c.fg : c.muted,384 whiteSpace: "nowrap",385 }}386 >387 {label}388 </div>389 </div>390 );391 })}392 {fields.map(([k, v], i) => {393 const y = pane.y + bar + 24 * u + i * 0.075 * H;394 return (395 <div key={k}>396 <div397 style={{398 position: "absolute",399 left: insp + 20 * u,400 top: y - 4 * u,401 fontSize: 14 * u,402 color: c.muted,403 }}404 >405 {k}406 </div>407 <div408 style={{409 ...abs({410 x: insp + 20 * u,411 y: y + 16 * u,412 w: 0.155 * W - 40 * u,413 h: 26 * u,414 }),415 borderRadius: 6 * u,416 boxShadow: `inset 0 0 0 1px ${c.hair}`,417 display: "flex",418 alignItems: "center",419 paddingLeft: 9 * u,420 fontSize: 15 * u,421 color: c.fg,422 fontVariantNumeric: "tabular-nums",423 }}424 >425 {v}426 </div>427 </div>428 );429 })}430 <div431 style={{432 ...abs({433 x: side,434 y: canvas.y + canvas.h + 0.07 * H,435 w: insp - side,436 h: 1,437 }),438 background: c.hair,439 }}440 />441 {tracks.map((clips, ti) =>442 clips.map(([a, b], ci) => (443 <div444 key={`${ti}-${a}`}445 style={{446 ...abs({447 x: a * W,448 y: canvas.y + canvas.h + 0.1 * H + ti * 0.058 * H,449 w: (b - a) * W - 4 * u,450 h: 0.042 * H,451 }),452 borderRadius: 6 * u,453 background: ti === 1 ? accent : c.block,454 display: "flex",455 alignItems: "center",456 paddingLeft: 8 * u,457 fontSize: 13 * u,458 color: ti === 1 ? "#FFFFFF" : c.muted,459 whiteSpace: "nowrap",460 overflow: "hidden",461 }}462 >463 {clipNames[(ti + ci) % clipNames.length]}464 </div>465 )),466 )}467 <div468 style={{469 ...abs({470 x: 0.47 * W,471 y: canvas.y + canvas.h + 0.085 * H,472 w: 2 * u,473 h: 0.2 * H,474 }),475 background: c.fg,476 borderRadius: u,477 }}478 />479 {slot ? null : (480 <div481 style={{482 ...abs(canvas),483 borderRadius: 10 * u,484 background: lightPane ? "#0A0A0B" : "#F5F4F0",485 boxShadow: `inset 0 0 0 1px ${c.hair}`,486 display: "flex",487 alignItems: "center",488 justifyContent: "center",489 overflow: "hidden",490 }}491 >492 <div493 style={{494 position: "absolute",495 inset: 0,496 background: `radial-gradient(ellipse 80% 90% at 30% 0%, ${lightPane ? "rgba(255,255,255,0.09)" : "rgba(255,255,255,0.9)"}, transparent 70%)`,497 }}498 />499 {x.logo ?? (500 <KuchiR501 height={58 * u}502 color={lightPane ? "#F3F2EF" : "#121214"}503 />504 )}505 </div>506 )}507 </>508 );509}510511function NumberBody({ x }: { x: Ctx }) {512 const { W, H, u, c, accent, slot } = x;513 const left = 0.08 * W;514 return (515 <>516 <div517 style={{518 position: "absolute",519 left,520 top: 0.16 * H,521 display: "flex",522 alignItems: "center",523 gap: 10 * u,524 fontSize: 19 * u,525 fontWeight: 400,526 color: c.muted,527 }}528 >529 <div530 style={{531 width: 9 * u,532 height: 9 * u,533 borderRadius: 9 * u,534 background: c.dot,535 }}536 />537 Median render time538 </div>539 <div540 style={{541 position: "absolute",542 left: left - 10 * u,543 top: 0.27 * H,544 ...displayType(250 * u, x.font),545 letterSpacing: "-0.05em",546 }}547 >548 4.8×549 </div>550 <div551 style={{552 position: "absolute",553 left,554 top: 0.27 * H + 268 * u,555 width: slot ? slot.x - left - 0.04 * W : 0.4 * W,556 fontSize: 28 * u,557 lineHeight: 1.25,558 color: c.muted,559 letterSpacing: "-0.015em",560 textWrap: "balance",561 }}562 >563 faster from script to final cut564 </div>565 {slot566 ? null567 : [568 { label: "Before", w: 1, color: c.block },569 { label: "With acme", w: 1 / 4.8, color: accent },570 ].map((row, i) => (571 <div572 key={row.label}573 style={{574 position: "absolute",575 left: 0.58 * W,576 top: 0.4 * H + i * 0.13 * H,577 }}578 >579 <div580 style={{581 fontSize: 18 * u,582 color: c.muted,583 marginBottom: 12 * u,584 }}585 >586 {row.label}587 </div>588 <div589 style={{590 width: 0.32 * W * row.w,591 height: 20 * u,592 borderRadius: 10 * u,593 background: row.color,594 boxShadow:595 row.color === accent596 ? undefined597 : `inset 0 0 0 1px ${c.hair}`,598 }}599 />600 </div>601 ))}602 </>603 );604}605606function StatementBody({ x }: { x: Ctx }) {607 const { W, H, u, c } = x;608 return (609 <>610 <div611 style={{612 position: "absolute",613 left: 0.08 * W,614 top: 0.1 * H,615 display: "flex",616 alignItems: "center",617 gap: 10 * u,618 }}619 >620 {x.logo ?? <KuchiLogo fontSize={30 * u} color={c.fg} />}621 </div>622 <div623 style={{624 position: "absolute",625 left: 0.08 * W - 6 * u,626 bottom: 0.13 * H,627 ...displayType(140 * u, x.font),628 lineHeight: 0.94,629 }}630 >631 <div>Own every</div>632 <div>633 <span634 style={{635 fontFamily: serif,636 fontStyle: "italic",637 fontWeight: 400,638 letterSpacing: "-0.02em",639 }}640 >641 frame642 </span>643 <span style={{ color: c.dot }}>.</span>644 </div>645 </div>646 <div647 style={{648 position: "absolute",649 right: 0.08 * W,650 bottom: 0.14 * H,651 fontSize: 20 * u,652 color: c.muted,653 }}654 >655 acme.dev656 </div>657 </>658 );659}660661function MarkBody({ x }: { x: Ctx }) {662 const { u, c } = x;663 return (664 <AbsoluteFill665 style={{666 alignItems: "center",667 justifyContent: "center",668 flexDirection: "column",669 gap: 26 * u,670 }}671 >672 {x.logo ?? <KuchiLogo fontSize={170 * u} color={c.fg} />}673 </AbsoluteFill>674 );675}676677function ClockBody({ x }: { x: Ctx }) {678 const { W, H, u, c, accent } = x;679 const left = 0.08 * W;680 const right = 0.92 * W;681 const base = 0.74 * H;682 const head = 12.07 / 30;683 const marks = [0, 5, 10, 15, 20, 25, 30];684 const clips = [685 { a: 0, b: 4.2, label: "Hook" },686 { a: 4.4, b: 13.6, label: "Feature tour" },687 { a: 13.8, b: 22.4, label: "Pricing" },688 { a: 22.6, b: 30, label: "Logo" },689 ];690 const span = right - left;691 return (692 <>693 <div style={{ position: "absolute", left, top: 0.16 * H }}>694 <div style={{ fontSize: 22 * u, color: c.muted }}>Playhead</div>695 <div style={{ ...displayType(150 * u, x.font), marginTop: 14 * u }}>696 00:12:04697 </div>698 <div699 style={{700 fontSize: 22 * u,701 color: c.muted,702 marginTop: 14 * u,703 fontVariantNumeric: "tabular-nums",704 }}705 >706 Frame 362 of 900707 </div>708 </div>709 {marks.map((m) => (710 <div711 key={m}712 style={{713 position: "absolute",714 left: left + (m / 30) * span,715 top: base - 34 * u,716 fontSize: 17 * u,717 color: c.muted,718 fontVariantNumeric: "tabular-nums",719 transform: m === 0 ? undefined : "translateX(-50%)",720 }}721 >722 {m}s723 </div>724 ))}725 <div726 style={{727 ...abs({ x: left, y: base - 8 * u, w: span, h: 1 }),728 background: c.hair,729 }}730 />731 {clips.map((cl, i) => (732 <div733 key={cl.label}734 style={{735 ...abs({736 x: left + (cl.a / 30) * span,737 y: base + 6 * u,738 w: ((cl.b - cl.a) / 30) * span,739 h: 52 * u,740 }),741 borderRadius: 8 * u,742 background: i === 1 ? mix(accent, c.bg, 0.25) : c.block,743 boxShadow: `inset 0 0 0 1px ${c.hair}`,744 display: "flex",745 alignItems: "center",746 paddingLeft: 14 * u,747 fontSize: 18 * u,748 color: i === 1 ? "#FFFFFF" : c.fg,749 whiteSpace: "nowrap",750 overflow: "hidden",751 }}752 >753 {cl.label}754 </div>755 ))}756 <div757 style={{758 ...abs({759 x: left + head * span - 1 * u,760 y: base - 14 * u,761 w: 2 * u,762 h: 82 * u,763 }),764 background: c.fg,765 borderRadius: u,766 }}767 />768 </>769 );770}771772function ChartBody({ x }: { x: Ctx }) {773 const { W, H, u, c, accent } = x;774 const heights = [0.14, 0.19, 0.17, 0.26, 0.31, 0.38, 0.52];775 const base = 0.84 * H;776 const bw = 0.046 * W;777 const gap = 0.018 * W;778 const x0 = 0.92 * W - heights.length * (bw + gap) + gap;779 return (780 <>781 <div style={{ position: "absolute", left: 0.08 * W, top: 0.14 * H }}>782 <div style={{ fontSize: 20 * u, color: c.muted, fontWeight: 400 }}>783 Videos shipped per week784 </div>785 <div style={{ ...displayType(110 * u, x.font), marginTop: 22 * u }}>786 +212%787 </div>788 </div>789 <div790 style={{791 ...abs({ x: 0.08 * W, y: base, w: 0.84 * W, h: 1 }),792 background: c.hair,793 }}794 />795 {heights.map((h, i) => (796 <div797 key={h}798 style={{799 ...abs({800 x: x0 + i * (bw + gap),801 y: base - h * H,802 w: bw,803 h: h * H,804 }),805 borderRadius: `${8 * u}px ${8 * u}px ${2 * u}px ${2 * u}px`,806 background: i === heights.length - 1 ? accent : c.block,807 }}808 />809 ))}810 </>811 );812}813814function WordBody({ x }: { x: Ctx }) {815 const { W, H, u, c } = x;816 return (817 <>818 <div819 style={{820 ...abs({ x: 0.08 * W, y: 0.2 * H, w: 0.84 * W, h: 1 }),821 background: c.hair,822 }}823 />824 <div825 style={{826 position: "absolute",827 left: 0.08 * W,828 right: 0.08 * W,829 top: 0.2 * H - 40 * u,830 display: "flex",831 justifyContent: "space-between",832 fontSize: 19 * u,833 color: c.muted,834 fontVariantNumeric: "tabular-nums",835 }}836 >837 <span>02</span>838 <span>Chapter</span>839 </div>840 <div841 style={{842 position: "absolute",843 left: 0.08 * W - 12 * u,844 bottom: 0.1 * H,845 ...displayType(250 * u, x.font),846 letterSpacing: "-0.055em",847 }}848 >849 Render<span style={{ color: c.dot }}>.</span>850 </div>851 </>852 );853}854855const BODIES: Record<Kind, (p: { x: Ctx }) => React.ReactElement> = {856 title: TitleBody,857 pane: PaneBody,858 number: NumberBody,859 statement: StatementBody,860 mark: MarkBody,861 clock: ClockBody,862 chart: ChartBody,863 word: WordBody,864};865866function PlaceholderScene({867 kind,868 tone,869 accent,870 slot,871 lightStage,872 font,873 logo,874}: SceneSpec & {875 accent: string;876 slot?: Rect;877 lightStage: boolean;878 font: string;879 logo?: React.ReactNode;880}) {881 const { width: W, height: H } = useVideoConfig();882 const u = Math.min(H / 720, W / 1280);883 const c = palette(tone, accent, lightStage);884 const Body = BODIES[kind];885 const open = tone === "accent";886 return (887 <AbsoluteFill888 style={{889 background: c.bg,890 color: c.fg,891 fontFamily: font,892 overflow: "hidden",893 }}894 >895 {open ? null : (896 <AbsoluteFill897 style={{898 background: `radial-gradient(ellipse 75% 85% at ${LIGHT_AT[kind]}, ${c.light}, transparent 72%)`,899 }}900 />901 )}902 <Body x={{ W, H, u, c, accent, slot, font, logo }} />903 </AbsoluteFill>904 );905}906907function ImageScene({ src }: { src: string }) {908 return (909 <AbsoluteFill style={{ background: "#0A0A0B" }}>910 <Img911 src={src}912 style={{ width: "100%", height: "100%", objectFit: "cover" }}913 />914 </AbsoluteFill>915 );916}917918function Grain({ opacity }: { opacity: number }) {919 const id = `g${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;920 return (921 <svg922 width="100%"923 height="100%"924 style={{925 position: "absolute",926 inset: 0,927 opacity,928 pointerEvents: "none",929 }}930 aria-hidden931 >932 <filter id={id}>933 <feTurbulence934 type="fractalNoise"935 baseFrequency="0.9"936 numOctaves={2}937 seed={7}938 stitchTiles="stitch"939 />940 <feColorMatrix values="0 0 0 0 0.5 0 0 0 0 0.5 0 0 0 0 0.5 0 0 0 0.9 0" />941 </filter>942 <rect width="100%" height="100%" filter={`url(#${id})`} />943 </svg>944 );945}946947export interface GridMontageProps {948 scenes?: React.ReactNode[];949 images?: string[];950 beat?: number;951 /** Corner radius of the tiles, in px at 720 tall. */952 radius?: number;953 accent?: string;954 /** Solid fill in place of the gradient ground; "transparent" drops it. */955 background?: string;956 /** Typeface for the built-in placeholder scenes. Load it yourself; Instrument Sans by default. */957 fontFamily?: string;958 /** Replaces the acme mark in the built-in placeholder scenes. */959 logo?: React.ReactNode;960 ground?: GshPresetId;961 groundParams?: Partial<GradientParams>;962 /** Size of the montage, around the frame's center. */963 scale?: number;964 /** Frames to hold the first frame before anything moves. */965 delay?: number;966 grain?: boolean;967 speed?: number;968 theme?: "dark" | "light";969}970971const DARK_LIST: SceneSpec[] = [972 { kind: "mark", tone: "accent" },973 { kind: "pane", tone: "light" },974 { kind: "number", tone: "graphite" },975 { kind: "clock", tone: "graphite" },976 { kind: "chart", tone: "light" },977 { kind: "mark", tone: "accent" },978 { kind: "word", tone: "light" },979 { kind: "title", tone: "graphite" },980 { kind: "pane", tone: "graphite" },981 { kind: "number", tone: "light" },982 { kind: "statement", tone: "light" },983];984const flipTone = (t: Tone): Tone =>985 t === "light" ? "graphite" : t === "graphite" ? "light" : t;986const LIGHT_LIST: SceneSpec[] = DARK_LIST.map((s) => ({987 ...s,988 tone: flipTone(s.tone),989}));990991const EASE_SNAP = Easing.bezier(0.22, 1, 0.36, 1);992const CARD = { mass: 0.8, damping: 18, stiffness: 160 };993const MORPH_SPRING = { damping: 200 };994const EASE_SWAP = Easing.bezier(0.83, 0, 0.17, 1);995const SWAP = 8;996const MERGE = 12;997998const CORNERS = [999 [0, 0],1000 [0, 2],1001 [2, 0],1002 [2, 2],1003];1004const FOCI = [1005 [-10, -6],1006 [8, -10],1007 [-6, 8],1008 [10, 6],1009 [0, -12],1010 [-12, 0],1011 [0, 0],1012 [12, 0],1013 [0, 12],1014];10151016function grid(1017 cols: number,1018 rows: number,1019 W: number,1020 H: number,1021 m: number,1022 g: number,1023) {1024 const cw = (W - 2 * m - (cols - 1) * g) / cols;1025 const ch = (H - 2 * m - (rows - 1) * g) / rows;1026 const out: Rect[] = [];1027 for (let r = 0; r < rows; r++) {1028 for (let c = 0; c < cols; c++) {1029 out.push({ x: m + c * (cw + g), y: m + r * (ch + g), w: cw, h: ch });1030 }1031 }1032 return out;1033}10341035const lerpRect = (a: Rect, b: Rect, t: number): Rect => ({1036 x: a.x + (b.x - a.x) * t,1037 y: a.y + (b.y - a.y) * t,1038 w: a.w + (b.w - a.w) * t,1039 h: a.h + (b.h - a.h) * t,1040});10411042type Fill =1043 | { type: "own"; shot: number; push: number; focus: number[]; nominal: Rect }1044 | { type: "piece"; shot: number; push: number; source: Rect };10451046/** The foreground at `scale`, around the frame's center; nothing extra at 1. */1047function ScaleLayer({1048 scale,1049 children,1050}: {1051 scale: number;1052 children: React.ReactNode;1053}) {1054 if (scale === 1) return children;1055 return (1056 <AbsoluteFill style={{ transform: `scale(${scale})` }}>1057 {children}1058 </AbsoluteFill>1059 );1060}10611062export function GridMontage({1063 scenes,1064 images,1065 beat = 14,1066 radius = 14,1067 accent = "#3B5BFD",1068 background,1069 fontFamily = sans,1070 logo,1071 ground,1072 groundParams,1073 scale = 1,1074 delay = 0,1075 grain = true,1076 speed = 1,1077 theme = "dark",1078}: GridMontageProps) {1079 const frame = Math.max(0, useCurrentFrame() - delay) * speed;1080 const { width: W, height: H, fps } = useVideoConfig();1081 const u = H / 720;1082 const b = Math.max(8, beat);1083 const lightStage = theme === "light";1084 const specs = theme === "light" ? LIGHT_LIST : DARK_LIST;1085 const stage = theme === "light" ? "#F5F4F0" : "#0A0A0B";1086 const hairline =1087 theme === "light" ? "rgba(18,18,20,0.10)" : "rgba(255,255,255,0.10)";1088 const panelShadow = (e: number) =>1089 lightStage1090 ? `0 ${2 * u}px ${4 * u}px rgba(26,42,158,${0.06 * e}), 0 ${18 * u}px ${40 * u}px -${14 * u}px rgba(26,42,158,${0.22 * e})`1091 : `0 ${2 * u}px ${6 * u}px rgba(0,0,0,${0.3 * e}), 0 ${20 * u}px ${44 * u}px -${14 * u}px rgba(0,0,0,${0.6 * e})`;10921093 const M = 0.042 * H;1094 const G = 0.022 * H;1095 const R = radius * u;10961097 const tOpen = 2 * b;1098 const tSwap1 = 3 * b;1099 const tGrow = 5 * b;1100 const tSwap2 = 7 * b;1101 const tMerge = 9 * b;1102 const p = (start: number, dur: number) =>1103 interpolate(frame, [start, start + dur], [0, 1], CLAMP);11041105 const beats = (since: number) => {1106 let s = 0;1107 for (let k = 0; k < 12; k++) {1108 s += EASE_SNAP(interpolate(since - k * b, [0, 7], [0, 1], CLAMP));1109 }1110 return s;1111 };1112 const ownPush = (start: number) => 1.02 + 0.012 * beats(frame - start);1113 const openerPush = 1 + 0.03 * (frame / Math.max(1, tGrow));1114 const closerPush =1115 1 +1116 0.035 *1117 Easing.bezier(0.22, 1, 0.36, 1)(p(tSwap2, tMerge + MERGE + 40 - tSwap2));11181119 const list = scenes ?? images;1120 const pickShot = (slot: number) => {1121 if (!list?.length) return slot;1122 const len = list.length;1123 if (slot === 0) return 0;1124 if (slot === 10) return len - 1;1125 if (len <= 2) return slot % len;1126 return 1 + ((slot - 1) % (len - 2));1127 };1128 const shot = (slot: number) => {1129 const i = pickShot(slot);1130 if (scenes) return <AbsoluteFill>{scenes[i]}</AbsoluteFill>;1131 if (images) return <ImageScene src={images[i]} />;1132 return (1133 <PlaceholderScene1134 {...specs[i]}1135 accent={accent}1136 lightStage={lightStage}1137 font={fontFamily}1138 logo={logo}1139 />1140 );1141 };11421143 const content = (fill: Fill, tile: Rect) => {1144 if (fill.type === "piece") {1145 const q = fill.source;1146 const sc = Math.max(tile.w / q.w, tile.h / q.h);1147 return (1148 <div1149 style={{1150 position: "absolute",1151 width: W,1152 height: H,1153 transformOrigin: "0 0",1154 transform: `translate(${tile.w / 2}px, ${tile.h / 2}px) scale(${sc}) translate(${-(q.x + q.w / 2)}px, ${-(q.y + q.h / 2)}px)`,1155 }}1156 >1157 <AbsoluteFill style={{ transform: `scale(${fill.push})` }}>1158 {shot(fill.shot)}1159 </AbsoluteFill>1160 </div>1161 );1162 }1163 const sc = Math.max(fill.nominal.w / W, fill.nominal.h / H);1164 return (1165 <div1166 style={{1167 position: "absolute",1168 width: W,1169 height: H,1170 transformOrigin: "0 0",1171 transform: `translate(${tile.w / 2}px, ${tile.h / 2}px) scale(${sc}) translate(${-W / 2}px, ${-H / 2}px)`,1172 }}1173 >1174 <AbsoluteFill1175 style={{1176 transform: `scale(${fill.push})`,1177 transformOrigin: `${50 + fill.focus[0]}% ${50 + fill.focus[1]}%`,1178 }}1179 >1180 {shot(fill.shot)}1181 </AbsoluteFill>1182 </div>1183 );1184 };11851186 const panel = (1187 key: string,1188 rect: Rect,1189 radius: number,1190 edge: number,1191 from: Fill,1192 to: Fill,1193 swap: number,1194 ) => (1195 <div1196 key={key}1197 style={{1198 position: "absolute",1199 left: rect.x,1200 top: rect.y,1201 width: rect.w,1202 height: rect.h,1203 borderRadius: Math.min(radius, rect.w / 2, rect.h / 2),1204 overflow: "hidden",1205 boxShadow: edge > 0.01 ? panelShadow(edge) : undefined,1206 }}1207 >1208 {swap < 1 ? (1209 <AbsoluteFill1210 style={{ transform: `translateY(${-0.4 * rect.h * swap}px)` }}1211 >1212 {content(from, rect)}1213 <AbsoluteFill1214 style={{ background: "#0A0A0B", opacity: 0.22 * swap }}1215 />1216 </AbsoluteFill>1217 ) : null}1218 {swap > 0 ? (1219 <AbsoluteFill1220 style={{1221 transform: `translateY(${rect.h * (1 - swap)}px)`,1222 boxShadow:1223 swap < 11224 ? `0 ${-10 * u}px ${30 * u}px rgba(0,0,0,${0.3 * (1 - swap)})`1225 : undefined,1226 }}1227 >1228 {content(to, rect)}1229 </AbsoluteFill>1230 ) : null}1231 <AbsoluteFill1232 style={{1233 borderRadius: Math.min(radius, rect.w / 2, rect.h / 2),1234 boxShadow: `inset 0 0 0 1px ${hairline}`,1235 opacity: edge,1236 }}1237 />1238 </div>1239 );12401241 let body: React.ReactNode;1242 if (frame < tOpen) {1243 body = (1244 <AbsoluteFill style={{ transform: `scale(${openerPush})` }}>1245 {shot(0)}1246 </AbsoluteFill>1247 );1248 } else if (frame < tGrow) {1249 const q = spring({ frame: frame - tOpen, fps, config: CARD });1250 const rects = grid(2, 2, W, H, M * q, G * q);1251 const quads = grid(2, 2, W, H, 0, 0);1252 body = rects.map((rect, i) =>1253 panel(1254 `a${i}`,1255 rect,1256 R * q,1257 q,1258 { type: "piece", shot: 0, push: openerPush, source: quads[i] },1259 {1260 type: "own",1261 shot: 1 + i,1262 push: ownPush(tSwap1),1263 focus: FOCI[i],1264 nominal: rect,1265 },1266 EASE_SWAP(p(tSwap1 + i * 2, SWAP)),1267 ),1268 );1269 } else {1270 const qg = spring({ frame: frame - tGrow, fps, config: CARD });1271 const qm =1272 frame >= tMerge + MERGE1273 ? 11274 : Math.min(1275 1,1276 spring({1277 frame: frame - tMerge,1278 fps,1279 config: MORPH_SPRING,1280 durationInFrames: MERGE,1281 }),1282 );1283 const m = M * (1 - qm);1284 const g = G * (1 - qm);1285 const cells = grid(3, 3, W, H, m, g);1286 const full3 = grid(3, 3, W, H, M, G);1287 const thirds = grid(3, 3, W, H, 0, 0);1288 const two = grid(2, 2, W, H, M, G);1289 const span = (k: number, axis: "x" | "y") => {1290 const a = axis === "x" ? two[k] : two[k * 2];1291 return axis === "x" ? [a.x, a.x + a.w] : [a.y, a.y + a.h];1292 };1293 const seed = (r: number, c: number): Rect => {1294 const xs = c === 1 ? [W / 2, W / 2] : span(c === 0 ? 0 : 1, "x");1295 const ys = r === 1 ? [H / 2, H / 2] : span(r === 0 ? 0 : 1, "y");1296 return { x: xs[0], y: ys[0], w: xs[1] - xs[0], h: ys[1] - ys[0] };1297 };1298 let fresh = 0;1299 const panels: React.ReactNode[] = [];1300 for (let r = 0; r < 3; r++) {1301 for (let c = 0; c < 3; c++) {1302 const idx = r * 3 + c;1303 const corner = CORNERS.findIndex(([cr, cc]) => cr === r && cc === c);1304 const isCorner = corner >= 0;1305 const rect = isCorner1306 ? lerpRect(two[corner], cells[idx], qg)1307 : lerpRect(seed(r, c), cells[idx], qg);1308 const ownShot = isCorner ? 1 + corner : 5 + fresh++;1309 const own: Fill = {1310 type: "own",1311 shot: ownShot,1312 push: ownPush(isCorner ? tSwap1 : tGrow),1313 focus: FOCI[isCorner ? corner : 4 + fresh - 1],1314 nominal: isCorner ? rect : full3[idx],1315 };1316 if (rect.w < 0.5 || rect.h < 0.5) continue;1317 panels.push(1318 panel(1319 `b${idx}`,1320 rect,1321 R * (1 - qm),1322 1 - qm,1323 own,1324 {1325 type: "piece",1326 shot: 10,1327 push: closerPush,1328 source: thirds[idx],1329 },1330 EASE_SWAP(p(tSwap2 + (r + c) * 2, SWAP)),1331 ),1332 );1333 }1334 }1335 body =1336 qm >= 1 ? (1337 <AbsoluteFill style={{ transform: `scale(${closerPush})` }}>1338 {shot(10)}1339 </AbsoluteFill>1340 ) : (1341 <>1342 {qm > 0.9 ? (1343 <AbsoluteFill style={{ transform: `scale(${closerPush})` }}>1344 {shot(10)}1345 </AbsoluteFill>1346 ) : null}1347 {panels}1348 </>1349 );1350 }13511352 return (1353 <AbsoluteFill1354 style={{1355 background: background === undefined ? stage : undefined,1356 overflow: "hidden",1357 }}1358 >1359 {background === undefined ? (1360 <GshGround1361 preset={ground ?? (lightStage ? "glasswork" : "velvet-hour")}1362 params={groundParams}1363 timeOffset={4}1364 timeScale={0.5}1365 />1366 ) : (1367 <AbsoluteFill1368 style={{1369 background: background === "transparent" ? undefined : background,1370 }}1371 />1372 )}1373 <ScaleLayer scale={scale}>{body}</ScaleLayer>1374 {grain ? <Grain opacity={theme === "light" ? 0.04 : 0.055} /> : null}1375 </AbsoluteFill>1376 );1377}









