Hex Fill
A hexagonal grid fills with gradient cells up to an 86% reading.
Source
components/kuchi/hex-fill.tsx
1"use client";23import { loadFont } from "@remotion/google-fonts/BricolageGrotesque";4import type React from "react";5import { useId, useMemo } from "react";6import {7 AbsoluteFill,8 Easing,9 interpolate,10 spring,11 useCurrentFrame,12 useVideoConfig,13} from "remotion";1415const { fontFamily: brico } = loadFont("normal", {16 weights: ["300", "400"],17 subsets: ["latin"],18});1920// gradients.sh "aurora" palette21const AURORA = {22 bg: "#03050b",23 c: ["#a0f0c8", "#3fb5a3", "#5147b6", "#b25c9c"],24} as const;2526export interface HexFillProps {27 value?: number;28 label?: string;29 rings?: number;30 /** Mark after the figure. */31 unit?: string;32 accent?: string;33 /** Color of the figure; overrides the theme ink. */34 ink?: string;35 /** Background fill; "transparent" drops it. */36 background?: string;37 /** Typeface for the copy. Load it yourself; Bricolage Grotesque by default. */38 fontFamily?: string;39 /** Weight of the figure. */40 fontWeight?: number;41 /** Frames the cells take to fill. */42 fillLength?: number;43 /** Size of the foreground, around the frame's center. */44 scale?: number;45 /** Frames to hold the first frame before anything moves. */46 delay?: number;47 grain?: boolean;48 theme?: "dark" | "light";49 speed?: number;50}5152const FILL0 = 14;53const FILL_LEN = 70;54const RING_LAG = 1.2;55const CELL = 0.88;5657const EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);58const EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);59const CLAMP = {60 extrapolateLeft: "clamp",61 extrapolateRight: "clamp",62} as const;6364const THEMES = {65 dark: {66 bg: AURORA.bg,67 fg: "#F1F3F2",68 muted: "#8A8F99",69 hair: "rgba(255,255,255,0.26)",70 stops: [AURORA.c[0], AURORA.c[1], AURORA.c[2], AURORA.c[3]],71 grain: 0.06,72 },73 light: {74 bg: "#F5F4F0",75 fg: "#121214",76 muted: "#6E6C67",77 hair: "rgba(18,18,20,0.24)",78 stops: ["#52c7a4", AURORA.c[1], AURORA.c[2], AURORA.c[3]],79 grain: 0.045,80 },81} as const;8283interface Cell {84 q: number;85 r: number;86 x: number;87 y: number;88 ring: number;89 ang: number;90}9192function buildCells(rings: number): Cell[] {93 const out: Cell[] = [];94 for (let q = -rings; q <= rings; q++) {95 for (let r = -rings; r <= rings; r++) {96 const s = -q - r;97 const ring = Math.max(Math.abs(q), Math.abs(r), Math.abs(s));98 if (ring > rings) continue;99 const x = Math.sqrt(3) * (q + r / 2);100 const y = 1.5 * r;101 let ang = ring === 0 ? -1 : Math.atan2(x, -y);102 if (ring > 0 && ang < -1e-6) ang += Math.PI * 2;103 out.push({ q, r, x, y, ring, ang: ring === 0 ? 0 : ang / (Math.PI * 2) });104 }105 }106 out.sort((a, b) => a.ang - b.ang || a.ring - b.ring);107 return out;108}109110const NB = [111 [1, 0],112 [-1, 0],113 [0, 1],114 [0, -1],115 [1, -1],116 [-1, 1],117];118119function pickFill(cells: Cell[], F: number) {120 const index = new Map<string, number>();121 cells.forEach((c, i) => {122 index.set(`${c.q},${c.r}`, i);123 });124 const nbs = cells.map((c) =>125 NB.map(([dq, dr]) => index.get(`${c.q + dq},${c.r + dr}`)).filter(126 (v): v is number => v !== undefined,127 ),128 );129 const on = cells.map((_, i) => i < F);130 const score = (i: number) =>131 nbs[i].reduce((sum, j) => sum + (on[j] ? 1 : 0), 0) / nbs[i].length;132 for (let it = 0; it < 40; it++) {133 let worst = -1;134 let best = -1;135 for (let i = 1; i < cells.length; i++) {136 if (on[i] && (worst < 0 || score(i) < score(worst))) worst = i;137 if (!on[i] && (best < 0 || score(i) > score(best))) best = i;138 }139 if (worst < 0 || best < 0 || score(best) - score(worst) < 0.34) break;140 on[worst] = false;141 on[best] = true;142 }143 const chosen = cells.filter((_, i) => on[i]);144 const rest = cells.filter((_, i) => !on[i]);145 return [...chosen, ...rest];146}147148function hexPath(cx: number, cy: number, r: number) {149 let d = "";150 for (let i = 0; i < 6; i++) {151 const a = (Math.PI / 3) * i - Math.PI / 2;152 d += `${i === 0 ? "M" : "L"}${(cx + r * Math.cos(a)).toFixed(2)} ${(cy + r * Math.sin(a)).toFixed(2)}`;153 }154 return `${d}Z`;155}156157function Grain({ opacity }: { opacity: number }) {158 const id = `g${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;159 return (160 <svg161 width="100%"162 height="100%"163 style={{ position: "absolute", inset: 0, opacity }}164 aria-hidden165 >166 <filter id={id}>167 <feTurbulence168 type="fractalNoise"169 baseFrequency="0.9"170 numOctaves={2}171 seed={53}172 stitchTiles="stitch"173 />174 <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" />175 </filter>176 <rect width="100%" height="100%" filter={`url(#${id})`} />177 </svg>178 );179}180181/** The foreground at `scale`, around the frame's center; nothing extra at 1. */182function ScaleLayer({183 scale,184 children,185}: {186 scale: number;187 children: React.ReactNode;188}) {189 if (scale === 1) return children;190 return (191 <AbsoluteFill style={{ transform: `scale(${scale})` }}>192 {children}193 </AbsoluteFill>194 );195}196197export function HexFill({198 value = 86,199 label = "of renders served from cache",200 rings = 7,201 unit = "%",202 accent = "#3B5BFD",203 ink,204 background,205 fontFamily = brico,206 fontWeight = 300,207 fillLength = FILL_LEN,208 scale = 1,209 delay = 0,210 grain = true,211 theme = "dark",212 speed = 1,213}: HexFillProps) {214 const t = Math.max(0, useCurrentFrame() - delay) * speed;215 const { width, height, fps } = useVideoConfig();216 const u = Math.min(width / 1280, height / 720);217 const p = THEMES[theme];218 const fg = ink ?? p.fg;219 const N = Math.max(3, Math.min(10, Math.round(rings)));220 const pct = Math.max(0, Math.min(100, value));221 const all = useMemo(() => buildCells(N), [N]);222 const F = Math.round((pct / 100) * all.length);223 const cells = useMemo(() => pickFill(all, F), [all, F]);224225 const heroSize = 168 * u;226 const pctSize = heroSize * 0.55;227 const labelSize = 24 * u;228 const digits = String(Math.round(pct)).length;229 const heroW = digits * 0.6 * heroSize + 0.62 * pctSize;230 const labelMax = 300 * u;231 const labelW = Math.min(labelMax, label.length * 0.5 * labelSize) + 22 * u;232 const textW = Math.max(heroW, labelW);233 const gap = 80 * u;234235 const gridH = 3 * N + 2;236 const gridWUnits = (2 * N + 1) * Math.sqrt(3);237 const s = Math.min(238 (height * 0.74) / gridH,239 (width * 0.86 - gap - textW) / gridWUnits,240 );241 const gridW = gridWUnits * s;242 const left = (width - (gridW + gap + textW)) / 2;243 const gcx = left + gridW / 2;244 const gcy = height / 2;245 const textX = left + gridW + gap;246247 const angMax = F > 0 ? cells[F - 1].ang : 1;248 const fillAt = (i: number) => {249 const k = Math.min(1, cells[i].ang / Math.max(1e-3, angMax));250 return (251 FILL0 +252 (fillLength * Math.acos(1 - 2 * k)) / Math.PI +253 cells[i].ring * RING_LAG254 );255 };256 let fillEnd = FILL0;257 let filled = 0;258 for (let i = 0; i < F; i++) {259 const at = fillAt(i);260 fillEnd = Math.max(fillEnd, at);261 if (t >= at) filled++;262 }263 const shown = F > 0 ? Math.round((pct * filled) / F) : 0;264265 const push = interpolate(t, [0, fillEnd + 40], [0, 1], {266 ...CLAMP,267 easing: EASE_IO,268 });269 const camScale = 0.985 + 0.025 * push;270 const textIn = interpolate(t, [4, 22], [0, 1], {271 ...CLAMP,272 easing: EASE_OUT,273 });274 const labelIn = interpolate(t, [10, 28], [0, 1], {275 ...CLAMP,276 easing: EASE_OUT,277 });278 const done = interpolate(t, [fillEnd + 2, fillEnd + 18], [0, 1], {279 ...CLAMP,280 easing: EASE_IO,281 });282 const hexR = s * CELL;283 const gradId = `hf${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;284 const gR = (gridH * s) / 2;285 const drift = interpolate(t, [0, fillEnd + 40], [0, 1], {286 ...CLAMP,287 easing: EASE_IO,288 });289 const sw = Math.max(1, 1.1 * u);290291 return (292 <AbsoluteFill293 style={{294 background:295 background === "transparent" ? undefined : (background ?? p.bg),296 overflow: "hidden",297 }}298 >299 <ScaleLayer scale={scale}>300 <AbsoluteFill301 style={{302 transform: `scale(${camScale})`,303 transformOrigin: "50% 50%",304 }}305 >306 <svg307 width={width}308 height={height}309 style={{ position: "absolute", inset: 0 }}310 aria-hidden311 >312 <defs>313 <linearGradient314 id={gradId}315 gradientUnits="userSpaceOnUse"316 x1={gcx - gR * (0.9 - 0.15 * drift)}317 y1={gcy - gR}318 x2={gcx + gR * (0.9 + 0.15 * drift)}319 y2={gcy + gR}320 >321 <stop offset="0" stopColor={p.stops[0]} />322 <stop offset="0.36" stopColor={p.stops[1]} />323 <stop offset="0.74" stopColor={p.stops[2]} />324 <stop offset="1" stopColor={p.stops[3]} />325 </linearGradient>326 </defs>327 {cells.map((c, i) => {328 const x = gcx + c.x * s;329 const y = gcy + c.y * s;330 const ringIn = interpolate(331 t,332 [c.ring * 1.4, c.ring * 1.4 + 10],333 [0, 1],334 {335 ...CLAMP,336 easing: EASE_OUT,337 },338 );339 const isFill = i < F;340 const local = isFill ? t - fillAt(i) : -1;341 const pop =342 local >= 0343 ? spring({344 frame: local,345 fps,346 config: { damping: 13, stiffness: 240, mass: 0.6 },347 })348 : 0;349 const outlineA = isFill ? ringIn : ringIn * (1 - 0.2 * done);350 return (351 <g key={`${c.x.toFixed(2)}_${c.y.toFixed(2)}`}>352 <path353 d={hexPath(x, y, hexR * (0.94 + 0.06 * ringIn))}354 fill="none"355 stroke={p.hair}356 strokeWidth={sw}357 opacity={358 local >= 0359 ? outlineA * (1 - Math.min(1, local / 4))360 : outlineA361 }362 />363 {local >= 0 ? (364 <path365 d={hexPath(x, y, hexR * (0.55 + 0.45 * pop))}366 fill={`url(#${gradId})`}367 />368 ) : null}369 </g>370 );371 })}372 </svg>373374 <div375 style={{376 position: "absolute",377 left: textX,378 top: gcy,379 transform: "translateY(-50%)",380 display: "flex",381 flexDirection: "column",382 alignItems: "flex-start",383 gap: 18 * u,384 }}385 >386 <div387 style={{388 fontFamily,389 fontWeight,390 fontSize: heroSize,391 lineHeight: 0.9,392 letterSpacing: "-0.05em",393 color: fg,394 fontVariantNumeric: "tabular-nums",395 whiteSpace: "nowrap",396 opacity: textIn,397 transform: `translateY(${(1 - textIn) * 14 * u}px)`,398 }}399 >400 {shown}401 <span402 style={{403 fontSize: pctSize,404 letterSpacing: "-0.02em",405 marginLeft: 6 * u,406 color: p.muted,407 }}408 >409 {unit}410 </span>411 </div>412 <div413 style={{414 display: "flex",415 alignItems: "flex-start",416 gap: 12 * u,417 marginLeft: 6 * u,418 maxWidth: labelMax + 22 * u,419 opacity: labelIn,420 transform: `translateY(${(1 - labelIn) * 8 * u}px)`,421 }}422 >423 <span424 style={{425 width: 10 * u,426 height: 10 * u,427 borderRadius: "50%",428 background: accent,429 flexShrink: 0,430 marginTop: labelSize * 0.42,431 }}432 />433 <span434 style={{435 fontFamily,436 fontWeight: 400,437 fontSize: labelSize,438 lineHeight: 1.3,439 letterSpacing: "-0.01em",440 color: p.muted,441 textWrap: "balance",442 }}443 >444 {label}445 </span>446 </div>447 </div>448 </AbsoluteFill>449 </ScaleLayer>450451 <AbsoluteFill452 style={{453 background: `radial-gradient(ellipse 85% 85% at 50% 50%, transparent 64%, ${p.bg} 100%)`,454 opacity: 0.5,455 }}456 />457 {grain ? <Grain opacity={p.grain} /> : null}458 </AbsoluteFill>459 );460}









