{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "shape",
  "title": "Shape",
  "description": "The shape language as data: the built-in ball, pill, tile and triangle as SVG paths, custom paths with their viewBox, fitting, masks and a cached sampled outline for physics.",
  "dependencies": [],
  "devDependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "lib/shape.ts",
      "content": "/**\n * The shape language as data. Every scenic component that draws \"a big flat shape\" takes a\n * `Shape`: one of the four built-in pieces, or any SVG path with its viewBox. The built-ins are\n * paths of the same format (`SHAPE_PATHS`), so a custom piece goes down the same road: fitted to\n * its box (the viewBox's larger side is the piece's `size`), filled and rimmed from the tokens.\n *\n * Geometry for physics is read once per path and cached: `shapeOutline` samples the outline with\n * `getPointAtLength` (client only) and `shapeRadius` gives the circle around the bounding box, so\n * a custom shape costs nothing extra per frame.\n */\n\nexport type ShapeName = \"ball\" | \"pill\" | \"tile\" | \"triangle\"\n\n/** Any SVG path, in the coordinates of its `viewBox` (\"minX minY width height\"). */\nexport interface CustomShape {\n  d: string\n  viewBox: string\n}\n\nexport type Shape = ShapeName | CustomShape\n\ninterface Vec {\n  x: number\n  y: number\n}\n\n/** The four built-in pieces as paths: ball, capsule (40% as tall), rounded tile and triangle. */\nexport const SHAPE_PATHS: Record<ShapeName, CustomShape> = {\n  ball: { d: \"M50 0A50 50 0 1 1 50 100A50 50 0 1 1 50 0Z\", viewBox: \"0 0 100 100\" },\n  pill: { d: \"M20 0H80A20 20 0 0 1 80 40H20A20 20 0 0 1 20 0Z\", viewBox: \"0 0 100 40\" },\n  tile: {\n    d: \"M20 0H80A20 20 0 0 1 100 20V80A20 20 0 0 1 80 100H20A20 20 0 0 1 0 80V20A20 20 0 0 1 20 0Z\",\n    viewBox: \"0 0 100 100\",\n  },\n  triangle: {\n    d: \"M43.5 7.5Q50 -2 56.5 7.5L96.5 76Q101 86 90 86H10Q-1 86 3.5 76Z\",\n    viewBox: \"0 0 100 88\",\n  },\n}\n\nexport const isShapeName = (shape: Shape): shape is ShapeName => typeof shape === \"string\"\n\n/** The path of any shape: built-ins resolve to their `SHAPE_PATHS` entry. */\nexport const resolveShape = (shape: Shape): CustomShape =>\n  isShapeName(shape) ? SHAPE_PATHS[shape] : shape\n\n/** `data-kind` value and cache key: the built-in's name, or `custom`. */\nexport const shapeName = (shape: Shape): ShapeName | \"custom\" =>\n  isShapeName(shape) ? shape : \"custom\"\n\n/** Stable string for memo keys. */\nexport const shapeKey = (shape: Shape) =>\n  isShapeName(shape) ? shape : `${shape.viewBox}|${shape.d}`\n\n/** The viewBox as numbers; a malformed one falls back to 0 0 100 100. */\nexport function parseViewBox(viewBox: string) {\n  const n = viewBox\n    .trim()\n    .split(/[\\s,]+/)\n    .map(Number)\n  const [x = 0, y = 0, w = 100, h = 100] = n\n  if (n.length !== 4 || n.some((v) => !Number.isFinite(v)) || w <= 0 || h <= 0) {\n    return { x: 0, y: 0, w: 100, h: 100 }\n  }\n  return { x, y, w, h }\n}\n\n/** Width and height of a shape whose larger side is `size`. */\nexport function shapeBox(shape: Shape, size: number) {\n  const vb = parseViewBox(resolveShape(shape).viewBox)\n  const m = Math.max(vb.w, vb.h)\n  return { w: (size * vb.w) / m, h: (size * vb.h) / m }\n}\n\n/** Radius of the circle around the shape's bounding box (collisions and reach). */\nexport function shapeRadius(shape: Shape, size: number) {\n  const { w, h } = shapeBox(shape, size)\n  return Math.hypot(w, h) / 2\n}\n\n/**\n * SVG transform that draws the path in a `w` x `h` box centred on the origin (rotated by `rotate`\n * degrees). Pair it with `vector-effect: non-scaling-stroke` so the rim keeps its width.\n */\nexport function shapeTransform(shape: Shape, w: number, h: number, rotate = 0) {\n  const vb = parseViewBox(resolveShape(shape).viewBox)\n  const f = (v: number) => Number(v.toFixed(4))\n  const turn = rotate ? `rotate(${f(rotate)}) ` : \"\"\n  return `${turn}translate(${f(-w / 2)} ${f(-h / 2)}) scale(${f(w / vb.w)} ${f(h / vb.h)}) translate(${f(-vb.x)} ${f(-vb.y)})`\n}\n\n/** The shape as a CSS mask (a data URI), for pieces drawn as boxes with a background. */\nexport function shapeMask(shape: Shape) {\n  const { d, viewBox } = resolveShape(shape)\n  const svg = `<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"${viewBox}\" preserveAspectRatio=\"xMidYMid meet\"><path d=\"${d}\"/></svg>`\n  return `url(\"data:image/svg+xml,${encodeURIComponent(svg)}\") center / contain no-repeat`\n}\n\n/* -------------------------------------------------------------------------------------------------\n * Outline: sampled once per path, cached\n * -----------------------------------------------------------------------------------------------*/\n\n/** Points read along the outline: enough for a five-point star's tips at hero sizes. */\nconst SAMPLES = 120\n/** Rows of the width profile (float-line's waterline crossing). */\nexport const PROFILE_ROWS = 48\n\nexport interface ShapeOutline {\n  /** Outline points in a box whose larger side is 1, centred on the origin (y down). */\n  points: Vec[]\n  /** Box size in the same units (the larger side is 1). */\n  w: number\n  h: number\n  /** Half width at PROFILE_ROWS + 1 evenly spaced rows, top to bottom (0 where the row misses). */\n  halfWidths: number[]\n  /** True when the points come from the real path (false: the bounding box, before hydration). */\n  sampled: boolean\n}\n\nconst outlines = new Map<string, ShapeOutline>()\n\nfunction profile(points: Vec[], h: number) {\n  return Array.from({ length: PROFILE_ROWS + 1 }, (_, i) => {\n    const y = -h / 2 + (h * i) / PROFILE_ROWS\n    let lo = Infinity\n    let hi = -Infinity\n    points.forEach((a, j) => {\n      const b = points[(j + 1) % points.length] ?? a\n      if ((a.y - y) * (b.y - y) > 0 || a.y === b.y) return\n      const x = a.x + ((y - a.y) / (b.y - a.y)) * (b.x - a.x)\n      lo = Math.min(lo, x)\n      hi = Math.max(hi, x)\n    })\n    return hi > lo ? (hi - lo) / 2 : 0\n  })\n}\n\nfunction boxOutline(w: number, h: number): ShapeOutline {\n  const points = [\n    { x: -w / 2, y: -h / 2 },\n    { x: w / 2, y: -h / 2 },\n    { x: w / 2, y: h / 2 },\n    { x: -w / 2, y: h / 2 },\n  ]\n  return { points, w, h, halfWidths: profile(points, h), sampled: false }\n}\n\n/**\n * The outline of a shape, normalised (larger side 1, centred). Read once with `getPointAtLength`\n * and cached; on the server or where SVG geometry is missing (jsdom) it is the bounding box.\n */\nexport function shapeOutline(shape: Shape): ShapeOutline {\n  const key = shapeKey(shape)\n  const hit = outlines.get(key)\n  if (hit) return hit\n  const { d, viewBox } = resolveShape(shape)\n  const vb = parseViewBox(viewBox)\n  const m = Math.max(vb.w, vb.h)\n  const w = vb.w / m\n  const h = vb.h / m\n  if (typeof document === \"undefined\") return boxOutline(w, h)\n  let out = boxOutline(w, h)\n  const ns = \"http://www.w3.org/2000/svg\"\n  const svg = document.createElementNS(ns, \"svg\")\n  try {\n    const path = document.createElementNS(ns, \"path\")\n    path.setAttribute(\"d\", d)\n    svg.setAttribute(\"aria-hidden\", \"true\")\n    svg.style.cssText = \"position:absolute;width:0;height:0;overflow:hidden;visibility:hidden\"\n    svg.appendChild(path)\n    document.body.appendChild(svg)\n    const len = typeof path.getTotalLength === \"function\" ? path.getTotalLength() : 0\n    if (len > 0) {\n      const points = Array.from({ length: SAMPLES }, (_, i) => {\n        const p = path.getPointAtLength((len * i) / SAMPLES)\n        return { x: (p.x - vb.x) / m - w / 2, y: (p.y - vb.y) / m - h / 2 }\n      })\n      out = { points, w, h, halfWidths: profile(points, h), sampled: true }\n    }\n  } catch {\n    // No SVG geometry here: the bounding box stands in.\n  } finally {\n    svg.remove()\n  }\n  outlines.set(key, out)\n  return out\n}\n\n/** Half width of a sampled outline at `dy` from its centre (same units as `h`), or null outside. */\nexport function halfWidthAt(outline: ShapeOutline, h: number, dy: number) {\n  const t = (dy / h + 0.5) * PROFILE_ROWS\n  if (t < 0 || t > PROFILE_ROWS) return null\n  const i = Math.min(PROFILE_ROWS - 1, Math.floor(t))\n  const a = outline.halfWidths[i] ?? 0\n  const b = outline.halfWidths[i + 1] ?? 0\n  const v = (a + (b - a) * (t - i)) * (h / outline.h)\n  return v > 0 ? v : null\n}\n",
      "type": "registry:lib"
    }
  ],
  "meta": {
    "tier": "free",
    "collection": "interface",
    "needsImage": false,
    "addedAt": "2026-11-01",
    "tags": [
      "shape",
      "path",
      "svg",
      "geometry"
    ],
    "tech": [],
    "presets": [],
    "keyboard": [],
    "heavy": false
  },
  "categories": [
    "internal"
  ],
  "type": "registry:lib"
}