Waitlist Swarm
Cursors and avatar dots swarm to form an R while the waitlist count rolls up.
Source
components/kuchi/waitlist-swarm.tsx
1"use client";23import { loadFont } from "@remotion/google-fonts/BricolageGrotesque";4import { getLength, getPointAtLength, getSubpaths } from "@remotion/paths";5import type React from "react";6import { useMemo } 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: sans } = loadFont("normal", {20 weights: ["300", "400"],21 subsets: ["latin"],22});2324export interface SwarmShapePart {25 d: string;26 accent?: boolean;27}2829export interface WaitlistSwarmProps {30 shape?: string | SwarmShapePart[];31 value?: number;32 label?: string;33 count?: number;34 /** Size of each dot, as a multiple of the default. */35 dotSize?: number;36 /** Ripple of accent light across the swarm once it has packed. */37 pulse?: boolean;38 accent?: string;39 /** Color of the counter; overrides the theme ink. */40 ink?: string;41 /** Solid fill in place of the gradient ground; "transparent" drops it. */42 background?: string;43 /** Typeface for the counter and label. Load it yourself; Bricolage Grotesque by default. */44 fontFamily?: string;45 /** Size of the swarm and counter, around the frame's center. */46 scale?: number;47 /** Frames to hold the first frame before anything moves. */48 delay?: number;49 speed?: number;50 theme?: "dark" | "light";51 ground?: GshPresetId;52 groundParams?: Partial<GradientParams>;53}54const PLACEHOLDER_MARK_PATH =55 "M 62.03 11.13 A 56 56 0 1 1 61.99 11.13 Z";5657const DEFAULT_SHAPE: SwarmShapePart[] = [{ d: PLACEHOLDER_MARK_PATH }];58const MESH = {59 dark: ["#ECDCF2", "#8796EE", "#2E45B7", "#8796EE", "#ECDCF2"],60 light: ["#8796EE", "#2E45B7", "#0E1645", "#2E45B7", "#8796EE"],61} as const;62const MESH_POS = [63 [0.16, 0.2],64 [0.84, 0.26],65 [0.72, 0.86],66 [0.22, 0.8],67 [0.52, 0.48],68] as const;6970const EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);71const EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);72const FLIGHT = Easing.bezier(0.65, 0, 0.35, 1);73const CAMERA = Easing.bezier(0.87, 0, 0.13, 1);74const CARD = { mass: 0.8, damping: 18, stiffness: 160 };75const CHIP = { mass: 0.5, damping: 12, stiffness: 200 };76const LIGHT_GLASS: Partial<GradientParams> = {77 colors: ["#a9bbff", "#3b5bfd", "#1a2a9e", "#8fa6ff"],78};79const CLAMP = {80 extrapolateLeft: "clamp",81 extrapolateRight: "clamp",82} as const;8384const THEMES = {85 dark: {86 bg: "#020309",87 ground: "spotlight" as GshPresetId,88 fg: "#F3F2EF",89 muted: "rgba(243,242,239,0.55)",90 scrim: "2,3,9",91 scrimA: 0.35,92 },93 light: {94 bg: "#EEF0F6",95 ground: "glasswork" as GshPresetId,96 fg: "#121214",97 muted: "rgba(18,18,20,0.55)",98 scrim: "244,244,247",99 scrimA: 0.82,100 },101} as const;102const CURSOR = "M0 0L0 14.2L3.6 10.9L6.1 16.4L8.6 15.3L6.1 9.9L11 9.9Z";103104function hexRgb(hex: string): [number, number, number] {105 const n = Number.parseInt(hex.replace("#", ""), 16);106 return [(n >> 16) & 255, (n >> 8) & 255, n & 255];107}108function meshAt(109 ux: number,110 uy: number,111 cols: readonly [number, number, number][],112 drift: number,113) {114 let r = 0;115 let g = 0;116 let b = 0;117 let wsum = 0;118 for (let i = 0; i < cols.length; i++) {119 const [bx, by] = MESH_POS[i];120 const dx = ux - (bx + Math.sin(drift + i * 1.7) * 0.04);121 const dy = uy - (by + Math.cos(drift * 0.8 + i * 2.3) * 0.04);122 const w = Math.exp(-(dx * dx + dy * dy) / 0.06);123 r += cols[i][0] * w;124 g += cols[i][1] * w;125 b += cols[i][2] * w;126 wsum += w;127 }128 return [r / wsum, g / wsum, b / wsum] as const;129}130131function hash(n: number) {132 const x = Math.sin(n * 127.1 + 311.7) * 43758.5453;133 return x - Math.floor(x);134}135136type Poly = [number, number][];137138function insideEvenOdd(px: number, py: number, polys: Poly[]) {139 let inside = false;140 for (const poly of polys) {141 for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {142 const [xi, yi] = poly[i];143 const [xj, yj] = poly[j];144 if (145 yi > py !== yj > py &&146 px < ((xj - xi) * (py - yi)) / (yj - yi) + xi147 ) {148 inside = !inside;149 }150 }151 }152 return inside;153}154155interface Target {156 x: number;157 y: number;158 accent: boolean;159 edge: boolean;160}161function sampleTargets(parts: SwarmShapePart[], count: number) {162 const flat = parts.map((part) => {163 const polys: Poly[] = [];164 const outlines: { poly: Poly; len: number }[] = [];165 for (const sub of getSubpaths(part.d)) {166 const len = getLength(sub);167 if (len <= 0) continue;168 const n = Math.max(16, Math.min(500, Math.ceil(len / 0.5)));169 const poly: Poly = [];170 for (let i = 0; i <= n; i++) {171 const p = getPointAtLength(sub, (len * i) / n);172 if (p) poly.push([p.x, p.y]);173 }174 polys.push(poly);175 outlines.push({ poly, len });176 }177 return { polys, outlines, accent: Boolean(part.accent) };178 });179180 let minX = Number.POSITIVE_INFINITY;181 let minY = Number.POSITIVE_INFINITY;182 let maxX = Number.NEGATIVE_INFINITY;183 let maxY = Number.NEGATIVE_INFINITY;184 for (const part of flat) {185 for (const poly of part.polys) {186 for (const [x, y] of poly) {187 minX = Math.min(minX, x);188 minY = Math.min(minY, y);189 maxX = Math.max(maxX, x);190 maxY = Math.max(maxY, y);191 }192 }193 }194 const bw = maxX - minX || 1;195 const bh = maxY - minY || 1;196 const which = (x: number, y: number) => {197 for (let k = flat.length - 1; k >= 0; k--) {198 if (insideEvenOdd(x, y, flat[k].polys)) return k;199 }200 return -1;201 };202 const g = 80;203 let hits = 0;204 for (let i = 0; i < g; i++) {205 for (let j = 0; j < g; j++) {206 if (which(minX + ((i + 0.5) / g) * bw, minY + ((j + 0.5) / g) * bh) >= 0)207 hits++;208 }209 }210 const area = (hits / (g * g)) * bw * bh;211 const perimeter = flat.reduce(212 (s, p) => s + p.outlines.reduce((a, o) => a + o.len, 0),213 0,214 );215 const a = count;216 const b = -perimeter / 0.9;217 const c = -area / 0.866;218 const spacing = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);219220 const out: Target[] = [];221 for (const part of flat) {222 for (const { poly, len } of part.outlines) {223 const n = Math.max(6, Math.round(len / (spacing * 0.9)));224 for (let i = 0; i < n; i++) {225 const idx = Math.floor((i / n) * (poly.length - 1));226 const [ax, ay] = poly[Math.max(0, idx - 1)];227 const [bx, by] = poly[Math.min(poly.length - 1, idx + 1)];228 const l = Math.hypot(bx - ax, by - ay) || 1;229 let nx = -(by - ay) / l;230 let ny = (bx - ax) / l;231 const [px, py] = poly[idx];232 if (!insideEvenOdd(px + nx * 0.05, py + ny * 0.05, part.polys)) {233 nx = -nx;234 ny = -ny;235 }236 out.push({237 x: px + nx * spacing * 0.3,238 y: py + ny * spacing * 0.3,239 accent: part.accent,240 edge: true,241 });242 }243 }244 }245 const edgeCount = out.length;246 const rowH = spacing * 0.866;247 let row = 0;248 for (let y = minY + rowH * 0.6; y < maxY; y += rowH, row++) {249 const off = row % 2 ? spacing / 2 : 0;250 for (let x = minX + off + spacing * 0.5; x < maxX; x += spacing) {251 const k = out.length * 13 + row * 7;252 const jx = x + (hash(k + 0.31) - 0.5) * spacing * 0.3;253 const jy = y + (hash(k + 0.77) - 0.5) * spacing * 0.3;254 const part = which(jx, jy);255 if (part < 0) continue;256 let near = false;257 for (let e = 0; e < edgeCount; e++) {258 const dx = out[e].x - jx;259 const dy = out[e].y - jy;260 if (dx * dx + dy * dy < spacing * spacing * 0.42) {261 near = true;262 break;263 }264 }265 if (!near)266 out.push({ x: jx, y: jy, accent: flat[part].accent, edge: false });267 }268 }269 const cx = minX + bw / 2;270 const cy = minY + bh / 2;271 return {272 points: out.map((p) => ({ ...p, x: (p.x - cx) / bh, y: (p.y - cy) / bh })),273 spacing: spacing / bh,274 aspect: bw / bh,275 };276}277278interface Particle {279 tx: number;280 ty: number;281 sx: number;282 sy: number;283 cx: number;284 cy: number;285 start: number;286 dur: number;287 arrive: number;288 cursor: boolean;289 size: number;290 ux: number;291 uy: number;292 tone: number;293 depth: number;294 phase: number;295 accent: boolean;296}297298function formatNumber(n: number) {299 return Math.round(n)300 .toString()301 .replace(/\B(?=(\d{3})+(?!\d))/g, ",");302}303304function bezier(p: Particle, e: number) {305 const k = 1 - e;306 return {307 x: k * k * p.sx + 2 * k * e * p.cx + e * e * p.tx,308 y: k * k * p.sy + 2 * k * e * p.cy + e * e * p.ty,309 };310}311312/** The foreground at `scale`, around the frame's center; nothing extra at 1. */313function ScaleLayer({314 scale,315 children,316}: {317 scale: number;318 children: React.ReactNode;319}) {320 if (scale === 1) return children;321 return (322 <AbsoluteFill style={{ transform: `scale(${scale})` }}>323 {children}324 </AbsoluteFill>325 );326}327328export function WaitlistSwarm({329 shape = DEFAULT_SHAPE,330 value = 12480,331 label = "on the waitlist",332 count = 760,333 dotSize = 1,334 pulse = true,335 accent = "#3B5BFD",336 ink,337 background,338 fontFamily = sans,339 scale: sceneScale = 1,340 delay = 0,341 speed = 1,342 theme = "dark",343 ground,344 groundParams,345}: WaitlistSwarmProps) {346 const t = Math.max(0, useCurrentFrame() - delay) * speed;347 const { width, height, fps } = useVideoConfig();348 const tall = height > width;349 const u = height / 720;350 const pal = THEMES[theme];351352 const parts = useMemo(353 () => (typeof shape === "string" ? [{ d: shape }] : shape),354 [shape],355 );356 const sampled = useMemo(() => sampleTargets(parts, count), [parts, count]);357358 const markH = tall ? width * 0.62 : height * 0.56;359 const fx = tall ? width / 2 : width * 0.33;360 const fy = tall ? height * 0.34 : height * 0.5;361 const sp = sampled.spacing * markH;362363 const particles = useMemo(() => {364 const list: Particle[] = sampled.points.map((pt, i) => {365 const h1 = hash(i + 1.1);366 const h2 = hash(i + 2.3);367 const h3 = hash(i + 3.7);368 const h4 = hash(i + 4.9);369 const tx = fx + pt.x * markH;370 const ty = fy + pt.y * markH;371 const base = Math.atan2(pt.y, pt.x * 0.8);372 const ang = base + (h1 - 0.5) * 2.4;373 const dx = Math.cos(ang);374 const dy = Math.sin(ang);375 const reach = Math.min(376 Math.abs((width / 2 + 60 * u) / (dx || 1e-3)),377 Math.abs((height / 2 + 60 * u) / (dy || 1e-3)),378 );379 const far = reach + h2 * 260 * u;380 const sx = width / 2 + dx * far;381 const sy = height / 2 + dy * far;382 const mx = (sx + tx) / 2;383 const my = (sy + ty) / 2;384 const field =385 Math.sin(mx / (210 * u) + 1.3) * 1.4 +386 Math.cos(my / (170 * u) - 0.7) * 1.2 +387 Math.sin((mx + my) / (330 * u) + 2.1);388 const dist = Math.hypot(tx - sx, ty - sy);389 const bend = dist * (0.32 + h3 * 0.14);390 const ccx = mx + Math.cos(field) * bend;391 const ccy = my + Math.sin(field) * bend;392 const radial = Math.min(1, Math.hypot(pt.x, pt.y) / 0.6);393 const start = -22 + (1 - radial) * 20 + h4 * 34;394 const dur = 40 + h3 * 18 + (far / (width * 0.7)) * 10;395 const cursor = !pt.accent && hash(i + 9.1) < 0.12;396 return {397 tx,398 ty,399 sx,400 sy,401 cx: ccx,402 cy: ccy,403 start,404 dur,405 arrive: start + dur,406 cursor,407 size: pt.edge ? 0.8 + h2 * 0.16 : 0.62 + hash(i + 7.7) ** 1.4 * 0.42,408 ux: pt.x / sampled.aspect + 0.5,409 uy: pt.y + 0.5,410 tone: pt.edge ? 1.04 : 0.8 + hash(i + 5.3) * 0.3,411 depth: hash(i + 8.2),412 phase: h1 * Math.PI * 2,413 accent: pt.accent,414 };415 });416 return list.sort((a, b) => a.depth - b.depth);417 }, [sampled, fx, fy, markH, width, height, u]);418 const meshCols = useMemo(() => MESH[theme].map(hexRgb), [theme]);419 const drift = t / 60;420421 const arrivals = useMemo(422 () => particles.map((p) => p.arrive).sort((a, b) => a - b),423 [particles],424 );425 const firstArrival = arrivals[Math.floor(arrivals.length * 0.04)];426 const lastArrival = arrivals[arrivals.length - 1];427 let landed = 0;428 for (const a of arrivals) if (a <= t) landed++;429 const fill = landed / arrivals.length;430 const pack = 1 + 0.06 * (1 - EASE_IO(fill));431 const cam = interpolate(t, [0, 170], [1.07, 1], {432 ...CLAMP,433 easing: CAMERA,434 });435 const camY = interpolate(t, [0, 170], [10 * u, 0], {436 ...CLAMP,437 easing: CAMERA,438 });439 const pulseStart = lastArrival + 8;440 const origin = useMemo(() => {441 const acc = particles.filter((p) => p.accent);442 const pts = acc.length > 8 ? acc : particles;443 return {444 x: pts.reduce((s, p) => s + p.tx, 0) / pts.length,445 y: pts.reduce((s, p) => s + p.ty, 0) / pts.length,446 };447 }, [particles]);448 const pulseR = (t - pulseStart) * 11 * u;449 const pulseW = 54 * u;450 const pulseOn = pulse && t > pulseStart && pulseR < markH * 1.6;451452 const els: React.ReactNode[] = [];453 const ghosts: React.ReactNode[] = [];454 for (let i = 0; i < particles.length; i++) {455 const p = particles[i];456 const raw = (t - p.start) / p.dur;457 if (raw <= 0) continue;458 const prog = Math.min(1, raw);459 const e = FLIGHT(prog);460 const pos = bezier(p, e);461 const ptx = fx + (p.tx - fx) * pack;462 const pty = fy + (p.ty - fy) * pack;463 let x = pos.x + (ptx - p.tx) * e;464 let y = pos.y + (pty - p.ty) * e;465 const docked = interpolate(t - p.arrive, [0, 20], [0, 1], CLAMP);466 x += Math.sin(t / 19 + p.phase) * 0.5 * u * docked;467 y += Math.cos(t / 23 + p.phase) * 0.5 * u * docked;468469 let scale = 1 + (1 - e) * p.depth * 1.5;470 let opacity = interpolate(raw, [0, 0.12], [0, 1], CLAMP);471 opacity *= 0.55 + 0.45 * Math.min(1, e * 1.4);472473 let glow = 0;474 if (pulseOn) {475 const d = Math.hypot(p.tx - origin.x, p.ty - origin.y);476 glow = Math.exp(-(((d - pulseR) / pulseW) ** 2));477 const fade = interpolate(478 pulseR,479 [markH * 1.1, markH * 1.6],480 [1, 0],481 CLAMP,482 );483 glow *= fade;484 const nx = (p.tx - origin.x) / (d || 1);485 const ny = (p.ty - origin.y) / (d || 1);486 x += nx * glow * 4 * u;487 y += ny * glow * 4 * u;488 scale *= 1 + glow * 0.5;489 }490 const rest = p.accent ? 1 : 1 - docked * 0.16;491 opacity *= rest + (1 - rest) * glow;492493 const r = ((sp * p.size * scale) / 2) * dotSize;494 const [mr, mg, mb] = p.accent495 ? hexRgb(accent)496 : meshAt(p.ux, p.uy, meshCols, drift);497 const tk = theme === "dark" ? p.tone : 2 - p.tone;498 const color =499 theme === "dark"500 ? `rgb(${Math.min(255, mr * tk)},${Math.min(255, mg * tk)},${Math.min(255, mb * tk)})`501 : `rgb(${mr + (245 - mr) * (tk - 1) * 0.9},${mg + (244 - mg) * (tk - 1) * 0.9},${mb + (240 - mb) * (tk - 1) * 0.9})`;502 if (prog < 0.92) {503 for (let k = 1; k <= 2; k++) {504 const pe = FLIGHT(Math.max(0, (t - k * 1.4 - p.start) / p.dur));505 const gp = bezier(p, pe);506 const sx = gp.x + (ptx - p.tx) * pe;507 const sy = gp.y + (pty - p.ty) * pe;508 if (Math.hypot(sx - x, sy - y) < r * 0.8) continue;509 ghosts.push(510 <circle511 key={`g${i}-${k}`}512 cx={sx}513 cy={sy}514 r={r * (p.cursor ? 0.5 : 0.85)}515 fill={color}516 opacity={opacity * (k === 1 ? 0.22 : 0.09)}517 />,518 );519 }520 }521 const morph = p.cursor ? interpolate(prog, [0.78, 1], [0, 1], CLAMP) : 1;522 if (p.cursor && morph < 1) {523 const s = (sp * 1.35 * scale * (1 - morph * 0.4)) / 16;524 const vx =525 x - bezier(p, FLIGHT(Math.max(0, (t - 1 - p.start) / p.dur))).x;526 const tilt = Math.max(-14, Math.min(14, -vx * 0.6)) * (1 - e);527 els.push(528 <path529 key={`c${i}`}530 d={CURSOR}531 fill={color}532 stroke={pal.bg}533 strokeWidth={1.1 / s}534 strokeLinejoin="round"535 opacity={opacity * (1 - morph)}536 transform={`translate(${x - 4 * s} ${y - 8 * s}) rotate(${tilt}) scale(${s})`}537 />,538 );539 }540 if (morph > 0) {541 els.push(542 <circle543 key={i}544 cx={x}545 cy={y}546 r={r * (0.7 + morph * 0.3)}547 fill={color}548 stroke={pal.bg}549 strokeWidth={Math.max(0.8, r * 0.22)}550 opacity={opacity * morph}551 />,552 );553 if (glow > 0.04 && !p.accent) {554 els.push(555 <circle556 key={`p${i}`}557 cx={x}558 cy={y}559 r={r * (0.7 + morph * 0.3)}560 fill={accent}561 opacity={glow * 0.45}562 />,563 );564 }565 }566 }567 const shown =568 landed >= arrivals.length ? value : Math.min(value - 1, value * fill);569 const counterRise = spring({570 frame: t - (firstArrival - 16),571 fps,572 config: CARD,573 });574 const counterIn = interpolate(575 t,576 [firstArrival - 16, firstArrival + 2],577 [0, 1],578 { ...CLAMP, easing: EASE_OUT },579 );580 const labelIn = interpolate(581 t,582 [firstArrival - 10, firstArrival + 10],583 [0, 1],584 { ...CLAMP, easing: EASE_OUT },585 );586 const punch =587 t >= lastArrival588 ? 1 + 0.05 * (1 - spring({ frame: t - lastArrival, fps, config: CHIP }))589 : 1;590 const numFs = tall ? height * 0.1 : height * 0.18;591 const labelFs = tall ? height * 0.03 : height * 0.05;592 const counterBox: React.CSSProperties = tall593 ? {594 left: 0,595 right: 0,596 top: fy + markH / 2 + height * 0.07,597 alignItems: "center",598 }599 : {600 left: width * 0.56,601 top: fy - numFs * 0.72,602 alignItems: "flex-start",603 };604605 return (606 <AbsoluteFill607 style={{608 background: background === undefined ? pal.bg : undefined,609 overflow: "hidden",610 }}611 >612 {background === undefined ? (613 <>614 <GshGround615 preset={groundParams ? undefined : (ground ?? pal.ground)}616 params={617 groundParams ??618 (theme === "light" && !ground ? LIGHT_GLASS : undefined)619 }620 timeOffset={theme === "dark" ? 2 : 11}621 timeScale={0.45}622 />623 <AbsoluteFill624 style={{625 background: `radial-gradient(${tall ? "95% 48%" : "70% 80%"} at ${tall ? 50 : 45}% ${tall ? 42 : 50}%, rgba(${pal.scrim},${pal.scrimA}) 0%, rgba(${pal.scrim},0) 100%)`,626 }}627 />628 </>629 ) : (630 <AbsoluteFill631 style={{632 background: background === "transparent" ? undefined : background,633 }}634 />635 )}636 <ScaleLayer scale={sceneScale}>637 <AbsoluteFill638 style={{639 transform: `translateY(${camY}px) scale(${cam})`,640 transformOrigin: `${(fx / width) * 100}% ${(fy / height) * 100}%`,641 }}642 >643 <svg644 width={width}645 height={height}646 viewBox={`0 0 ${width} ${height}`}647 style={{ position: "absolute", inset: 0, overflow: "visible" }}648 aria-hidden649 >650 <g>{ghosts}</g>651 <g>{els}</g>652 </svg>653 </AbsoluteFill>654655 <div656 style={{657 position: "absolute",658 ...counterBox,659 display: "flex",660 flexDirection: "column",661 gap: labelFs * 0.5,662 fontFamily,663 color: ink ?? pal.fg,664 }}665 >666 <span667 style={{668 fontWeight: 300,669 fontSize: numFs,670 lineHeight: 1,671 letterSpacing: "-0.045em",672 fontVariantNumeric: "tabular-nums",673 opacity: counterIn,674 transformOrigin: tall ? "50% 60%" : "0% 60%",675 transform: `translateY(${(1 - counterRise) * 0.18 * numFs}px) scale(${punch})`,676 filter: `blur(${(1 - counterIn) * 6 * u}px)`,677 }}678 >679 {formatNumber(shown)}680 </span>681 <span682 style={{683 fontWeight: 400,684 fontSize: labelFs,685 lineHeight: 1.1,686 letterSpacing: "-0.01em",687 paddingLeft: tall ? 0 : numFs * 0.05,688 color: pal.muted,689 opacity: labelIn,690 transform: `translateY(${(1 - labelIn) * 0.4 * labelFs}px)`,691 }}692 >693 {label}694 </span>695 </div>696 </ScaleLayer>697 </AbsoluteFill>698 );699}









