import { hexLine, type AxialCoord, type HexCell } from "./hex.ts"; import { buildSignalCellStates, createOriginSignal, createPathSignal, type SignalCellState } from "./SignalEngine.ts"; export type GhLogoNodeRole = "border" | "g" | "h"; export interface GhLogoNode extends AxialCoord { id: string; role: GhLogoNodeRole; } export interface GhLogoEdge { from: string; to: string; role: GhLogoNodeRole; } export type GhLogoVariant = "border-reveal" | "center-origin" | "stroke-sweep" | "two-phase" | "idle-echo" | "corner-flow"; export interface GhLogoFrame { states: SignalCellState[]; phase: "background" | "ambient" | "border" | "g" | "h" | "settled" | "idle-signal"; progress: number; } function coordinateId({ q, r }: AxialCoord) { return `hex-${q}-${r}`; } function createPointyBorder(): AxialCoord[] { const vertices: AxialCoord[] = [ { q: 4, r: -8 }, { q: 8, r: -4 }, { q: 4, r: 4 }, { q: -4, r: 8 }, { q: -8, r: 4 }, { q: -4, r: -4 }, ]; const coordinates: AxialCoord[] = []; const occupied = new Set(); vertices.forEach((vertex, index) => { const next = vertices[(index + 1) % vertices.length]; const segment = hexLine(vertex, next); segment.forEach((coordinate, step) => { const isVisibleNode = step % 2 === 0 || step === segment.length - 1; const id = coordinateId(coordinate); if (!isVisibleNode || occupied.has(id)) return; occupied.add(id); coordinates.push(coordinate); }); }); return coordinates; } const borderCoordinates = createPointyBorder(); const gCoordinates: AxialCoord[] = [ { q: 1, r: -4 }, { q: -1, r: -3 }, { q: -3, r: -2 }, { q: -4, r: 0 }, { q: -5, r: 2 }, { q: -4, r: 3 }, { q: -3, r: 4 }, { q: -2, r: 2 }, { q: -1, r: 0 }, { q: -2, r: 0 }, ]; const hLeftCoordinates: AxialCoord[] = [ { q: 3, r: -4 }, { q: 2, r: -2 }, { q: 1, r: 0 }, { q: 0, r: 2 }, { q: -1, r: 4 }, ]; const hCrossCoordinates: AxialCoord[] = [ { q: 1, r: 0 }, { q: 2, r: 0 }, { q: 3, r: 0 }, { q: 4, r: 0 }, ]; const hRightCoordinates: AxialCoord[] = [ { q: 5, r: -2 }, { q: 4, r: 0 }, { q: 3, r: 2 }, ]; const hCoordinates: AxialCoord[] = [ ...hLeftCoordinates, ...hCrossCoordinates.slice(1, -1), ...hRightCoordinates, ]; const hTraversalCoordinates: AxialCoord[] = [ ...hLeftCoordinates, ...hLeftCoordinates.slice(0, -2).reverse(), ...hCrossCoordinates.slice(1), ...hRightCoordinates.slice(0, -1).reverse(), ...hRightCoordinates.slice(1), ]; const borderNodes = createNodes(borderCoordinates, "border"); const gNodes = createNodes(gCoordinates, "g"); const hNodes = createNodes(hCoordinates, "h"); const hLeftNodes = createNodes(hLeftCoordinates, "h"); const hCrossNodes = createNodes(hCrossCoordinates, "h"); const hRightNodes = createNodes(hRightCoordinates, "h"); const hTraversalNodes = createNodes(hTraversalCoordinates, "h"); const borderPath = borderNodes.map(({ q, r }) => ({ q, r })); const gPath = gNodes.map(({ q, r }) => ({ q, r })); const hTraversalPath = hTraversalNodes.map(({ q, r }) => ({ q, r })); const borderIds = new Set(borderNodes.map((node) => node.id)); const gIds = new Set(gNodes.map((node) => node.id)); const borderAndGIds = new Set([...borderIds, ...gIds]); const allIds = new Set([...borderAndGIds, ...hNodes.map((node) => node.id)]); const GH_LOGO_FRAME_STEP = 1000 / 30; const ghLogoFrameCache = new Map(); const ghLogoSettledFrameCache = new WeakMap(); function createNodes(coordinates: AxialCoord[], role: GhLogoNodeRole): GhLogoNode[] { return coordinates.map((coordinate) => ({ ...coordinate, id: coordinateId(coordinate), role })); } function createEdges(nodes: GhLogoNode[], close = false): GhLogoEdge[] { const edges = nodes.slice(1).map((node, index) => ({ from: nodes[index].id, to: node.id, role: node.role, })); if (close && nodes.length > 2) { edges.push({ from: nodes.at(-1)!.id, to: nodes[0].id, role: nodes[0].role }); } return edges; } export const GH_LOGO_NODES = [ ...borderNodes, ...gNodes, ...hNodes, ]; export const GH_LOGO_EDGES = [ ...createEdges(borderNodes, true), ...createEdges(gNodes), ...createEdges(hLeftNodes), ...createEdges(hCrossNodes), ...createEdges(hRightNodes), ]; export const GH_LOGO_ROLE_BY_CELL = Object.fromEntries( GH_LOGO_NODES.map((node) => [node.id, node.role]), ) as Record; const timeline = { backgroundEnd: 700, ambientEnd: 1700, borderEnd: 3100, gEnd: 3900, hEnd: 4600, }; function clamp(value: number) { return Math.min(1, Math.max(0, value)); } function sequenceStates( cells: HexCell[], elapsed: number, path: AxialCoord[], start: number, end: number, settledIds: Set, ): SignalCellState[] { const progress = clamp((elapsed - start) / Math.max(1, end - start)); const signal = createPathSignal(path, { startedAt: start, duration: Math.max(1, end - start), intensity: 1, }); const signalStates = signal ? buildSignalCellStates({ cells, signals: [signal], now: start + progress * (end - start) }) : {}; return cells.map((cell) => { const signalState = signalStates[cell.id]; if (signalState && signalState.state !== "idle") { return signalState; } if (settledIds.has(cell.id)) { return { id: cell.id, state: "resolved", intensity: 0.52 }; } return { id: cell.id, state: "idle", intensity: 0.05 }; }); } function getDelayedSignalElapsed(idleElapsed: number, cycle: number, duration: number) { const cycleElapsed = ((idleElapsed % cycle) + cycle) % cycle; const signalStart = cycle - duration; return cycleElapsed >= signalStart ? cycleElapsed - signalStart : null; } function getSettledFrame(cells: HexCell[], allIds: Set): GhLogoFrame { const cached = ghLogoSettledFrameCache.get(cells); if (cached) return cached; const frame: GhLogoFrame = { states: cells.map((cell) => ({ id: cell.id, state: allIds.has(cell.id) ? "resolved" : "idle", intensity: allIds.has(cell.id) ? 0.52 : 0.05, })), phase: "settled", progress: 1, }; ghLogoSettledFrameCache.set(cells, frame); return frame; } function buildIdleFrame(cells: HexCell[], variant: GhLogoVariant, frameTime: number, allIds: Set): GhLogoFrame { const idleElapsed = frameTime - timeline.hEnd; const borderNodes = GH_LOGO_NODES.filter((node) => node.role === "border"); const borderOrigin = borderNodes[0] ?? { q: 0, r: 0 }; const borderCycle = 14000; const centerCycle = 18000; const echoCycle = 22000; const sweepCycle = 16000; const signalDuration = 1800; const sweepDuration = 2400; const borderPathSignal = createPathSignal([...borderPath, ...gPath, ...hTraversalPath], { startedAt: 0, duration: sweepDuration, intensity: 1, }); switch (variant) { case "center-origin": { const signalElapsed = getDelayedSignalElapsed(idleElapsed, centerCycle, signalDuration); if (signalElapsed !== null) { const originSignal = createOriginSignal({ q: 0, r: 0 }, [], { startedAt: 0, duration: signalDuration, intensity: 0.9, }); const signalStates = buildSignalCellStates({ cells, signals: [originSignal], now: signalElapsed }); return { states: cells.map((cell) => signalStates[cell.id] ?? { id: cell.id, state: allIds.has(cell.id) ? "resolved" : "idle", intensity: allIds.has(cell.id) ? 0.52 : 0.05 }), phase: "idle-signal", progress: signalElapsed / signalDuration, }; } break; } case "stroke-sweep": { const signalElapsed = getDelayedSignalElapsed(idleElapsed, sweepCycle, sweepDuration); if (signalElapsed !== null && borderPathSignal) { const signalStates = buildSignalCellStates({ cells, signals: [borderPathSignal], now: signalElapsed }); return { states: cells.map((cell) => signalStates[cell.id] ?? { id: cell.id, state: allIds.has(cell.id) ? "resolved" : "idle", intensity: allIds.has(cell.id) ? 0.52 : 0.05 }), phase: "idle-signal", progress: signalElapsed / sweepDuration, }; } break; } case "two-phase": break; case "idle-echo": { const signalElapsed = getDelayedSignalElapsed(idleElapsed, echoCycle, signalDuration); if (signalElapsed !== null) { const originSignal = createOriginSignal({ q: 0, r: 0 }, [], { startedAt: 0, duration: signalDuration, intensity: 0.5, }); const signalStates = buildSignalCellStates({ cells, signals: [originSignal], now: signalElapsed }); return { states: cells.map((cell) => signalStates[cell.id] ?? { id: cell.id, state: allIds.has(cell.id) ? "resolved" : "idle", intensity: allIds.has(cell.id) ? 0.52 : 0.05 }), phase: "idle-signal", progress: signalElapsed / signalDuration, }; } break; } case "corner-flow": { const signalElapsed = getDelayedSignalElapsed(idleElapsed, borderCycle, signalDuration); if (signalElapsed !== null) { const originSignal = createOriginSignal(borderOrigin, [], { startedAt: 0, duration: signalDuration, intensity: 0.92, }); const signalStates = buildSignalCellStates({ cells, signals: [originSignal], now: signalElapsed }); return { states: cells.map((cell) => signalStates[cell.id] ?? { id: cell.id, state: allIds.has(cell.id) ? "resolved" : "idle", intensity: allIds.has(cell.id) ? 0.52 : 0.05 }), phase: "idle-signal", progress: signalElapsed / signalDuration, }; } break; } case "border-reveal": default: { const signalElapsed = getDelayedSignalElapsed(idleElapsed, borderCycle, signalDuration); if (signalElapsed !== null) { return { states: sequenceStates(cells, signalElapsed, borderPath, 0, signalDuration, allIds), phase: "idle-signal", progress: signalElapsed / signalDuration, }; } break; } } return getSettledFrame(cells, allIds); } export function getGhLogoFrame(cells: HexCell[], elapsed: number, variant: GhLogoVariant = "border-reveal"): GhLogoFrame { const frameTime = Math.floor(elapsed / GH_LOGO_FRAME_STEP) * GH_LOGO_FRAME_STEP; const cacheKey = variant + ":" + cells.length + ":" + frameTime; const cached = ghLogoFrameCache.get(cacheKey); if (cached) { return cached; } let result: GhLogoFrame; if (frameTime < timeline.backgroundEnd) { result = { states: cells.map((cell) => ({ id: cell.id, state: "idle", intensity: 0.02 + 0.04 * clamp(frameTime / timeline.backgroundEnd) })), phase: "background", progress: clamp(frameTime / timeline.backgroundEnd), }; } else if (frameTime < timeline.ambientEnd) { const ambientProgress = clamp((frameTime - timeline.backgroundEnd) / (timeline.ambientEnd - timeline.backgroundEnd)); const ambientIndexes = new Set([ Math.floor(cells.length * 0.18), Math.floor(cells.length * 0.52), Math.floor(cells.length * 0.81), ]); result = { states: cells.map((cell, index) => { if (!ambientIndexes.has(index)) { return { id: cell.id, state: "idle", intensity: 0.05 }; } const order = index === Math.floor(cells.length * 0.18) ? 0 : index === Math.floor(cells.length * 0.52) ? 1 : 2; const localProgress = clamp((ambientProgress - order * 0.14) / 0.72); const breath = Math.sin(Math.PI * localProgress) ** 2; return { id: cell.id, state: "idle", intensity: 0.05 + breath * 0.11 }; }), phase: "ambient", progress: ambientProgress, }; } else if (frameTime < timeline.borderEnd) { result = { states: sequenceStates(cells, frameTime, borderPath, timeline.ambientEnd, timeline.borderEnd, new Set()), phase: "border", progress: clamp((frameTime - timeline.ambientEnd) / (timeline.borderEnd - timeline.ambientEnd)), }; } else if (frameTime < timeline.gEnd) { result = { states: sequenceStates(cells, frameTime, gPath, timeline.borderEnd, timeline.gEnd, borderIds), phase: "g", progress: clamp((frameTime - timeline.borderEnd) / (timeline.gEnd - timeline.borderEnd)), }; } else if (frameTime < timeline.hEnd) { result = { states: sequenceStates(cells, frameTime, hTraversalPath, timeline.gEnd, timeline.hEnd, borderAndGIds), phase: "h", progress: clamp((frameTime - timeline.gEnd) / (timeline.hEnd - timeline.gEnd)), }; } else { result = buildIdleFrame(cells, variant, frameTime, allIds); } if (result.phase === "settled") return result; ghLogoFrameCache.set(cacheKey, result); if (ghLogoFrameCache.size > 480) { const firstKey = ghLogoFrameCache.keys().next().value; if (firstKey) { ghLogoFrameCache.delete(firstKey); } } return result; }