Something went wrong. Try again.
doll.systems community website
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304import { clamp } from '../lib/math';import type { PageKind } from '../lib/layouts';import { cssValue, labelGrid, pixelSize } from './pixel-grid';
type Point = { x: number; y: number };export type BitmapNode = Point & { kind: PageKind; radius: number; primary: boolean; focused: boolean; linked: boolean; related: boolean;};export type BitmapLink = { source: Point; target: Point; highlighted: boolean; related: boolean };const color = { ink: '#aaa', line: '#a2a2a2', core: '#c7c7c7', ring: '#828282', black: '#000000', accent: cssValue('--accent'), related: '#ac9ab8', red: '#df8888', cyan: '#94cbd4',};type LabelBitmap = { href: string; width: number; height: number; baseline: number };// Static artwork can survive a page swap; graph positions and focus cannot.const sprites = new Map<string, HTMLCanvasElement>();const sharedLabels = new Map<string, LabelBitmap>();const labelLimit = 256;export const graphLabel = { width: 176, padding: 2 };const outlineCoverage = 64;const spriteExtent = { node: 16, radar: 64 };let radar: HTMLCanvasElement | undefined;function radarSprite() { if (radar) return radar; radar = document.createElement('canvas'); const extent = spriteExtent.radar; radar.width = radar.height = 2 * extent + 1; const ctx = radar.getContext('2d')!; const ink = new Path2D(), outline = new Path2D(); for (let y = -extent; y <= extent; y++) for (let x = -extent; x <= extent; x++) { const radius = Math.hypot(x, y); const ring = (Math.abs(radius - 40) < 0.7 || Math.abs(radius - 60) < 0.6) && ((x + y) & 3) === 0; const axis = (x === 0 && Math.abs(y) > 7 && Math.abs(y) < 20) || (y === 0 && Math.abs(x) > 7 && Math.abs(x) < 20); if (!ring && !axis) continue; ink.rect(x + extent, y + extent, 1, 1); outline.rect(x + extent - 1, y + extent - 1, 3, 3); } ctx.fillStyle = color.black; ctx.fill(outline); ctx.fillStyle = color.accent; ctx.fill(ink); return radar;}
export function graphBitmap(canvas: HTMLCanvasElement, fontsReady: boolean) { const context = canvas.getContext('2d')!; // Reserve the whole reticle outside the graph's interaction bounds, on the same pixel grid. const padding = spriteExtent.radar + 1; // Keep the current graph's working set even if it exceeds the cross-page cache. const labels = new Map<string, LabelBitmap>(); let edges: { key: string; ink: Path2D; outline: Path2D }[] = []; const unit = (value: number) => Math.round(value / pixelSize); function nodeSprite(node: BitmapNode) { const key = `${node.kind}/${node.radius}/${node.primary}/${node.focused}/${node.linked}/${node.related}`; if (sprites.has(key)) return sprites.get(key)!; const sprite = document.createElement('canvas'); const extent = spriteExtent.node; sprite.width = sprite.height = 2 * extent + 1; const ctx = sprite.getContext('2d')!; ctx.imageSmoothingEnabled = false; const radius = unit(node.radius), lit = node.primary || node.focused || node.linked; const distance = (x: number, y: number) => node.kind === 'page' || node.kind === 'category' ? Math.max(Math.abs(x), Math.abs(y)) : node.kind === 'author' ? Math.abs(x) + Math.abs(y) : Math.hypot(x, y); const inkAt = (x: number, y: number) => { const d = distance(x, y); return d < radius ? 'core' : Math.abs(d - radius - 6) < 0.55 ? 'ring' : null; }; for (let y = -extent; y <= extent; y++) for (let x = -extent; x <= extent; x++) { const ink = inkAt(x, y); const outline = !ink && [-1, 0, 1].some((dy) => [-1, 0, 1].some((dx) => inkAt(x + dx, y + dy))); ctx.globalAlpha = 1; if (ink === 'core') ctx.fillStyle = lit ? color.accent : color.core; else if (ink === 'ring') ctx.fillStyle = node.primary ? color.accent : node.related ? color.related : color.ring; else if (outline) ctx.fillStyle = color.black; else if (distance(x, y) <= radius + 10 && lit) { ctx.fillStyle = color.accent; ctx.globalAlpha = node.focused ? 0.35 : 0.2; } else continue; ctx.fillRect(x + extent, y + extent, 1, 1); } sprites.set(key, sprite); return sprite; } function label( text: string, tone: 'normal' | 'related' | 'focused' = 'normal', maxWidth = graphLabel.width, ) { const density = window.devicePixelRatio || 1; const { font, height: lineHeight, baseline } = labelGrid(text); const key = JSON.stringify([text, font, tone, density, maxWidth]); const cached = labels.get(key); if (cached) return cached; const shared = fontsReady && sharedLabels.get(key); if (shared) { labels.set(key, shared); return shared; } const glyph = document.createElement('canvas'); const ctx = glyph.getContext('2d')!; ctx.font = font; ctx.fontKerning = 'none'; // Keep the full page title, wrapping at words (or characters for long/Japanese text). const lines: string[] = []; let line = ''; for (const char of text) { const next = line + char; if (ctx.measureText(next).width <= maxWidth || !line) { line = next; continue; } const boundary = next.lastIndexOf(' '); lines.push(boundary > 0 ? next.slice(0, boundary) : line); line = boundary > 0 ? next.slice(boundary + 1) : char; } if (line) lines.push(line); const height = lineHeight * lines.length; const width = Math.ceil(Math.max(0, ...lines.map((line) => ctx.measureText(line).width))) + 2 * graphLabel.padding; glyph.width = Math.ceil(width * density); glyph.height = Math.ceil((height + 2 * graphLabel.padding) * density); ctx.scale(density, density); ctx.font = font; ctx.fontKerning = 'none'; ctx.fillStyle = tone === 'focused' ? color.accent : tone === 'related' ? color.related : color.ink; lines.forEach((line, index) => ctx.fillText(line, graphLabel.padding, baseline + graphLabel.padding + index * lineHeight), ); const ink = ctx.getImageData(0, 0, glyph.width, glyph.height); const outline = document.createElement('canvas'); outline.width = glyph.width; outline.height = glyph.height; const halo = outline.getContext('2d')!; // Derive the outline from the exact glyph image we display. Only the black // border snaps to CSS pixels; the letter itself retains native-resolution ink. for (let y = 1; y < height + 3; y++) for (let x = 1; x < width - 1; x++) { let coverage = 0, samples = 0; for (let py = Math.round(y * density); py < Math.round((y + 1) * density); py++) { for (let px = Math.round(x * density); px < Math.round((x + 1) * density); px++) { coverage += ink.data[(py * glyph.width + px) * 4 + 3]; samples++; } } if (coverage < samples * outlineCoverage) continue; const left = Math.round((x - 1) * density), top = Math.round((y - 1) * density); halo.fillRect( left, top, Math.round((x + 2) * density) - left, Math.round((y + 2) * density) - top, ); } halo.drawImage(glyph, 0, 0); const sprite = { href: outline.toDataURL(), width, height: height + 2 * graphLabel.padding, baseline: baseline + graphLabel.padding, }; labels.set(key, sprite); if (fontsReady) { sharedLabels.set(key, sprite); if (sharedLabels.size > labelLimit) sharedLabels.delete(sharedLabels.keys().next().value!); } return sprite; } function edgePath(a: Point, b: Point, index: number) { let x = unit(a.x), y = unit(a.y); const endX = unit(b.x), endY = unit(b.y); const key = `${x},${y},${endX},${endY}`; if (edges[index]?.key === key) return edges[index]; const ink = new Path2D(), outline = new Path2D(); const dx = Math.abs(endX - x), dy = -Math.abs(endY - y); const sx = x < endX ? 1 : -1, sy = y < endY ? 1 : -1; let error = dx + dy; for (;;) { ink.rect(x, y, 1, 1); outline.rect(x - 1, y - 1, 3, 3); if (x === endX && y === endY) break; const twice = error * 2; if (twice >= dy) { error += dy; x += sx; } if (twice <= dx) { error += dx; y += sy; } } return (edges[index] = { key, ink, outline }); } return { resize(width: number, height: number) { canvas.width = Math.ceil(width / pixelSize) + 2 * padding; canvas.height = Math.ceil(height / pixelSize) + 2 * padding; canvas.style.left = canvas.style.top = `${-padding * pixelSize}px`; canvas.style.width = `${canvas.width * pixelSize}px`; canvas.style.height = `${canvas.height * pixelSize}px`; context.translate(padding, padding); context.imageSmoothingEnabled = false; edges = []; }, label, refreshFonts() { fontsReady = true; labels.clear(); }, draw(nodes: BitmapNode[], links: BitmapLink[], focus?: Point) { context.clearRect(-padding, -padding, canvas.width, canvas.height); // Rasterize each edge once, then reuse its path as highlights and the pointer move. links.forEach((edge, index) => edgePath(edge.source, edge.target, index)); context.fillStyle = color.black; context.globalAlpha = 1; for (const edge of edges) context.fill(edge.outline); links.forEach((edge, index) => { // Tiny, grid-aligned color offsets disturb nearby edges without filtering the labels. if (focus) { const dx = edge.target.x - edge.source.x, dy = edge.target.y - edge.source.y; const t = clamp( ((focus.x - edge.source.x) * dx + (focus.y - edge.source.y) * dy) / (dx * dx + dy * dy || 1), ); const distance = Math.hypot( edge.source.x + t * dx - focus.x, edge.source.y + t * dy - focus.y, ); const intensity = Math.exp(-(((distance - 40) / 48) ** 2)) * 0.2; if (intensity > 0.01) { context.save(); context.globalAlpha = intensity; context.fillStyle = color.red; context.translate(-1, 0); context.fill(edges[index].ink); context.fillStyle = color.cyan; context.translate(2, 0); context.fill(edges[index].ink); context.restore(); } } context.fillStyle = edge.highlighted ? color.accent : edge.related ? color.related : color.line; context.globalAlpha = edge.highlighted ? 1 : edge.related ? 0.78 : 0.6; context.fill(edges[index].ink); }); context.globalAlpha = 1; for (const node of nodes) { context.drawImage( nodeSprite(node), unit(node.x) - spriteExtent.node, unit(node.y) - spriteExtent.node, ); } if (focus) { context.globalAlpha = 0.32; context.drawImage( radarSprite(), unit(focus.x) - spriteExtent.radar, unit(focus.y) - spriteExtent.radar, ); } context.globalAlpha = 1; }, };}