{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "plotter",
  "title": "Plotter",
  "description": "Seeded line-drawing generators (flow fields, contours, globes, tunnels, rosettes, harmonographs, spirals, Lissajous knots, ridgelines, routes, hatching, fingerprints, starbursts, gauges, moirés, logo marks and sketched rules) and scrollWindow(), the trigger that finishes a drawing as it reaches the middle of the screen. Every scrollfx block draws with it.",
  "files": [
    {
      "path": "lib/plotter.ts",
      "content": "/**\n * plotter — line drawings the way a pen plotter makes them: flow fields,\n * contour maps, globes, tunnels, rosettes and hatching. Every generator is\n * seeded, so a seed gives the same drawing on every visit (engines may round\n * Math.sin differently and differ by a stroke or two), and returns plain SVG path\n * strings for svg-scroll-draw to ink in.\n *\n * Part of scrollfx (MIT): https://github.com/DhruvilChauahan0210/scrollfx\n */\n\nexport interface Stroke {\n  d: string;\n  /** Which pen draws it: 0 is the ink, 1 the accent. */\n  pen: 0 | 1;\n}\n\ntype Pt = [number, number];\n\nconst f = (v: number) => v.toFixed(1);\nconst toPath = (pts: Pt[]) => 'M' + pts.map(([x, y]) => `${f(x)} ${f(y)}`).join('L');\n\n/** A small, fast seeded random generator. */\nexport function random(seed: number) {\n  return () => {\n    seed |= 0;\n    seed = (seed + 0x6d2b79f5) | 0;\n    let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);\n    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;\n    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n  };\n}\n\n/** A seed from any string, such as a version number. */\nexport function seedOf(text: string) {\n  let h = 2166136261;\n  for (let i = 0; i < text.length; i++) h = Math.imul(h ^ text.charCodeAt(i), 16777619);\n  return h >>> 0;\n}\n\n/** Classic 2D Perlin noise over a seeded permutation, about -0.7 to 0.7. */\nexport function noise2(seed: number) {\n  const rand = random(seed);\n  const p = Array.from({ length: 256 }, (_, i) => i);\n  for (let i = 255; i > 0; i--) {\n    const j = Math.floor(rand() * (i + 1));\n    [p[i], p[j]] = [p[j], p[i]];\n  }\n  const perm = [...p, ...p];\n  const fade = (t: number) => t * t * t * (t * (t * 6 - 15) + 10);\n  const grad = (h: number, x: number, y: number) => ((h & 1) ? -x : x) + ((h & 2) ? -y : y);\n  return (x: number, y: number) => {\n    const X = Math.floor(x) & 255;\n    const Y = Math.floor(y) & 255;\n    x -= Math.floor(x);\n    y -= Math.floor(y);\n    const u = fade(x);\n    const v = fade(y);\n    const a = perm[X] + Y;\n    const b = perm[X + 1] + Y;\n    const l1 = grad(perm[a], x, y) + u * (grad(perm[b], x - 1, y) - grad(perm[a], x, y));\n    const l2 = grad(perm[a + 1], x, y - 1) + u * (grad(perm[b + 1], x - 1, y - 1) - grad(perm[a + 1], x, y - 1));\n    return l1 + v * (l2 - l1);\n  };\n}\n\n/**\n * A round flow field in a 1000 × 1000 box: evenly spaced streamlines that\n * never touch, ordered top to bottom as a plotter travels. The accent pen\n * takes the lines that pass through `accent` (a circle: x, y, radius).\n */\nexport function flowField({ seed = 417, spacing = 7, radius = 455, accent = [640, 610, 135] as [number, number, number] } = {}): Stroke[] {\n  const rand = random(seed);\n  const noise = noise2(seed);\n  const C = 500;\n  const step = 1.6;\n  const test = spacing * 0.5;\n\n  const angle = (x: number, y: number) => {\n    const dx = x - C;\n    const dy = y - C;\n    const r = Math.hypot(dx, dy) / radius;\n    // Noise for the drift, and a slow vortex round the centre for structure.\n    const drift = noise(x * 0.0021, y * 0.0021) * Math.PI * 2.4;\n    const swirl = Math.atan2(dy, dx) + Math.PI / 2;\n    const w = 0.55 * (1 - r) * (1 - r);\n    // Blend directions, not angles: angles wrap at ±π and leave a seam.\n    return Math.atan2(Math.sin(drift) * (1 - w) + Math.sin(swirl) * w, Math.cos(drift) * (1 - w) + Math.cos(swirl) * w);\n  };\n\n  const cell = spacing;\n  const cols = Math.ceil(1000 / cell);\n  const grid = new Map<number, Pt[]>();\n  const inside = (x: number, y: number) => Math.hypot(x - C, y - C) < radius;\n  const free = (x: number, y: number, d: number) => {\n    const cx = Math.floor(x / cell);\n    const cy = Math.floor(y / cell);\n    for (let j = -1; j <= 1; j++) {\n      for (let i = -1; i <= 1; i++) {\n        const pts = grid.get((cy + j) * cols + (cx + i));\n        if (!pts) continue;\n        for (const [px, py] of pts) if ((px - x) ** 2 + (py - y) ** 2 < d * d) return false;\n      }\n    }\n    return true;\n  };\n  const trace = (sx: number, sy: number): Pt[] => {\n    const half = (dir: 1 | -1) => {\n      const out: Pt[] = [];\n      let x = sx;\n      let y = sy;\n      for (let k = 0; k < 700; k++) {\n        const a = angle(x, y);\n        x += Math.cos(a) * step * dir;\n        y += Math.sin(a) * step * dir;\n        if (!inside(x, y) || !free(x, y, test)) break;\n        out.push([x, y]);\n      }\n      return out;\n    };\n    return [...half(-1).reverse(), [sx, sy], ...half(1)];\n  };\n\n  const lines: { d: string; y: number; pen: 0 | 1 }[] = [];\n  const queue: Pt[] = [[C, C]];\n  const fallback: Pt[] = [];\n  for (let y = C - radius; y < C + radius; y += spacing * 3) {\n    for (let x = C - radius; x < C + radius; x += spacing * 3) fallback.push([x + rand() * spacing, y + rand() * spacing]);\n  }\n  for (let i = fallback.length - 1; i > 0; i--) {\n    const j = Math.floor(rand() * (i + 1));\n    [fallback[i], fallback[j]] = [fallback[j], fallback[i]];\n  }\n\n  while (queue.length || fallback.length) {\n    const [sx, sy] = (queue.pop() ?? fallback.pop())!;\n    if (!inside(sx, sy) || !free(sx, sy, spacing)) continue;\n    const line = trace(sx, sy);\n    if (line.length < 14) continue;\n    for (const [x, y] of line) {\n      const k = Math.floor(y / cell) * cols + Math.floor(x / cell);\n      const bucket = grid.get(k);\n      if (bucket) bucket.push([x, y]);\n      else grid.set(k, [[x, y]]);\n    }\n    // New seeds one spacing away on either side, along the line.\n    for (let i = 0; i < line.length; i += 6) {\n      const [x, y] = line[i];\n      const a = angle(x, y) + Math.PI / 2;\n      queue.push([x + Math.cos(a) * spacing, y + Math.sin(a) * spacing]);\n      queue.push([x - Math.cos(a) * spacing, y - Math.sin(a) * spacing]);\n    }\n    const [mx, my] = line[Math.floor(line.length / 2)];\n    lines.push({\n      d: toPath(line.filter((_, i) => i % 3 === 0 || i === line.length - 1)),\n      y: Math.min(line[0][1], line[line.length - 1][1]),\n      pen: Math.hypot(mx - accent[0], my - accent[1]) < accent[2] ? 1 : 0,\n    });\n  }\n  return lines.sort((a, b) => a.y - b.y).map(({ d, pen }) => ({ d, pen }));\n}\n\n/**\n * A topographic map in a w × h box: the contour lines of a noisy island,\n * traced with marching squares and joined into whole lines. Every fifth\n * level is an index contour, drawn by the accent pen.\n */\nexport function contours({ seed = 7, w = 400, h = 300, levels = 18, cell = 5 } = {}): Stroke[] {\n  const noise = noise2(seed);\n  const nx = Math.ceil(w / cell) + 1;\n  const ny = Math.ceil(h / cell) + 1;\n  const v: number[] = [];\n  for (let j = 0; j < ny; j++) {\n    for (let i = 0; i < nx; i++) {\n      const x = i * cell;\n      const y = j * cell;\n      const dx = (x - w * 0.5) / (w * 0.5);\n      const dy = (y - h * 0.5) / (h * 0.5);\n      const fall = 1 - Math.min(1, Math.hypot(dx, dy * 0.95));\n      const n = noise(x / 130, y / 130) + 0.5 * noise(x / 60 + 9, y / 60 + 9) + 0.25 * noise(x / 28 + 3, y / 28);\n      v.push(fall * 1.1 + n * 0.55);\n    }\n  }\n  const at = (i: number, j: number) => v[j * nx + i];\n  const out: Stroke[] = [];\n  for (let l = 1; l <= levels; l++) {\n    const t = l / (levels + 1) * 1.2;\n    // Segments for this level, then join them end to end.\n    const segs: [Pt, Pt][] = [];\n    const lerp = (a: number, b: number) => (t - a) / (b - a);\n    for (let j = 0; j < ny - 1; j++) {\n      for (let i = 0; i < nx - 1; i++) {\n        const a = at(i, j), b = at(i + 1, j), c = at(i + 1, j + 1), d = at(i, j + 1);\n        const x = i * cell, y = j * cell;\n        const e: Pt[] = [];\n        if ((a < t) !== (b < t)) e.push([x + lerp(a, b) * cell, y]);\n        if ((b < t) !== (c < t)) e.push([x + cell, y + lerp(b, c) * cell]);\n        if ((d < t) !== (c < t)) e.push([x + lerp(d, c) * cell, y + cell]);\n        if ((a < t) !== (d < t)) e.push([x, y + lerp(a, d) * cell]);\n        if (e.length === 2) segs.push([e[0], e[1]]);\n        else if (e.length === 4) { segs.push([e[0], e[1]]); segs.push([e[2], e[3]]); }\n      }\n    }\n    const key = ([x, y]: Pt) => `${Math.round(x * 10)},${Math.round(y * 10)}`;\n    const ends = new Map<string, number[]>();\n    segs.forEach(([p, q], i) => {\n      for (const k of [key(p), key(q)]) ends.set(k, [...(ends.get(k) ?? []), i]);\n    });\n    const used = new Set<number>();\n    for (let s = 0; s < segs.length; s++) {\n      if (used.has(s)) continue;\n      used.add(s);\n      const line: Pt[] = [segs[s][0], segs[s][1]];\n      for (const forward of [true, false]) {\n        for (;;) {\n          const tip = forward ? line[line.length - 1] : line[0];\n          const next = (ends.get(key(tip)) ?? []).find((i) => !used.has(i));\n          if (next === undefined) break;\n          used.add(next);\n          const [p, q] = segs[next];\n          const add = key(p) === key(tip) ? q : p;\n          if (forward) line.push(add);\n          else line.unshift(add);\n        }\n      }\n      if (line.length > 6) out.push({ d: toPath(line), pen: l % 5 === 0 ? 1 : 0 });\n    }\n  }\n  return out;\n}\n\n/** A wireframe globe in a w × h box: meridians and parallels, tilted, front half only. */\nexport function globe({ w = 400, h = 300, tilt = 0.4, turn = 0.35, step = 10 } = {}): Stroke[] {\n  const R = Math.min(w, h) * 0.44;\n  const cx = w / 2, cy = h / 2;\n  const project = (lat: number, lon: number): [number, number, number] => {\n    let x = Math.cos(lat) * Math.sin(lon + turn);\n    let y = Math.sin(lat);\n    let z = Math.cos(lat) * Math.cos(lon + turn);\n    [y, z] = [y * Math.cos(tilt) - z * Math.sin(tilt), y * Math.sin(tilt) + z * Math.cos(tilt)];\n    return [cx + x * R, cy - y * R, z];\n  };\n  const out: Stroke[] = [];\n  const trace = (fn: (t: number) => [number, number, number], n: number, pen: 0 | 1) => {\n    let run: Pt[] = [];\n    for (let k = 0; k <= n; k++) {\n      const [x, y, z] = fn(k / n);\n      if (z >= 0) run.push([x, y]);\n      else if (run.length) { if (run.length > 2) out.push({ d: toPath(run), pen }); run = []; }\n    }\n    if (run.length > 2) out.push({ d: toPath(run), pen });\n  };\n  const rad = Math.PI / 180;\n  for (let lon = 0; lon < 180; lon += step) {\n    trace((t) => project((t * 360 - 180) * rad, lon * rad), 120, 0);\n  }\n  for (let lat = -80; lat <= 80; lat += step) {\n    trace((t) => project(lat * rad, t * Math.PI * 2), 160, lat === 0 ? 1 : 0);\n  }\n  out.push({ d: toPath(Array.from({ length: 121 }, (_, k): Pt => [cx + R * Math.cos((k / 120) * Math.PI * 2), cy + R * Math.sin((k / 120) * Math.PI * 2)])), pen: 0 });\n  return out;\n}\n\n/** A tunnel in a w × h box: nested squares, each smaller and turned a little. */\nexport function tunnel({ w = 400, h = 300, count = 34, shrink = 0.92, twist = 4 } = {}): Stroke[] {\n  const out: Stroke[] = [];\n  let s = Math.min(w, h) * 0.86;\n  const cx = w / 2, cy = h / 2;\n  for (let i = 0; i < count; i++) {\n    const a = (i * twist * Math.PI) / 180;\n    // Drift the centre a little, as if the tunnel bends away.\n    const ox = cx + Math.sin(i * 0.12) * i * 0.9;\n    const oy = cy - i * 0.7;\n    const pts: Pt[] = [0, 1, 2, 3, 4].map((k) => {\n      const t = a + Math.PI / 4 + (k * Math.PI) / 2;\n      return [ox + (Math.cos(t) * s) / Math.SQRT2, oy + (Math.sin(t) * s) / Math.SQRT2];\n    });\n    out.push({ d: toPath(pts) + 'Z', pen: i === count - 1 ? 1 : 0 });\n    s *= shrink;\n  }\n  return out;\n}\n\n/** A rosette (a hypotrochoid) in a size × size box, shaped by the seed. */\nexport function rosette(seed: number, size = 100): Stroke[] {\n  const rand = random(seed);\n  const R = 5 + Math.floor(rand() * 5);\n  const r = 2 + Math.floor(rand() * (R - 2));\n  const d = r * (0.6 + rand() * 0.9);\n  const gcd = (a: number, b: number): number => (b ? gcd(b, a % b) : a);\n  const turns = r / gcd(R, r);\n  const scale = (size * 0.46) / (R - r + d);\n  const n = Math.round(turns * 140);\n  const pts: Pt[] = [];\n  for (let k = 0; k <= n; k++) {\n    const t = (k / n) * turns * Math.PI * 2;\n    pts.push([size / 2 + scale * ((R - r) * Math.cos(t) + d * Math.cos(((R - r) / r) * t)), size / 2 + scale * ((R - r) * Math.sin(t) - d * Math.sin(((R - r) / r) * t))]);\n  }\n  return [{ d: toPath(pts), pen: 0 }];\n}\n\n/** Diagonal hatching across a w × h box, one stroke per line, left to right. */\nexport function hatch({ w, h, gap = 4, angle = 60 }: { w: number; h: number; gap?: number; angle?: number }): Stroke[] {\n  const out: Stroke[] = [];\n  const run = h / Math.tan((angle * Math.PI) / 180);\n  for (let x = -run; x < w; x += gap) {\n    // Clip the line from (x, h) to (x + run, 0) to the box.\n    let x0 = x, y0 = h, x1 = x + run, y1 = 0;\n    if (x0 < 0) { y0 = h - ((0 - x0) / run) * h; x0 = 0; }\n    if (x1 > w) { y1 = h - ((w - x) / run) * h; x1 = w; }\n    if (y0 - y1 > 1) out.push({ d: `M${f(x0)} ${f(y0)}L${f(x1)} ${f(y1)}`, pen: 0 });\n  }\n  return out;\n}\n\n/** A smooth curve through points (Catmull-Rom as cubic Béziers). */\nexport function smooth(pts: Pt[]): string {\n  let d = `M${f(pts[0][0])} ${f(pts[0][1])}`;\n  for (let i = 0; i < pts.length - 1; i++) {\n    const a = pts[Math.max(0, i - 1)], b = pts[i], c = pts[i + 1], e = pts[Math.min(pts.length - 1, i + 2)];\n    d += `C${f(b[0] + (c[0] - a[0]) / 6)} ${f(b[1] + (c[1] - a[1]) / 6)} ${f(c[0] - (e[0] - b[0]) / 6)} ${f(c[1] - (e[1] - b[1]) / 6)} ${f(c[0])} ${f(c[1])}`;\n  }\n  return d;\n}\n\n/** A point on that curve's x, found by sampling (for hatching under it). */\nexport function curveY(pts: Pt[], x: number): number {\n  // Piecewise cubic Hermite with Catmull-Rom tangents, the same curve as smooth().\n  for (let i = 0; i < pts.length - 1; i++) {\n    const b = pts[i], c = pts[i + 1];\n    if (x < b[0] || x > c[0]) continue;\n    const a = pts[Math.max(0, i - 1)], e = pts[Math.min(pts.length - 1, i + 2)];\n    const p1: Pt = [b[0] + (c[0] - a[0]) / 6, b[1] + (c[1] - a[1]) / 6];\n    const p2: Pt = [c[0] - (e[0] - b[0]) / 6, c[1] - (e[1] - b[1]) / 6];\n    // Solve the Bézier's x for t by bisection, then take its y.\n    let lo = 0, hi = 1;\n    const bx = (t: number) => (1 - t) ** 3 * b[0] + 3 * (1 - t) ** 2 * t * p1[0] + 3 * (1 - t) * t * t * p2[0] + t ** 3 * c[0];\n    for (let k = 0; k < 24; k++) { const m = (lo + hi) / 2; if (bx(m) < x) lo = m; else hi = m; }\n    const t = (lo + hi) / 2;\n    return (1 - t) ** 3 * b[1] + 3 * (1 - t) ** 2 * t * p1[1] + 3 * (1 - t) * t * t * p2[1] + t ** 3 * c[1];\n  }\n  return pts[pts.length - 1][1];\n}\n\n/** A hand-drawn rectangle: the pen goes round twice, never quite on the same line. */\nexport function sketchRect({ w, h, seed = 1, wobble = 2.2, r = 18 }: { w: number; h: number; seed?: number; wobble?: number; r?: number }): Stroke[] {\n  const rand = random(seed);\n  const j = () => (rand() - 0.5) * wobble * 2;\n  const pass = (o: number): string => {\n    const k = r * 0.45;\n    const p = [\n      [r + j(), o + j()], [w - r + j(), o + j()],\n      [w - o + j(), r + j()], [w - o + j(), h - r + j()],\n      [w - r + j(), h - o + j()], [r + j(), h - o + j()],\n      [o + j(), h - r + j()], [o + j(), r + j()],\n    ].map(([x, y]) => [f(x), f(y)]);\n    return `M${p[0][0]} ${p[0][1]}L${p[1][0]} ${p[1][1]}C${f(w - r + k)} ${f(o)} ${f(w - o)} ${f(r - k)} ${p[2][0]} ${p[2][1]}L${p[3][0]} ${p[3][1]}C${f(w - o)} ${f(h - r + k)} ${f(w - r + k)} ${f(h - o)} ${p[4][0]} ${p[4][1]}L${p[5][0]} ${p[5][1]}C${f(r - k)} ${f(h - o)} ${f(o)} ${f(h - r + k)} ${p[6][0]} ${p[6][1]}L${p[7][0]} ${p[7][1]}C${f(o)} ${f(r - k)} ${f(r - k)} ${f(o)} ${f(r + 6)} ${f(o + 1)}`;\n  };\n  return [{ d: pass(1), pen: 0 }, { d: pass(3), pen: 1 }];\n}\n\n/**\n * A harmonograph in a size × size box: the trace of two damped pendulums, one\n * long line that spirals in as the swing dies away. Shaped by the seed.\n */\nexport function harmonograph(seed: number, size = 1000): Stroke[] {\n  const rand = random(seed);\n  // Near-whole frequency ratios give the slow, braided precession plotters are known for.\n  const f = [2 + rand() * 0.02, 3 + rand() * 0.02, 3 + rand() * 0.02, 2 + rand() * 0.02];\n  const p = [rand() * Math.PI, rand() * Math.PI, rand() * Math.PI, rand() * Math.PI];\n  const damp = 0.0042 + rand() * 0.0015;\n  const R = size * 0.23;\n  const pts: Pt[] = [];\n  // Fine steps keep the wide outer loops round; stopping once the swing is a\n  // fifth of its start keeps the centre from filling in solid.\n  for (let t = 0; ; t += 0.05) {\n    const e = Math.exp(-damp * t);\n    if (e < 0.2) break;\n    pts.push([\n      size / 2 + R * e * (Math.sin(f[0] * t + p[0]) + Math.sin(f[1] * t + p[1])),\n      size / 2 + R * e * (Math.sin(f[2] * t + p[2]) + Math.sin(f[3] * t + p[3])),\n    ]);\n  }\n  return [{ d: toPath(pts), pen: 0 }];\n}\n\n/**\n * A route through points, the way a pen wanders between them on a map: each\n * leg bows to one side by a seeded amount, so the line never runs straight.\n */\nexport function route(stops: Pt[], seed = 1, bow = 0.22): string {\n  const rand = random(seed);\n  let d = `M${f(stops[0][0])} ${f(stops[0][1])}`;\n  for (let i = 0; i < stops.length - 1; i++) {\n    const [ax, ay] = stops[i], [bx, by] = stops[i + 1];\n    const dx = bx - ax, dy = by - ay;\n    const len = Math.hypot(dx, dy) || 1;\n    // A perpendicular nudge for each control point, opposite signs for an S.\n    const nx = -dy / len, ny = dx / len;\n    const k1 = (rand() * 0.6 + 0.4) * bow * len * (i % 2 ? 1 : -1);\n    const k2 = (rand() * 0.6 + 0.4) * bow * len * (i % 2 ? -1 : 1);\n    d += `C${f(ax + dx / 3 + nx * k1)} ${f(ay + dy / 3 + ny * k1)} ${f(ax + (2 * dx) / 3 + nx * k2)} ${f(ay + (2 * dy) / 3 + ny * k2)} ${f(bx)} ${f(by)}`;\n  }\n  return d;\n}\n\n/**\n * The scroll window for a drawing that should finish exactly as it reaches the\n * middle of the screen: not before (it played while nobody looked), not after\n * (it is still drawing up by the header).\n *\n * In svg-scroll-draw each path draws across the whole trigger, and `stagger`\n * starts each one later than the last, so the last path ends `spread` ranges\n * after `trigger.end` (spread = stagger × (paths − 1)). Measuring both ends\n * from the element's centre makes the range a fixed share of the screen at\n * any element height, so the end can be solved for:\n *\n *   start: centre at `from`        finish: centre at `to`\n *   end = (to + spread × from) / (1 + spread)\n *\n * Pass the same spread you give the drawing: `stagger = spread / (paths - 1)`.\n */\nexport function scrollWindow(spread = 0, { from = 1.15, to = 0.5 } = {}) {\n  const end = (to + spread * from) / (1 + spread);\n  const pct = (v: number) => `${(v * 100).toFixed(1)}%`;\n  return { start: `center ${pct(from)}`, end: `center ${pct(end)}` };\n}\n\n/** The stagger that spreads `paths` over `spread` ranges (see scrollWindow). */\nexport const staggerFor = (paths: number, spread: number) => (paths > 1 ? spread / (paths - 1) : 0);\n\n/**\n * A spiral medallion in a size × size box: one line wound from the rim inward,\n * its radius breathing a little as a hand-drawn spiral does. Shaped by the\n * seed, so a name always gets the same mark.\n */\nexport function spiral(seed: number, size = 100, { turns = 9, inner = 0.42 } = {}): Stroke[] {\n  const noise = noise2(seed);\n  const c = size / 2;\n  const outer = size * 0.47;\n  const steps = turns * 90;\n  const pts: Pt[] = [];\n  for (let k = 0; k <= steps; k++) {\n    const t = k / steps;\n    const a = t * turns * Math.PI * 2 + (seed % 360) * (Math.PI / 180);\n    const r = (outer - (outer - outer * inner) * t) * (1 + 0.045 * noise(Math.cos(a) * 1.3 + t * 4, Math.sin(a) * 1.3));\n    pts.push([c + Math.cos(a) * r, c + Math.sin(a) * r]);\n  }\n  return [{ d: toPath(pts), pen: 0 }];\n}\n\n/**\n * A Lissajous knot in a w × h box: one closed figure drawn four times, each a\n * little smaller, the way a plotter builds weight by repeating a line. The\n * seed picks one of four ratios; a quarter-turn phase closes each of them (at\n * other phases some retrace themselves into an open curve).\n */\nexport function lissajous(seed: number, w = 400, h = 400): Stroke[] {\n  const shapes: [number, number, number][] = [[3, 2, Math.PI / 2], [5, 4, Math.PI / 2], [3, 4, Math.PI / 2], [5, 6, Math.PI / 2]];\n  const [a, b, delta] = shapes[seed % shapes.length];\n  const n = 1400;\n  return [0, 1, 2, 3].map((k) => {\n    const s = 0.44 * Math.pow(0.9, k);\n    const pts: Pt[] = [];\n    for (let i = 0; i <= n; i++) {\n      const t = (i / n) * Math.PI * 2;\n      pts.push([w / 2 + Math.sin(a * t + delta) * w * s, h / 2 + Math.sin(b * t) * h * s]);\n    }\n    return { d: toPath(pts), pen: k === 0 ? 1 : 0 } as Stroke;\n  });\n}\n\n/**\n * Ridgelines in a w × h box: evenly spaced rows, flat at the edges and rising\n * toward the middle in peaks of their own, the nearer rows hiding the ones\n * behind, as a plotter draws a landscape with hidden lines removed. The accent\n * pen takes the nearest ridge. Strokes come back to front, as they are read.\n */\nexport function ridgelines({ seed = 9, w = 1200, h = 260, rows = 18, step = 5 } = {}): Stroke[] {\n  const noise = noise2(seed);\n  const rand = random(seed);\n  const cols = Math.ceil(w / step) + 1;\n  // Each row's own peaks: two or three bumps inside the middle half.\n  const peaks = Array.from({ length: rows }, () =>\n    Array.from({ length: 2 + Math.floor(rand() * 2) }, () => ({ at: w * (0.3 + rand() * 0.4), width: w * (0.04 + rand() * 0.07), height: 0.4 + rand() * 0.6 })),\n  );\n  const horizon = new Array<number>(cols).fill(Infinity);\n  const out: Stroke[] = [];\n  const amp = h * 0.34;\n  // Front to back, keeping the highest point drawn so far in each column: a\n  // ridge behind is drawn only where it rises above everything in front.\n  for (let r = rows - 1; r >= 0; r--) {\n    const base = h * 0.5 + (r / (rows - 1)) * h * 0.46;\n    let run: Pt[] = [];\n    const flush = () => { if (run.length > 1) out.push({ d: toPath(run), pen: r === rows - 1 ? 1 : 0 }); run = []; };\n    for (let i = 0; i < cols; i++) {\n      const x = i * step;\n      const middle = Math.exp(-(((x - w / 2) / (w * 0.24)) ** 4));\n      // The tallest bump, not their sum: overlapping bumps would add up past the box.\n      const bumps = Math.max(...peaks[r].map((p) => p.height * Math.exp(-(((x - p.at) / p.width) ** 2))));\n      const y = base - amp * middle * (bumps + 0.15 * (noise(x / 60, r * 0.8) + 0.5)) - 2.5 * noise(x / 25, r * 3.1);\n      if (y < horizon[i] - 0.5) {\n        run.push([x, y]);\n        horizon[i] = y;\n      } else flush();\n    }\n    flush();\n  }\n  return out.reverse();\n}\n\n/**\n * A fingerprint in a size × size box: open rings around a slightly off-centre\n * core, pushed about by noise as ridges are. The push is read from one field at\n * each point's place, so neighbouring rings bend together and never cross.\n * Shaped by the seed, so a name always gets the same print. Inside out.\n */\nexport function fingerprint(seed: number, size = 100, { rings = 13 } = {}): Stroke[] {\n  const noise = noise2(seed);\n  const rand = random(seed);\n  const cx = size * (0.47 + rand() * 0.06);\n  const cy = size * (0.45 + rand() * 0.08);\n  const gap = (size * 0.43) / rings;\n  const out: Stroke[] = [];\n  for (let k = 1; k <= rings; k++) {\n    const r = k * gap;\n    // Each ridge breaks off somewhere, as real ones do; the outer ones more.\n    const start = rand() * Math.PI * 2;\n    const sweep = Math.PI * 2 * (k < 3 ? 0.97 : 0.78 + rand() * 0.18);\n    const n = Math.max(24, Math.round(r * 2.4));\n    const pts: Pt[] = [];\n    for (let i = 0; i <= n; i++) {\n      const a = start + (i / n) * sweep;\n      const x = cx + Math.cos(a) * r * 1.08;\n      const y = cy + Math.sin(a) * r * 0.92;\n      // Displace along the radius by the field at this place: under one gap, so no crossings.\n      const push = gap * 0.85 * noise(x / (size * 0.22), y / (size * 0.22));\n      pts.push([x + Math.cos(a) * push, y + Math.sin(a) * push]);\n    }\n    out.push({ d: toPath(pts), pen: 0 });\n  }\n  return out;\n}\n\n/**\n * A starburst in a size × size box: rays from a small ring outward, their\n * lengths rising and falling with noise round the circle, drawn clockwise from\n * the top as a plotter sweeps. Every `every`-th ray is the accent pen's.\n */\nexport function starburst(seed: number, size = 400, { rays = 144, every = 12 } = {}): Stroke[] {\n  const noise = noise2(seed);\n  const c = size / 2;\n  const inner = size * 0.08;\n  const outer = size * 0.48;\n  const out: Stroke[] = [];\n  for (let i = 0; i < rays; i++) {\n    const a = -Math.PI / 2 + (i / rays) * Math.PI * 2;\n    const swell = 0.5 + 0.5 * Math.min(1, Math.max(0, (noise(Math.cos(a) * 1.8, Math.sin(a) * 1.8) + 0.7) / 1.4));\n    const len = inner + (outer - inner) * (i % every === 0 ? 1 : swell * 0.92);\n    out.push({\n      d: `M${f(c + Math.cos(a) * inner)} ${f(c + Math.sin(a) * inner)}L${f(c + Math.cos(a) * len)} ${f(c + Math.sin(a) * len)}`,\n      pen: i % every === 0 ? 1 : 0,\n    });\n  }\n  return out;\n}\n\nconst ring = (cx: number, cy: number, r: number, n = 64, from = 0, to = Math.PI * 2): Pt[] =>\n  Array.from({ length: n + 1 }, (_, k): Pt => {\n    const a = from + ((to - from) * k) / n;\n    return [cx + Math.cos(a) * r, cy + Math.sin(a) * r];\n  });\n\n/**\n * A gauge in a size × size box, the way a plotter draws an instrument face:\n * ticks round three quarters of a circle, clockwise from the lower left, every\n * fifth one long; a band of three arcs in the accent pen out to `share` (0 to\n * 1) of the way round; and a needle pointing at it. The seed nudges each tick,\n * so no two faces are quite alike.\n */\nexport function gauge({ seed = 1, size = 100, share = 0.7, ticks = 41 }: { seed?: number; size?: number; share?: number; ticks?: number } = {}): Stroke[] {\n  const rand = random(seed);\n  const c = size / 2;\n  const R = size * 0.46;\n  const a0 = Math.PI * 0.75;\n  const sweep = Math.PI * 1.5;\n  const s = Math.max(0, Math.min(1, share));\n  const out: Stroke[] = [];\n  for (let i = 0; i < ticks; i++) {\n    const a = a0 + (sweep * i) / (ticks - 1) + (rand() - 0.5) * 0.012;\n    const len = size * (i % 5 === 0 ? 0.12 : 0.06) * (0.92 + rand() * 0.16);\n    out.push({ d: toPath([[c + Math.cos(a) * R, c + Math.sin(a) * R], [c + Math.cos(a) * (R - len), c + Math.sin(a) * (R - len)]]), pen: 0 });\n  }\n  if (s > 0) {\n    for (const k of [0.7, 0.66, 0.62]) out.push({ d: toPath(ring(c, c, R * k, Math.max(4, Math.round(60 * s)), a0, a0 + sweep * s)), pen: 1 });\n  }\n  const an = a0 + sweep * s;\n  out.push({ d: toPath([[c, c], [c + Math.cos(an) * R * 0.5, c + Math.sin(an) * R * 0.5]]), pen: 0 });\n  out.push({ d: toPath(ring(c, c, size * 0.03, 16)), pen: 0 });\n  return out;\n}\n\n/** The kinds of mark logoMark draws. */\nexport type MarkKind = 'rings' | 'disc' | 'delta' | 'wave' | 'grid' | 'burst';\n\n/**\n * A simple generic mark in a size × size box, for a placeholder logo: rings,\n * a hatched disc, nested triangles, waves, a ruled square or a burst of rays.\n * The seed nudges every line a little, as a pen would.\n */\nexport function logoMark(kind: MarkKind, size = 40, seed = 1): Stroke[] {\n  const rand = random(seed);\n  const j = () => (rand() - 0.5) * size * 0.012;\n  const c = size / 2;\n  const R = size * 0.45;\n  const line = (pts: Pt[]): Stroke => ({ d: toPath(pts.map(([x, y]): Pt => [x + j(), y + j()])), pen: 0 });\n  switch (kind) {\n    case 'rings':\n      return [1, 0.74, 0.48, 0.22].map((k) => ({ d: toPath(ring(c, c, R * k, 48)), pen: 0 }));\n    case 'disc': {\n      const out: Stroke[] = [{ d: toPath(ring(c, c, R, 56)), pen: 0 }];\n      // Hatching at 45°, clipped to the circle.\n      const q = Math.SQRT1_2;\n      for (let o = -R + size * 0.08; o < R; o += size * 0.08) {\n        const h = Math.sqrt(R * R - o * o) * 0.94;\n        out.push(line([[c + (o - h) * q, c + (o + h) * q], [c + (o + h) * q, c + (o - h) * q]]));\n      }\n      return out;\n    }\n    case 'delta':\n      return [1, 0.66, 0.33].map((k) =>\n        line([0, 1, 2, 3].map((i): Pt => {\n          const a = -Math.PI / 2 + (i * Math.PI * 2) / 3;\n          return [c + Math.cos(a) * R * k, c + size * 0.06 + Math.sin(a) * R * k];\n        })),\n      );\n    case 'wave':\n      return [0.3, 0.43, 0.56, 0.69].map((y) => line(Array.from({ length: 25 }, (_, i): Pt => [size * 0.08 + (i / 24) * size * 0.84, size * y + Math.sin((i / 24) * Math.PI * 3) * size * 0.08])));\n    case 'grid': {\n      const a = size * 0.1, b = size * 0.9;\n      const out: Stroke[] = [line([[a, a], [b, a], [b, b], [a, b], [a, a]])];\n      for (const t of [0.3, 0.5, 0.7]) {\n        const p = a + (b - a) * t;\n        out.push(line([[p, a], [p, b]]));\n        out.push(line([[a, p], [b, p]]));\n      }\n      return out;\n    }\n    case 'burst':\n      return Array.from({ length: 12 }, (_, i) => {\n        const a = -Math.PI / 2 + (i * Math.PI * 2) / 12;\n        const r0 = R * 0.28, r1 = R * (i % 3 === 0 ? 1 : 0.72);\n        return line([[c + Math.cos(a) * r0, c + Math.sin(a) * r0], [c + Math.cos(a) * r1, c + Math.sin(a) * r1]]);\n      });\n  }\n}\n\n/**\n * A rule a pen draws across a w × h box, twice, never quite on the same line:\n * the first pass in ink, the second in the accent pen.\n */\nexport function sketchLine({ w, h = 12, seed = 1, wobble = 1 }: { w: number; h?: number; seed?: number; wobble?: number }): Stroke[] {\n  const rand = random(seed);\n  const pass = (dy: number, pen: 0 | 1): Stroke => {\n    const n = 16;\n    return { d: toPath(Array.from({ length: n + 1 }, (_, i): Pt => [1 + ((w - 2) * i) / n, h / 2 + dy + (rand() - 0.5) * wobble * 2])), pen };\n  };\n  return [pass(-1.2, 0), pass(1.2, 1)];\n}\n\n/**\n * A moiré in a size × size box: two sets of concentric rings, their centres a\n * little apart, so where they cross the paper fills with interference\n * fringes. The rings are drawn from the middle outward, one from each set in\n * turn; the accent pen takes one ring of the second set.\n */\nexport function moire({ seed = 5, size = 400, rings = 18 }: { seed?: number; size?: number; rings?: number } = {}): Stroke[] {\n  const rand = random(seed);\n  const d = size * (0.05 + rand() * 0.03);\n  const tilt = rand() * Math.PI * 2;\n  const R = size * 0.4;\n  const a: Pt = [size / 2 - Math.cos(tilt) * d, size / 2 - Math.sin(tilt) * d];\n  const b: Pt = [size / 2 + Math.cos(tilt) * d, size / 2 + Math.sin(tilt) * d];\n  const accent = Math.floor(rings * 0.7);\n  const out: Stroke[] = [];\n  for (let i = 1; i <= rings; i++) {\n    const r = (R * i) / rings;\n    const n = Math.max(24, Math.round(r * 0.9));\n    out.push({ d: toPath(ring(a[0], a[1], r, n)), pen: 0 });\n    out.push({ d: toPath(ring(b[0], b[1], r, n)), pen: i === accent ? 1 : 0 });\n  }\n  return out;\n}\n\n/**\n * Hatching inside any outline (or several, even-odd, so a ring leaves its hole\n * open): parallel strokes at `angle` degrees (0 runs across, 90 up the page),\n * `gap` apart, each kept `inset` clear of the outline so the line drawn round\n * it stands free. Lines sit on one grid across the page, so two shapes hatched\n * alike line up, and the pen sweeps left to right, turning at each end as a\n * plotter does (`reverse` sweeps back the other way).\n */\nexport function hatchPolygon(\n  outline: [number, number][] | [number, number][][],\n  { angle = 45, gap = 5, inset = 0, reverse = false }: { angle?: number; gap?: number; inset?: number; reverse?: boolean } = {},\n): Stroke[] {\n  const rings = (typeof outline[0][0] === 'number' ? [outline] : outline) as Pt[][];\n  const a = (angle * Math.PI) / 180;\n  // u runs along the strokes; n across them, pointed rightward so the sweep reads left to right.\n  const u: Pt = [Math.cos(a), -Math.sin(a)];\n  let n: Pt = [Math.sin(a), Math.cos(a)];\n  if (n[0] < -1e-9 || (Math.abs(n[0]) < 1e-9 && n[1] < 0)) n = [-n[0], -n[1]];\n  if (reverse) n = [-n[0], -n[1]];\n  const dot = (p: Pt, v: Pt) => p[0] * v[0] + p[1] * v[1];\n  const all = rings.flat();\n  const s = all.map((p) => dot(p, n));\n  const lo = Math.min(...s), hi = Math.max(...s);\n  const out: Stroke[] = [];\n  let flip = false;\n  for (let c = Math.ceil(lo / gap) * gap; c <= hi; c += gap) {\n    const ts: number[] = [];\n    for (const ring of rings) {\n      for (let i = 0; i < ring.length; i++) {\n        const p = ring[i], q = ring[(i + 1) % ring.length];\n        const sp = dot(p, n), sq = dot(q, n);\n        // Half-open, so a vertex on the line counts once.\n        if ((sp <= c && sq > c) || (sq <= c && sp > c)) {\n          const k = (c - sp) / (sq - sp);\n          ts.push(dot(p, u) + k * (dot(q, u) - dot(p, u)));\n        }\n      }\n    }\n    ts.sort((x, y) => x - y);\n    const runs: [number, number][] = [];\n    for (let i = 0; i + 1 < ts.length; i += 2) {\n      const t0 = ts[i] + inset, t1 = ts[i + 1] - inset;\n      if (t1 - t0 > 0.8) runs.push([t0, t1]);\n    }\n    if (flip) runs.reverse();\n    for (const [t0, t1] of runs) {\n      const [ta, tb] = flip ? [t1, t0] : [t0, t1];\n      out.push({ d: toPath([[c * n[0] + ta * u[0], c * n[1] + ta * u[1]], [c * n[0] + tb * u[0], c * n[1] + tb * u[1]]]), pen: 0 });\n    }\n    if (runs.length) flip = !flip;\n  }\n  return out;\n}\n\n// ── Ink pieces ──────────────────────────────────────────────────────────────\n// The marks the ink components draw (ink-highlight, ink-divider, ink-frame,\n// ink-progress, ink-stamp, ink-signature, ink-arrow): underlines, loops,\n// waves, dots, crop marks, stamp rings, dials and arrows, made the way a hand\n// makes them, never quite straight and never quite closed. Every one is\n// seeded, and draws in a box of the size it is given, in px.\n\n/** A point, in the units of the box a generator draws in. */\nexport type Point = Pt;\n\n/** One line of the pen's points, for paced(), and which pen draws it. */\nexport interface Line {\n  pts: Pt[];\n  pen?: 0 | 1;\n}\n\n/**\n * Lines cut into pieces about `step` long, in drawing order. svg-scroll-draw\n * draws every path over the same range, so a long line and a short one take\n * the same time; cut into even pieces and given a stagger of 1 (each piece\n * after the last), the pen moves at one speed, as a hand writing does.\n */\nexport function paced(lines: Line[], step = 8): Stroke[] {\n  const out: Stroke[] = [];\n  for (const { pts, pen = 0 } of lines) {\n    if (pts.length < 2) continue;\n    const pieces: Pt[][] = [];\n    let piece: Pt[] = [pts[0]];\n    let run = 0;\n    for (let i = 1; i < pts.length; i++) {\n      let [ax, ay] = pts[i - 1];\n      const [bx, by] = pts[i];\n      let seg = Math.hypot(bx - ax, by - ay);\n      while (seg > 0 && run + seg >= step) {\n        const t = (step - run) / seg;\n        const cut: Pt = [ax + (bx - ax) * t, ay + (by - ay) * t];\n        piece.push(cut);\n        pieces.push(piece);\n        piece = [cut];\n        seg -= step - run;\n        [ax, ay] = cut;\n        run = 0;\n      }\n      run += seg;\n      piece.push([bx, by]);\n    }\n    // A short tail joins the piece before it rather than taking a beat of its own.\n    if (run > step * 0.4 || !pieces.length) pieces.push(piece);\n    else pieces[pieces.length - 1].push(...piece.slice(1));\n    for (const p of pieces) out.push({ d: toPath(p), pen });\n  }\n  return out;\n}\n\n/**\n * A line from a to b as a hand rules it: a slight bow to one side and a slow\n * tremor along it, both seeded. Smooth waves, not jitter, so it never looks\n * jagged at any length.\n */\nfunction handLine(a: Pt, b: Pt, rand: () => number, { bow = 0.012, tremor = 0.4, step = 3 } = {}): Pt[] {\n  const len = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1;\n  const n = Math.max(2, Math.round(len / step));\n  const nx = -(b[1] - a[1]) / len, ny = (b[0] - a[0]) / len;\n  const k = (rand() - 0.5) * 2 * bow * len;\n  const p1 = rand() * 6, p2 = rand() * 6;\n  return Array.from({ length: n + 1 }, (_, i): Pt => {\n    const t = i / n;\n    // The tremor fades in and out at the ends, where the pen is set down and lifted.\n    const off = k * Math.sin(Math.PI * t) + tremor * Math.sin(Math.PI * t) ** 0.5 * (0.6 * Math.sin((t * len) / 26 + p1) + 0.4 * Math.sin((t * len) / 9 + p2));\n    return [a[0] + (b[0] - a[0]) * t + nx * off, a[1] + (b[1] - a[1]) * t + ny * off];\n  });\n}\n\n/** A circle drawn by hand: a little more than one turn, its radius breathing, from a seeded angle. */\nfunction handCircle(cx: number, cy: number, r: number, rand: () => number, { turns = 1.04, wobble = 0.018, ry = r } = {}): Pt[] {\n  const a0 = rand() * Math.PI * 2;\n  const p = rand() * 6;\n  const n = Math.max(48, Math.round((r + ry) * turns * 1.6));\n  return Array.from({ length: n + 1 }, (_, i): Pt => {\n    const t = i / n;\n    const a = a0 + t * turns * Math.PI * 2;\n    // Spirals out a touch over the turn, so the end passes just outside the start.\n    const k = 1 + wobble * Math.sin(a * 2 + p) + (t - 0.5) * wobble * 1.2;\n    return [cx + Math.cos(a) * r * k, cy + Math.sin(a) * ry * k];\n  });\n}\n\n/** A circle drawn by hand in a size × size box: a little more than one turn, from a seeded angle. */\nexport function sketchCircle({ size, seed = 1, r = size * 0.42, turns = 1.08 }: { size: number; seed?: number; r?: number; turns?: number }): Stroke[] {\n  return [{ d: toPath(handCircle(size / 2, size / 2, r, random(seed), { turns, wobble: 0.03 })), pen: 0 }];\n}\n\n/**\n * An underline under a w × h box, the pen pressing in from the left and\n * lifting with a small rise at the end; `double` adds a second, shorter\n * stroke under it. Accent pen.\n */\nexport function inkUnderline({ w, h = 12, seed = 1, double = false }: { w: number; h?: number; seed?: number; double?: boolean }): Stroke[] {\n  const rand = random(seed);\n  const y = double ? h * 0.32 : h * 0.5;\n  const tilt = (rand() - 0.3) * 2.2;\n  const first = handLine([2, y + tilt / 2], [w - 2, y - tilt / 2 - 1], rand, { bow: 0.006, tremor: 0.5 });\n  const out: Stroke[] = [{ d: toPath(first), pen: 1 }];\n  if (double) out.push({ d: toPath(handLine([w * (0.05 + rand() * 0.04), y + 4.6], [w * (0.86 + rand() * 0.08), y + 3.8 - tilt / 3], rand, { bow: 0.008, tremor: 0.5 })), pen: 1 });\n  return out;\n}\n\n/** A line struck through a w × h box, a little above the middle and rising slightly. Accent pen. */\nexport function inkStrike({ w, h, seed = 1 }: { w: number; h: number; seed?: number }): Stroke[] {\n  const rand = random(seed);\n  return [{ d: toPath(handLine([1, h * 0.6], [w - 1, h * (0.46 + rand() * 0.06)], rand, { bow: 0.01, tremor: 0.45 })), pen: 1 }];\n}\n\n/**\n * A loop round a w × h box, as a hand circles a word: an ellipse touching the\n * box, a little more than one turn, its end passing outside its start.\n * Accent pen.\n */\nexport function inkLoop({ w, h, seed = 1, turns = 1.1 }: { w: number; h: number; seed?: number; turns?: number }): Stroke[] {\n  const rand = random(seed);\n  const pts = handCircle(w / 2, h / 2, w / 2 - 3, rand, { turns, wobble: 0.03, ry: h / 2 - 3 });\n  // A circling hand leans: tilt the ellipse a degree or two.\n  const tilt = (rand() - 0.5) * 0.06;\n  const c = Math.cos(tilt), s = Math.sin(tilt);\n  return [{ d: toPath(pts.map(([x, y]): Pt => [w / 2 + (x - w / 2) * c - (y - h / 2) * s, h / 2 + (x - w / 2) * s + (y - h / 2) * c])), pen: 1 }];\n}\n\n/**\n * A box round a w × h area as a hand rules one: four strokes, clockwise from\n * the top, each running a little past the corner so they cross there. Accent\n * pen.\n */\nexport function inkBox({ w, h, seed = 1, over = 4 }: { w: number; h: number; seed?: number; over?: number }): Stroke[] {\n  const rand = random(seed);\n  const o = () => over * (0.4 + rand() * 0.8);\n  const j = () => (rand() - 0.5) * 2.4;\n  const m = over;\n  const sides: [Pt, Pt][] = [\n    [[m - o(), m + j()], [w - m + o(), m + j()]],\n    [[w - m + j(), m - o()], [w - m + j(), h - m + o()]],\n    [[w - m + o(), h - m + j()], [m - o(), h - m + j()]],\n    [[m + j(), h - m + o()], [m + j(), m - o()]],\n  ];\n  return sides.map(([a, b]) => ({ d: toPath(handLine(a, b, rand, { bow: 0.02, tremor: 0.5 })), pen: 1 }));\n}\n\n/** A rule across a w × h box, as a hand draws it; `reverse` draws it right to left. */\nexport function ruledLine({ w, h = 12, seed = 1, reverse = false }: { w: number; h?: number; seed?: number; reverse?: boolean }): Stroke[] {\n  const rand = random(seed);\n  const a: Pt = [1.5, h / 2 + (rand() - 0.5) * 0.8], b: Pt = [w - 1.5, h / 2 + (rand() - 0.5) * 0.8];\n  return [{ d: toPath(reverse ? handLine(b, a, rand, { bow: 0.0015, tremor: 0.35 }) : handLine(a, b, rand, { bow: 0.0015, tremor: 0.35 })), pen: 0 }];\n}\n\n/** A wavy line across a w × h box, its swing rising and falling a little along the way. */\nexport function inkWave({ w, h = 12, seed = 1, period = 22 }: { w: number; h?: number; seed?: number; period?: number }): Stroke[] {\n  const noise = noise2(seed);\n  const amp = h * 0.3;\n  const pts: Pt[] = [];\n  for (let x = 2; x <= w - 2; x += 1.5) {\n    const k = 1 + 0.5 * noise(x / 140, 0.5);\n    pts.push([x, h / 2 + Math.sin(((x - 2) / period) * Math.PI * 2) * amp * k]);\n  }\n  return [{ d: toPath(pts), pen: 0 }];\n}\n\n/** A dotted line across a w × h box: a dot every `gap`, each a short touch of the pen, left to right. */\nexport function inkDots({ w, h = 12, seed = 1, gap = 9 }: { w: number; h?: number; seed?: number; gap?: number }): Stroke[] {\n  const rand = random(seed);\n  const n = Math.max(1, Math.floor((w - 4) / gap));\n  const x0 = (w - (n - 1) * gap) / 2;\n  return Array.from({ length: n }, (_, i): Stroke => {\n    const x = x0 + i * gap + (rand() - 0.5) * gap * 0.12;\n    const y = h / 2 + (rand() - 0.5) * 0.8;\n    return { d: `M${f(x - 0.5)} ${f(y)}L${f(x + 0.5)} ${f(y + (rand() - 0.5) * 0.4)}`, pen: 0 };\n  });\n}\n\n/** Crop marks at the corners of a w × h box, `inset` in from its edges: an L at each, clockwise from the top left. */\nexport function cropMarks({ w, h, seed = 1, len = 16, inset = 2 }: { w: number; h: number; seed?: number; len?: number; inset?: number }): Stroke[] {\n  const rand = random(seed);\n  const j = () => (rand() - 0.5) * 0.9;\n  const corners: [number, number, number, number][] = [[inset, inset, 1, 1], [w - inset, inset, -1, 1], [w - inset, h - inset, -1, -1], [inset, h - inset, 1, -1]];\n  return corners.map(([x, y, sx, sy], i): Stroke => {\n    const arm = len * (0.9 + rand() * 0.2);\n    // Each L is drawn from the end of one arm round the corner to the end of the other, clockwise.\n    const a: Pt = i % 2 ? [x, y + sy * arm] : [x + sx * arm, y];\n    const b: Pt = i % 2 ? [x + sx * arm, y] : [x, y + sy * arm];\n    return { d: toPath([[a[0] + j(), a[1] + j()], [x + j(), y + j()], [b[0] + j(), b[1] + j()]]), pen: 0 };\n  });\n}\n\n/**\n * The rings of a rubber stamp in a size × size box: `circle` is two rings\n * close together round the rim and one inside, leaving a band for a line of\n * text; `seal` swaps the outer ring for a scalloped edge and dots the band's\n * inner side.\n */\nexport function stampRings({ size, seed = 1, kind = 'circle' }: { size: number; seed?: number; kind?: 'circle' | 'seal' }): Stroke[] {\n  const rand = random(seed);\n  const c = size / 2;\n  const out: Stroke[] = [];\n  if (kind === 'seal') {\n    const teeth = 28;\n    const R = size * 0.485;\n    const n = teeth * 12;\n    const a0 = rand() * Math.PI * 2;\n    out.push({\n      d: toPath(Array.from({ length: n + 1 }, (_, i): Pt => {\n        const a = a0 + (i / n) * Math.PI * 2;\n        const r = R * (0.93 + 0.07 * Math.abs(Math.cos((a * teeth) / 2))) * (1 + (rand() - 0.5) * 0.004);\n        return [c + Math.cos(a) * r, c + Math.sin(a) * r];\n      })),\n      pen: 0,\n    });\n    out.push({ d: toPath(handCircle(c, c, size * 0.405, rand)), pen: 0 });\n  } else {\n    out.push({ d: toPath(handCircle(c, c, size * 0.47, rand)), pen: 0 });\n    out.push({ d: toPath(handCircle(c, c, size * 0.445, rand, { turns: 1.02 })), pen: 0 });\n  }\n  out.push({ d: toPath(handCircle(c, c, size * 0.315, rand, { turns: 1.03 })), pen: 0 });\n  if (kind === 'seal') {\n    for (let i = 0; i < 48; i++) {\n      const a = (i / 48) * Math.PI * 2;\n      const r = size * 0.29;\n      out.push({ d: `M${f(c + Math.cos(a) * r)} ${f(c + Math.sin(a) * r)}L${f(c + Math.cos(a + 0.012) * r)} ${f(c + Math.sin(a + 0.012) * r)}`, pen: 0 });\n    }\n  }\n  return out;\n}\n\n/**\n * A progress dial in a size × size box: a ring the pen goes round once in\n * ink, a tick at each quarter, then a band of three arcs in the accent pen\n * from the top, clockwise, out to `share` (0 to 1), and a short cross-stroke\n * where it stops.\n */\nexport function dial({ size, seed = 1, share = 0.7 }: { size: number; seed?: number; share?: number }): Stroke[] {\n  const rand = random(seed);\n  const c = size / 2;\n  const R = size * 0.45;\n  const s = Math.max(0, Math.min(1, share));\n  const out: Stroke[] = [{ d: toPath(handCircle(c, c, R, rand, { turns: 1.02, wobble: 0.008 })), pen: 0 }];\n  for (let q = 0; q < 4; q++) {\n    const a = -Math.PI / 2 + (q * Math.PI) / 2;\n    out.push({ d: toPath([[c + Math.cos(a) * (R + 3), c + Math.sin(a) * (R + 3)], [c + Math.cos(a) * (R + 8), c + Math.sin(a) * (R + 8)]]), pen: 0 });\n  }\n  if (s > 0) {\n    const a0 = -Math.PI / 2, a1 = a0 + Math.PI * 2 * s;\n    for (const k of [0.86, 0.81, 0.76]) out.push({ d: toPath(ring(c, c, R * k + (rand() - 0.5) * 0.6, Math.max(6, Math.round(90 * s)), a0, a1)), pen: 1 });\n    out.push({ d: toPath([[c + Math.cos(a1) * R * 0.7, c + Math.sin(a1) * R * 0.7], [c + Math.cos(a1) * R * 0.92, c + Math.sin(a1) * R * 0.92]]), pen: 0 });\n  }\n  return out;\n}\n\n/** The kinds of arrow inkArrow draws. */\nexport type ArrowKind = 'straight' | 'curved' | 'looped';\n\n/**\n * A hand-drawn arrow in a w × h box, from `from` to `to` (each a point as a\n * share of the box, 0 to 1): `straight` with a slight bow, `curved` bending\n * `bend` of its length to one side (negative for the other), `looped` with a\n * loop the pen throws half way. The head is one stroke, barb to tip to barb.\n * Paced (see paced()), so give it a stagger of 1.\n */\nexport function inkArrow({\n  w, h, kind = 'curved', from = [0.06, 0.3], to = [0.92, 0.72], bend = 0.3, seed = 1, head = 12, step = 8,\n}: { w: number; h: number; kind?: ArrowKind; from?: [number, number]; to?: [number, number]; bend?: number; seed?: number; head?: number; step?: number }): Stroke[] {\n  const rand = random(seed);\n  const a: Pt = [from[0] * w, from[1] * h], b: Pt = [to[0] * w, to[1] * h];\n  const len = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1;\n  const ux = (b[0] - a[0]) / len, uy = (b[1] - a[1]) / len;\n  const nx = -uy, ny = ux;\n  const k = kind === 'straight' ? 0.04 * Math.sign(bend || 1) : bend;\n  const n = Math.max(24, Math.round(len / 2));\n  const p = rand() * 6;\n  const loopR = Math.min(len * 0.12, 24);\n  const shaft: Pt[] = [];\n  for (let i = 0; i <= n; i++) {\n    const t = i / n;\n    // A quadratic bow, the control point `k` of the length to the side.\n    let x = a[0] + (b[0] - a[0]) * t + nx * k * len * 2 * t * (1 - t);\n    let y = a[1] + (b[1] - a[1]) * t + ny * k * len * 2 * t * (1 - t);\n    if (kind === 'looped') {\n      // One turn round a circle between t = 0.3 and 0.7, on the bend's outer\n      // side: forward at both ends (so the line runs on without a corner) and\n      // backward over the top, faster than the line moves on, so it crosses itself.\n      const e = Math.max(0, Math.min(1, (t - 0.3) / 0.4));\n      const side = k >= 0 ? -1 : 1;\n      const back = Math.sin(e * Math.PI * 2) * loopR;\n      const out = (1 - Math.cos(e * Math.PI * 2)) * loopR * side;\n      x += ux * back + nx * out;\n      y += uy * back + ny * out;\n    }\n    const tr = 0.5 * Math.sin(Math.PI * t) * Math.sin(t * 9 + p);\n    shaft.push([x + nx * tr, y + ny * tr]);\n  }\n  // The head points along the shaft's last few points.\n  const [tx, ty] = shaft[shaft.length - 1];\n  const [qx, qy] = shaft[Math.max(0, shaft.length - 5)];\n  const dl = Math.hypot(tx - qx, ty - qy) || 1;\n  const dx = (tx - qx) / dl, dy = (ty - qy) / dl;\n  const barb = (sign: number): Pt => {\n    const ang = (0.47 + (rand() - 0.5) * 0.08) * sign;\n    const L = head * (0.9 + rand() * 0.2);\n    return [tx - (dx * Math.cos(ang) - dy * Math.sin(ang)) * L, ty - (dx * Math.sin(ang) + dy * Math.cos(ang)) * L];\n  };\n  const b1 = barb(1), b2 = barb(-1);\n  const headLine: Pt[] = [...handLine(b1, [tx, ty], rand, { bow: 0.04, tremor: 0.2, step: 2 }), ...handLine([tx, ty], b2, rand, { bow: -0.04, tremor: 0.2, step: 2 }).slice(1)];\n  return paced([{ pts: shaft, pen: 1 }, { pts: headLine, pen: 1 }], step);\n}\n",
      "type": "registry:lib"
    }
  ],
  "type": "registry:lib"
}