// svg.ts, part of Phase Portrait, the generative artwork on jpdonie.com. // © 2026 Jean Philipp Donie. Written with Claude (Anthropic); see https://jpdonie.com/lab#process import { lineInk, simplify, type Portrait } from './core'; export interface SvgStyle { /** Streamline ink. */ ink: string; /** Isophase contour ink. */ contourInk: string; /** Paper colour; null leaves the background transparent. */ paper: string | null; /** Base stroke width for streamlines (design px). */ lineWidth: number; /** Stroke width for contours (design px). */ contourWidth: number; /** Opacity of the contour layer. */ contourOpacity: number; /** Dash pattern for contours ('' for solid). */ contourDash: string; /** Simplification tolerance (design px). */ tolerance: number; /** Decimal places in path data. */ precision: number; /** Mark vortex cores with small rings (and anti-vortices with ticks). */ showDefects: boolean; defectInk: string; /** Radius and stroke width of the vortex rings (design px). */ defectRadius: number; defectWidth: number; /** Accessible title and description. */ title: string; desc: string; } export const DEFAULT_SVG_STYLE: SvgStyle = { ink: '#1b1d24', contourInk: '#2b45b8', paper: null, lineWidth: 0.9, contourWidth: 1.5, contourOpacity: 0.7, contourDash: '0.01 4.4', tolerance: 0.22, precision: 1, showDefects: false, defectInk: '#2b45b8', defectRadius: 6, defectWidth: 1.6, title: 'Phase Portrait', desc: 'A generative drawing of a lattice of coupled oscillators.', }; const WEIGHT_CLASSES = 5; /** Ink class for a line, from its mean local agreement (0 = most unresolved). */ export function inkClass(order: number): number { const unrest = 1 - Math.min(1, Math.max(0, order)); return Math.min(WEIGHT_CLASSES - 1, Math.floor(Math.sqrt(unrest) * WEIGHT_CLASSES)); } /** Representative agreement for an ink class (inverse of inkClass at the class centre). */ export function classOrder(cls: number): number { const u = (cls + 0.5) / WEIGHT_CLASSES; return 1 - u * u; } function escapeXml(s: string): string { return s.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); } /** Compact path data: absolute move, then relative line segments. */ export function pathData(pts: number[], precision: number): string { if (pts.length < 4) return ''; const f = 10 ** precision; const q = (v: number): number => Math.round(v * f); let x = q(pts[0]!); let y = q(pts[1]!); const fmt = (v: number): string => { const s = (v / f).toFixed(precision).replace(/\.?0+$/, ''); return s === '-0' ? '0' : s; }; let d = `M${fmt(x)} ${fmt(y)}l`; let first = true; for (let k = 2; k < pts.length; k += 2) { const nx = q(pts[k]!); const ny = q(pts[k + 1]!); const dx = nx - x; const dy = ny - y; if (dx === 0 && dy === 0) continue; const sx = fmt(dx); const sy = fmt(dy); d += (first || sx.startsWith('-') ? '' : ' ') + sx + (sy.startsWith('-') ? '' : ' ') + sy; first = false; x = nx; y = ny; } // A move with no segments would be invalid path data and, because paths are // concatenated, would stop the renderer from drawing everything after it. return first ? '' : d; } export function portraitToSVG(portrait: Portrait, style: Partial = {}): string { const s: SvgStyle = { ...DEFAULT_SVG_STYLE, ...style }; const { width: W, height: H } = portrait.params; const parts: string[] = []; parts.push( ``, `${escapeXml(s.title)}`, `${escapeXml(s.desc)}`, ); if (s.paper) parts.push(``); if (portrait.contours.length) { const d = portrait.contours.map((c) => pathData(simplify(c, s.tolerance), s.precision)).join(''); parts.push( ``, ); } // Group streamlines into a few ink classes (by local agreement) to keep the file small. const buckets: string[][] = Array.from({ length: WEIGHT_CLASSES }, () => []); for (const line of portrait.lines) { const cls = inkClass(line.order); const d = pathData(simplify(line.points, s.tolerance), s.precision); if (d) buckets[cls]!.push(d); } parts.push(``); buckets.forEach((paths, cls) => { if (!paths.length) return; const ink = lineInk(classOrder(cls), s.lineWidth); parts.push( ``, ); }); parts.push(''); if (s.showDefects && portrait.defects.length) { const rings: string[] = []; for (const d of portrait.defects) { if (d.charge === 1) rings.push(``); } parts.push( `${rings.join('')}`, ); } parts.push(''); return parts.join(''); }