// canvas.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 /** * Canvas renderers for the Lab: the finished portrait (the same drawing as * the SVG plates) and the "iron filings" view of the lattice while it settles. */ import { dcos, dsin, lineInk, TAU } from './core'; import { classOrder, DEFAULT_SVG_STYLE, inkClass, type SvgStyle } from './svg'; export interface PortraitData { width: number; height: number; lines: { points: Float32Array; order: number }[]; contours: Float32Array[]; defects: { x: number; y: number; charge: 1 | -1 }[]; } export interface LatticeFrame { nx: number; ny: number; cx: number; cy: number; x: Float32Array; y: Float32Array; order: Float32Array; time: number; globalOrder: number; } function polyline(ctx: CanvasRenderingContext2D, pts: Float32Array): void { if (pts.length < 4) return; ctx.moveTo(pts[0]!, pts[1]!); for (let k = 2; k + 1 < pts.length; k += 2) ctx.lineTo(pts[k]!, pts[k + 1]!); } /** Draw a finished portrait. `scale` maps design px to canvas px. */ export function drawPortrait( ctx: CanvasRenderingContext2D, data: PortraitData, scale: number, paper: string, style: SvgStyle = DEFAULT_SVG_STYLE, ): void { ctx.save(); ctx.setTransform(scale, 0, 0, scale, 0, 0); ctx.globalAlpha = 1; ctx.fillStyle = paper; ctx.fillRect(0, 0, data.width, data.height); ctx.lineCap = 'round'; ctx.lineJoin = 'round'; if (data.contours.length) { ctx.strokeStyle = style.contourInk; ctx.globalAlpha = style.contourOpacity; ctx.lineWidth = style.contourWidth; ctx.setLineDash(style.contourDash ? style.contourDash.split(/[\s,]+/).map(Number) : []); ctx.beginPath(); for (const c of data.contours) polyline(ctx, c); ctx.stroke(); ctx.setLineDash([]); } const buckets: Float32Array[][] = [[], [], [], [], []]; for (const line of data.lines) buckets[inkClass(line.order)]!.push(line.points); ctx.strokeStyle = style.ink; buckets.forEach((lines, cls) => { if (!lines.length) return; const ink = lineInk(classOrder(cls), style.lineWidth); ctx.globalAlpha = ink.opacity; ctx.lineWidth = ink.width; ctx.beginPath(); for (const pts of lines) polyline(ctx, pts); ctx.stroke(); }); ctx.globalAlpha = 1; ctx.strokeStyle = style.defectInk; ctx.lineWidth = style.defectWidth; ctx.beginPath(); for (const d of data.defects) { if (d.charge !== 1) continue; ctx.moveTo(d.x + style.defectRadius, d.y); ctx.arc(d.x, d.y, style.defectRadius, 0, TAU); } ctx.stroke(); ctx.restore(); } /** * Draw the lattice as short strokes, one per oscillator, pointing along its * (rotated) phase. Strokes are darker where neighbours still disagree. */ export function drawFilings( ctx: CanvasRenderingContext2D, frame: LatticeFrame, reading: number, scale: number, paper: string, ink: string, ): void { const { nx, ny, cx, cy, x, y, order } = frame; const c = dcos(reading * TAU); const s = dsin(reading * TAU); const half = 0.42 * Math.min(cx, cy); ctx.save(); ctx.setTransform(scale, 0, 0, scale, 0, 0); ctx.globalAlpha = 1; ctx.fillStyle = paper; ctx.fillRect(0, 0, nx * cx, ny * cy); ctx.strokeStyle = ink; ctx.lineCap = 'round'; const classes = 4; for (let cls = 0; cls < classes; cls++) { ctx.globalAlpha = 0.28 + 0.6 * ((cls + 0.5) / classes); ctx.lineWidth = 0.8 + 0.5 * (cls / classes); ctx.beginPath(); for (let j = 0; j < ny; j++) { for (let i = 0; i < nx; i++) { const k = j * nx + i; const unrest = Math.sqrt(Math.max(0, 1 - order[k]!)); if (Math.min(classes - 1, Math.floor(unrest * classes)) !== cls) continue; const ux = x[k]! * c - y[k]! * s; const uy = x[k]! * s + y[k]! * c; const px = (i + 0.5) * cx; const py = (j + 0.5) * cy; ctx.moveTo(px - ux * half, py - uy * half); ctx.lineTo(px + ux * half, py + uy * half); } } ctx.stroke(); } ctx.restore(); }