Repository Public
1import type { Tone } from "@/schema";2import { latexToHtml } from "@/renderer/latex";3import { latexToUnicode } from "@/renderer/mathtext";45/**6 * Generated cover of a sheet card. A sheet that has an equation shows it — its key formula, typeset on squared paper7 * in the colours of the sheet; otherwise a small node-and-edge motif drawn from the slug. No image file. Decorative.8 */9function rng(seed: string) {10 let h = 2166136261;11 for (let i = 0; i < seed.length; i++) { h ^= seed.charCodeAt(i); h = Math.imul(h, 16777619); }12 return () => { h += 0x6d2b79f5; let t = h; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };13}1415export function Cover({ seed, theme, accent, className, formula }: { seed: string; theme: string; accent: Tone | string; className?: string; formula?: string | null }) {16 const html = formula ? latexToHtml(formula, true) : "";17 if (html) {18 // The formula is sized from its length (an estimate of its width in em), in units of the cover's own width.19 const len = Math.max(8, [...latexToUnicode(formula ?? "")].length);20 return (21 <div className={`sheet cover-fx ${className ?? ""}`} data-theme={theme} data-accent={accent} aria-hidden="true">22 <span className="cover-fx-tag">ƒ</span>23 <span className="cover-fx-math" style={{ fontSize: `clamp(0.72rem, ${(150 / len).toFixed(2)}cqi, 1.9rem)` }} dangerouslySetInnerHTML={{ __html: html }} />24 </div>25 );26 }27 const r = rng(seed);28 const cols = 6, rows = 3, W = 360, H = 180;29 const nodes = Array.from({ length: cols * rows }, (_, i) => ({ x: 30 + (i % cols) * 60 + (r() - 0.5) * 22, y: 32 + Math.floor(i / cols) * 58 + (r() - 0.5) * 20, on: r() > 0.42, big: r() > 0.8 })).filter((n) => n.on);30 const edges: [number, number][] = [];31 nodes.forEach((a, i) => nodes.forEach((b, k) => { if (k > i && Math.hypot(a.x - b.x, a.y - b.y) < 84 && r() > 0.35) edges.push([i, k]); }));32 return (33 <div className={`sheet overflow-hidden ${className ?? ""}`} data-theme={theme} data-accent={accent} aria-hidden="true">34 <svg viewBox={`0 0 ${W} ${H}`} preserveAspectRatio="xMidYMid slice" className="block h-full w-full" style={{ background: "var(--sh-raised)" }}>35 {Array.from({ length: 7 }, (_, i) => <line key={i} x1="0" x2={W} y1={i * 30} y2={i * 30} stroke="var(--sh-grid)" strokeWidth="1" />)}36 {edges.map(([a, b], i) => <line key={i} x1={nodes[a].x} y1={nodes[a].y} x2={nodes[b].x} y2={nodes[b].y} stroke="var(--sh-accent)" strokeOpacity="0.45" strokeWidth="1.5" />)}37 {nodes.map((n, i) => <circle key={i} cx={n.x} cy={n.y} r={n.big ? 9 : 5.5} fill={n.big ? "var(--sh-accent)" : "var(--sh-surface)"} stroke="var(--sh-accent)" strokeWidth="1.75" />)}38 </svg>39 </div>40 );41}42