{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "hilo",
  "title": "Hilo",
  "description": "An ink thread tied between two points that lays out, sags, and snaps into frayed ends with an optional vermilion notch.",
  "registryDependencies": [
    "@jbm/tokens",
    "@jbm/scene-geometry"
  ],
  "files": [
    {
      "path": "registry/jbm/ui/hilo.tsx",
      "content": "import { color, stroke } from \"../lib/tokens\"\nimport { unit, type Pt } from \"../lib/geometry\"\n\n/** `arc` bows the thread sideways; `s` leaves and arrives level, like a line tied to a tab. */\nexport type HiloCurve = \"arc\" | \"s\"\n\nexport type HiloProps = {\n  /** Where the thread is tied first, in the parent SVG's user units. */\n  from: Pt\n  /** Where the thread is tied last, in the parent SVG's user units. */\n  to: Pt\n  /** Curvature, 0 = straight. `arc`: sideways bow as a fraction of the from→to distance (positive bows left of travel, upward for a left-to-right thread). `s`: level handles as a fraction of the horizontal distance, clamped to 0–1 so the thread never hooks around its anchors (0.5 is a soft S). */\n  bend?: number\n  /** Curve family that `bend` shapes. */\n  curve?: HiloCurve\n  /** Progress 0–1. Without `snapAt` it lays the thread from `from` to `to`. With `snapAt`, 0 → snapAt lays it and snapAt → 1 snaps it. */\n  draw?: number\n  /** Thread stroke width in user units; default stroke.outline (3), the shared outline. Knots, fibers and the notch scale with it. */\n  width?: number\n  /** The `draw` value (0–1) at which the laid thread snaps. Omit for a thread that never snaps; 0 starts already laid, so `draw` drives only the snap. */\n  snapAt?: number\n  /** Where the thread parts, as a fraction 0–1 of its length from `from` (clamped to 0.05–0.95). */\n  breakAt?: number\n  /** 0–1: how many short tapered strands (two or three) split from each snapped end, and how long (at most about 1.5 widths). 0 is a clean cut. */\n  fray?: number\n  /** 0–1: before a snap, how far the thread sags under gravity; after a snap, how limply the two ends hang (a limp end droops downward and loses the arc's bow, whichever way it bowed). */\n  slack?: number\n  /** Marks the break in vermilion: a short tint on each broken tip, at the thread's width and along its curve, so the mark belongs to the ends. The only accent the thread draws. */\n  notch?: boolean\n  /** Small ink knots where the thread is tied to `from` and `to`. */\n  knots?: boolean\n}\n\n/** A cubic Bézier: start, two handles, end. */\nexport type HiloCubic = readonly [Pt, Pt, Pt, Pt]\n\nconst add = (a: Pt, b: Pt, k = 1): Pt => ({ x: a.x + b.x * k, y: a.y + b.y * k })\nconst sub = (a: Pt, b: Pt): Pt => ({ x: a.x - b.x, y: a.y - b.y })\nconst lerp = (a: Pt, b: Pt, t: number): Pt => ({\n  x: a.x + (b.x - a.x) * t,\n  y: a.y + (b.y - a.y) * t,\n})\nconst norm = (v: Pt): Pt => {\n  const l = Math.hypot(v.x, v.y)\n  return l > 1e-9 ? { x: v.x / l, y: v.y / l } : { x: 1, y: 0 }\n}\nconst finite = (p: Pt): Pt => ({\n  x: Number.isFinite(p.x) ? p.x : 0,\n  y: Number.isFinite(p.y) ? p.y : 0,\n})\nconst num = (v: number | undefined, fallback: number) =>\n  typeof v === \"number\" && Number.isFinite(v) ? v : fallback\n\nexport function cubicPoint(c: HiloCubic, t: number): Pt {\n  const a = lerp(c[0], c[1], t),\n    b = lerp(c[1], c[2], t),\n    d = lerp(c[2], c[3], t)\n  return lerp(lerp(a, b, t), lerp(b, d, t), t)\n}\n\n/** The exact sub-curve between parameters t0 and t1 (de Casteljau). */\nfunction split(c: HiloCubic, t0: number, t1: number): HiloCubic {\n  const left = (q: HiloCubic, t: number): HiloCubic => {\n    const a = lerp(q[0], q[1], t),\n      b = lerp(q[1], q[2], t),\n      d = lerp(q[2], q[3], t),\n      e = lerp(a, b, t),\n      f = lerp(b, d, t)\n    return [q[0], a, e, lerp(e, f, t)]\n  }\n  const right = (q: HiloCubic, t: number): HiloCubic => {\n    const a = lerp(q[0], q[1], t),\n      b = lerp(q[1], q[2], t),\n      d = lerp(q[2], q[3], t),\n      e = lerp(a, b, t),\n      f = lerp(b, d, t)\n    return [lerp(e, f, t), f, d, q[3]]\n  }\n  const head = t1 >= 1 ? c : left(c, t1)\n  if (t0 <= 0) return head\n  return right(head, t1 > 0 ? t0 / t1 : 0)\n}\n\nconst SAMPLES = 96\n/** Cumulative arc length at t = i / SAMPLES. */\nfunction lengths(c: HiloCubic) {\n  const out = [0]\n  let prev = c[0]\n  for (let i = 1; i <= SAMPLES; i++) {\n    const p = cubicPoint(c, i / SAMPLES)\n    out.push(out[i - 1] + Math.hypot(p.x - prev.x, p.y - prev.y))\n    prev = p\n  }\n  return out\n}\n/** Curve parameter at a fraction 0–1 of the arc length. */\nfunction paramAt(table: number[], fraction: number) {\n  const total = table[SAMPLES]\n  if (total <= 0) return fraction\n  const target = unit(fraction) * total\n  let i = 1\n  while (i < SAMPLES && table[i] < target) i++\n  const span = table[i] - table[i - 1]\n  return (i - 1 + (span > 0 ? (target - table[i - 1]) / span : 0)) / SAMPLES\n}\n\n/**\n * Strands at a frayed end, in the order they appear as fray grows: where each leaves the cut\n * across the thread (-1 to 1, gravity side positive), its share of the cut's width, how far it\n * turns toward gravity (degrees), and its relative length. Two or three short, straight, tapered\n * strands that tile the cut, so the thread splits into them without a step: a frayed end, never\n * claws, legs, or a fan. Fixed, so every frame is identical.\n */\nconst STRANDS = [\n  [{ across: 0, share: 1, angle: 0, length: 1 }],\n  [\n    { across: -0.5, share: 0.5, angle: 2, length: 1 },\n    { across: 0.5, share: 0.5, angle: 7, length: 0.72 },\n  ],\n  [\n    { across: -0.667, share: 0.333, angle: 2, length: 0.85 },\n    { across: 0, share: 0.334, angle: 4, length: 1 },\n    { across: 0.667, share: 0.333, angle: 8, length: 0.7 },\n  ],\n] as const\n\n/**\n * Everything Hilo draws, in parent SVG units: the resting curve, the laid or snapped pieces (each\n * pinned to its anchor), frayed fiber strokes, the vermilion notch, and the knots. Pure and\n * deterministic: the same props always give the same geometry.\n */\nexport function hiloGeometry({\n  from: rawFrom,\n  to: rawTo,\n  bend = 0,\n  curve = \"arc\",\n  draw = 1,\n  width = stroke.outline,\n  snapAt,\n  breakAt = 0.5,\n  fray = 0.6,\n  slack = 0,\n  notch = false,\n  knots = true,\n}: HiloProps) {\n  const from = finite(rawFrom),\n    to = finite(rawTo)\n  const w = Math.max(0.5, num(width, stroke.outline))\n  const k = num(bend, 0)\n  const s = unit(slack),\n    f = unit(fray)\n  const d = sub(to, from)\n  const L = Math.hypot(d.x, d.y)\n  // The resting curve: bend shapes it, slack sags it (gravity is +y).\n  let c1: Pt, c2: Pt\n  if (curve === \"s\") {\n    // Level handles shorter than 0 point back past the anchors and hook the thread around its\n    // own knots; longer than 1 they cross and loop. 0–1 keeps every S a single sweep.\n    const h = Math.min(1, Math.max(0, k))\n    c1 = { x: from.x + h * d.x, y: from.y }\n    c2 = { x: to.x - h * d.x, y: to.y }\n  } else {\n    const n = L > 0 ? { x: d.y / L, y: -d.x / L } : { x: 0, y: -1 }\n    c1 = add(add(from, d, 1 / 3), n, (4 / 3) * k * L)\n    c2 = add(add(from, d, 2 / 3), n, (4 / 3) * k * L)\n  }\n  // Phases: lay the thread, then (optionally) snap it.\n  const p = unit(draw)\n  const snaps = typeof snapAt === \"number\" && Number.isFinite(snapAt)\n  const at = snaps ? unit(snapAt) : 1\n  let lay = !snaps ? p : at <= 0 ? 1 : Math.min(1, p / at)\n  const recoil = snaps && at < 1 && p > at ? (p - at) / (1 - at) : 0\n  // Before a snap slack sags the whole route; as the ends recoil the sag hands over to each end\n  // hanging from its own anchor, so a snapped thread is two limp ends, never one deep V.\n  const sag = { x: 0, y: (4 / 3) * s * 0.25 * L * (1 - recoil) }\n  // A snapped thread has no tension left to hold its bow: with slack the bow relaxes toward the\n  // straight line as the ends recoil, and each end then droops under gravity (below), so limp\n  // ends always hang down, never arch up, whichever way the arc bowed.\n  const relax = s * recoil\n  const straight1 = add(from, d, 1 / 3),\n    straight2 = add(from, d, 2 / 3)\n  const base: HiloCubic = [\n    from,\n    add(lerp(c1, straight1, relax), sag),\n    add(lerp(c2, straight2, relax), sag),\n    to,\n  ]\n  const table = lengths(base)\n  const length = table[SAMPLES]\n  // Within a few knot radii of the far anchor the thread is laid: it reaches the knot, which\n  // then shows, instead of stopping in mid-air a hair short of it.\n  if (lay > 0 && lay < 1 && (1 - lay) * length <= 6 * w) lay = 1\n\n  const pieces: HiloCubic[] = []\n  const strands: Pt[][] = []\n  const tints: HiloCubic[] = []\n  const inked: HiloCubic[] = []\n  if (length > 0 && lay > 0 && recoil <= 0) {\n    pieces.push(split(base, 0, paramAt(table, lay)))\n  } else if (length > 0 && recoil > 0) {\n    // Half the gap, as a fraction of the length: about a fifth of the thread, never more than 90\n    // units, or 20 widths for a thick thread, so its fibers and the notch still fit with paper\n    // showing between them.\n    const half = Math.min(\n      Math.max(0.09, (10 * w) / length),\n      Math.max(45, 10 * w) / length,\n      0.45\n    )\n    // The gap opens around the break point, moved inward just enough to leave each end a stub\n    // longer than its knot and fibers.\n    const stub = Math.min(0.5 - half, Math.max(0.04, (8 * w) / length))\n    const b = Math.min(\n      1 - half - stub,\n      Math.max(half + stub, Math.min(0.95, Math.max(0.05, num(breakAt, 0.5))))\n    )\n    const sA = b - recoil * half,\n      sB = b + recoil * half\n    const tA = paramAt(table, sA),\n      tB = paramAt(table, sB)\n    const a = split(base, 0, tA),\n      z = split(base, tB, 1)\n    // Each end hangs from its own anchor: the droop grows with u² from the anchor, so the anchor\n    // and its tangent never move and only the free end falls.\n    const hang = (len: number) => recoil * s * 0.5 * len\n    const lenA = sA * length,\n      lenB = (1 - sB) * length\n    const dA = hang(lenA),\n      dB = hang(lenB)\n    // A limp end keeps its anchor tangent but only briefly: the anchor's handle shortens with\n    // slack, so the end falls away from its pin almost at once instead of first running on along\n    // the old route (which reads as an arch).\n    const grip = 1 - 0.75 * relax\n    const pieceA: HiloCubic = [\n      a[0],\n      lerp(a[0], a[1], grip),\n      add(a[2], { x: 0, y: dA / 3 }),\n      add(a[3], { x: 0, y: dA }),\n    ]\n    const pieceB: HiloCubic = [\n      add(z[0], { x: 0, y: dB }),\n      add(z[1], { x: 0, y: dB / 3 }),\n      lerp(z[3], z[2], grip),\n      z[3],\n    ]\n    pieces.push(pieceA, pieceB)\n    // Strands spring out quickly after the snap: straight continuations of the thread, at most\n    // about 1.5 widths long (never under 3 units, so a fine thread still shows its fray).\n    const spring = Math.min(1, recoil * 5)\n    const count = f > 0 ? (f < 0.5 ? 2 : 3) : 0\n    const reach = count > 0 ? spring * Math.max(3, w * (0.9 + 0.6 * f)) : 0\n    // The vermilion tint: the last few units of each broken end, along its own curve.\n    const tintLen = Math.max(5, 3 * w)\n    for (const [piece, atEnd] of [\n      [pieceA, true],\n      [pieceB, false],\n    ] as const) {\n      const pt = lengths(piece)\n      const pl = pt[SAMPLES]\n      if (notch && pl > 2 * tintLen) {\n        const cut = paramAt(pt, atEnd ? 1 - tintLen / pl : tintLen / pl)\n        if (atEnd) {\n          inked.push(split(piece, 0, cut))\n          tints.push(split(piece, cut, 1))\n        } else {\n          tints.push(split(piece, 0, cut))\n          inked.push(split(piece, cut, 1))\n        }\n      } else inked.push(piece)\n      if (reach <= 0 || reach < w * 0.6) continue\n      const tip = atEnd ? piece[3] : piece[0]\n      const near = cubicPoint(piece, atEnd ? 0.99 : 0.01)\n      const out = norm(sub(tip, near))\n      // Turn toward gravity (+y): the side of the thread that faces down, or the same fixed side\n      // when the end points straight up or down.\n      const side = out.x > 1e-6 ? 1 : out.x < -1e-6 ? -1 : 1\n      const across = { x: -out.y * side, y: out.x * side }\n      for (const st of STRANDS[count - 1]) {\n        const r = (side * st.angle * Math.PI) / 180\n        const dir = {\n          x: out.x * Math.cos(r) - out.y * Math.sin(r),\n          y: out.x * Math.sin(r) + out.y * Math.cos(r),\n        }\n        // A tapered strand: its base is its share of the square cut, its point a strand's\n        // length on. Bases tile the cut exactly, so the end has no step and no round cap.\n        const mid = add(tip, across, (st.across * w) / 2)\n        const halfBase = (st.share * w) / 2\n        const b0 = add(mid, across, -halfBase),\n          b1 = add(mid, across, halfBase)\n        strands.push([b0, add(mid, dir, reach * st.length), b1])\n      }\n    }\n  }\n  const tied = knots && lay > 0\n  return {\n    base,\n    length,\n    lay,\n    recoil,\n    snapped: recoil > 0,\n    width: w,\n    pieces,\n    /** The pieces as drawn in ink: each snapped end short of its vermilion tint. */\n    inked: inked.length ? inked : pieces,\n    /** Tapered strand outlines (base corner, point, base corner), filled in ink. */\n    strands,\n    /** The vermilion tints on the broken tips; empty without `notch` or before a snap. */\n    notch: tints,\n    knots: tied ? (lay >= 1 ? [from, to] : [from]) : [],\n    knotRadius: w * 1.4,\n  }\n}\n\nconst n2 = (v: number) => Math.round(v * 100) / 100\nconst cubicPath = (c: HiloCubic) =>\n  `M${n2(c[0].x)} ${n2(c[0].y)}C${n2(c[1].x)} ${n2(c[1].y)} ${n2(c[2].x)} ${n2(c[2].y)} ${n2(c[3].x)} ${n2(c[3].y)}`\n\n/**\n * An ink thread tied from one point to another. It lays itself out with `draw`, can sag with\n * `slack`, and can snap into two frayed ends that stay tied to `from` and `to`, with an optional\n * vermilion notch in the gap. SVG <g>: render inside an <svg> that shares the anchors' units.\n */\nexport function Hilo(props: HiloProps) {\n  const g = hiloGeometry(props)\n  return (\n    <g\n      aria-hidden=\"true\"\n      data-hilo={g.snapped ? \"snapped\" : g.lay >= 1 ? \"tied\" : \"laying\"}\n      fill=\"none\"\n      strokeLinecap=\"round\"\n      strokeLinejoin=\"round\"\n    >\n      {g.inked.map((c, i) => (\n        <path\n          key={i}\n          d={cubicPath(c)}\n          stroke={color.ink}\n          strokeWidth={g.width}\n          // A snapped end is cut square, so its fibers read as the thread coming apart, not as\n          // whiskers on a rounded stub. The tied end sits under its knot.\n          strokeLinecap={g.snapped ? \"butt\" : undefined}\n        />\n      ))}\n      {g.notch.map((c, i) => (\n        <path\n          key={`t${i}`}\n          d={cubicPath(c)}\n          stroke={color.accent}\n          strokeWidth={g.width}\n          strokeLinecap=\"butt\"\n        />\n      ))}\n      {g.strands.length > 0 && (\n        <path\n          d={g.strands\n            .map((pts) => \"M\" + pts.map((q) => `${n2(q.x)} ${n2(q.y)}`).join(\"L\") + \"Z\")\n            .join(\"\")}\n          // Strands continue their tip: vermilion where the break is marked, ink otherwise.\n          fill={g.notch.length > 0 ? color.accent : color.ink}\n          stroke=\"none\"\n        />\n      )}\n      {g.knots.map((k, i) => (\n        <circle\n          key={i}\n          cx={n2(k.x)}\n          cy={n2(k.y)}\n          r={n2(g.knotRadius)}\n          fill={color.ink}\n          stroke=\"none\"\n        />\n      ))}\n    </g>\n  )\n}\n",
      "type": "registry:ui",
      "target": "src/jbm/ui/hilo.tsx"
    }
  ],
  "categories": [
    "UI Bits"
  ],
  "type": "registry:ui"
}