{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "path-search",
  "title": "Path Search",
  "description": "A pathfinding search floods a grid and traces the route from start to goal.",
  "dependencies": ["remotion", "@remotion/google-fonts", "@remotion/paths"],
  "files": [
    {
      "path": "registry/remocn/path-search/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/GeistMono\";\nimport { evolvePath } from \"@remotion/paths\";\nimport type React from \"react\";\nimport { useId, useMemo } from \"react\";\nimport {\n  AbsoluteFill,\n  Easing,\n  interpolate,\n  spring,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst { fontFamily: mono } = loadFont(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\nexport interface PathSearchWall {\n  col: number;\n  row: number;\n  w: number;\n  h: number;\n}\n\nexport interface PathSearchProps {\n  cols?: number;\n  rows?: number;\n  start?: [number, number];\n  goal?: [number, number];\n  walls?: PathSearchWall[];\n  startLabel?: string;\n  goalLabel?: string;\n  accent?: string;\n  theme?: \"dark\" | \"light\";\n  speed?: number;\n}\n\nconst DEFAULT_WALLS: PathSearchWall[] = [\n  { col: 4, row: 0, w: 1, h: 6 },\n  { col: 8, row: 3, w: 1, h: 6 },\n  { col: 12, row: 0, w: 1, h: 5 },\n  { col: 9, row: 3, w: 2, h: 1 },\n];\n\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\n\nconst MARK_AT = 10;\nconst FLOOD_AT = 22;\nconst STEP = 1.45;\nconst TRACE = 32;\n\nconst THEMES = {\n  dark: {\n    bg: \"#0A0A0B\",\n    fg: \"#F3F2EF\",\n    muted: \"#8B8A90\",\n    grid: \"rgba(255,255,255,0.075)\",\n    frame: \"rgba(255,255,255,0.16)\",\n    wall: \"#2A2A2F\",\n    wallEdge: \"rgba(255,255,255,0.08)\",\n    tint: 0.42,\n    front: 0.95,\n    residue: 0.2,\n    grain: 0.055,\n    // gradients.sh \"beam\" (+ limb violet): violet at the start, azure at the front.\n    ramp: [\"#5a2fe0\", \"#3d5bff\", \"#2f7bff\", \"#3aa0ff\"],\n    route: [\"#3d5bff\", \"#6f8dff\", \"#b9cdff\"],\n    ground: \"rgba(61,91,255,0.2)\",\n  },\n  light: {\n    bg: \"#F5F4F0\",\n    fg: \"#121214\",\n    muted: \"#6E6C67\",\n    grid: \"rgba(18,18,20,0.08)\",\n    frame: \"rgba(18,18,20,0.18)\",\n    wall: \"#D2CFC6\",\n    wallEdge: \"rgba(18,18,20,0.1)\",\n    tint: 0.34,\n    front: 0.8,\n    residue: 0.36,\n    grain: 0.04,\n    // gradients.sh \"pixel-glass\" (light): blue → violet → pink.\n    ramp: [\"#3d6bff\", \"#7a3cff\", \"#b545e0\", \"#ff4fb8\"],\n    route: [\"#3d6bff\", \"#7a3cff\", \"#ff4fb8\"],\n    ground: \"rgba(122,60,255,0.09)\",\n  },\n} as const;\n\ntype Cell = [number, number];\n\nfunction hexRgb(hex: string): [number, number, number] {\n  const n = Number.parseInt(hex.slice(1), 16);\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\n/** Sample an evenly spaced colour ramp at k ∈ [0, 1]. */\nfunction sampleRamp(ramp: readonly string[], k: number) {\n  const x = Math.min(1, Math.max(0, k)) * (ramp.length - 1);\n  const i = Math.min(ramp.length - 2, Math.floor(x));\n  const f = x - i;\n  const a = hexRgb(ramp[i]);\n  const b = hexRgb(ramp[i + 1]);\n  const c = a.map((v, j) => Math.round(v + (b[j] - v) * f));\n  return `rgb(${c[0]},${c[1]},${c[2]})`;\n}\n\nconst DIRS: Cell[] = [\n  [1, 0],\n  [0, 1],\n  [-1, 0],\n  [0, -1],\n];\n\nfunction search(\n  cols: number,\n  rows: number,\n  start: Cell,\n  goal: Cell,\n  walls: PathSearchWall[],\n) {\n  const blocked = new Set<number>();\n  for (const w of walls) {\n    for (let c = w.col; c < w.col + w.w; c++) {\n      for (let r = w.row; r < w.row + w.h; r++) blocked.add(r * cols + c);\n    }\n  }\n  const idx = (c: number, r: number) => r * cols + c;\n  const distArr = new Array<number>(cols * rows).fill(-1);\n  const s = idx(start[0], start[1]);\n  const g = idx(goal[0], goal[1]);\n  blocked.delete(s);\n  blocked.delete(g);\n  distArr[s] = 0;\n  const queue = [s];\n  for (let qi = 0; qi < queue.length; qi++) {\n    const cur = queue[qi];\n    if (cur === g) break;\n    const c = cur % cols;\n    const r = Math.floor(cur / cols);\n    for (const [dc, dr] of DIRS) {\n      const nc = c + dc;\n      const nr = r + dr;\n      if (nc < 0 || nr < 0 || nc >= cols || nr >= rows) continue;\n      const ni = idx(nc, nr);\n      if (blocked.has(ni) || distArr[ni] >= 0) continue;\n      distArr[ni] = distArr[cur] + 1;\n      queue.push(ni);\n    }\n  }\n  const dGoal = distArr[g];\n  const path: Cell[] = [];\n  if (dGoal > 0) {\n    let c = goal[0];\n    let r = goal[1];\n    let dir: Cell = [0, 0];\n    path.push([c, r]);\n    while (distArr[idx(c, r)] > 0) {\n      const here = distArr[idx(c, r)];\n      const options = DIRS.filter(([dc, dr]) => {\n        const nc = c + dc;\n        const nr = r + dr;\n        if (nc < 0 || nr < 0 || nc >= cols || nr >= rows) return false;\n        return distArr[idx(nc, nr)] === here - 1;\n      });\n      const keep = options.find(([dc, dr]) => dc === dir[0] && dr === dir[1]);\n      dir = keep ?? options[0];\n      c += dir[0];\n      r += dir[1];\n      path.push([c, r]);\n    }\n  }\n  return { blocked, distArr, dGoal, path };\n}\n\nfunction roundedPath(pts: Cell[], radius: number) {\n  if (pts.length < 2) return \"\";\n  let d = `M${pts[0][0].toFixed(2)} ${pts[0][1].toFixed(2)}`;\n  for (let i = 1; i < pts.length - 1; i++) {\n    const [px, py] = pts[i - 1];\n    const [x, y] = pts[i];\n    const [nx, ny] = pts[i + 1];\n    const l1 = Math.hypot(x - px, y - py) || 1;\n    const l2 = Math.hypot(nx - x, ny - y) || 1;\n    const r = Math.min(radius, l1 / 2, l2 / 2);\n    const ax = x - ((x - px) / l1) * r;\n    const ay = y - ((y - py) / l1) * r;\n    const bx = x + ((nx - x) / l2) * r;\n    const by = y + ((ny - y) / l2) * r;\n    d += ` L${ax.toFixed(2)} ${ay.toFixed(2)} Q${x.toFixed(2)} ${y.toFixed(2)} ${bx.toFixed(2)} ${by.toFixed(2)}`;\n  }\n  const last = pts[pts.length - 1];\n  d += ` L${last[0].toFixed(2)} ${last[1].toFixed(2)}`;\n  return d;\n}\n\nfunction simplify(cells: Cell[]) {\n  if (cells.length < 3) return cells;\n  const out: Cell[] = [cells[0]];\n  for (let i = 1; i < cells.length - 1; i++) {\n    const [ax, ay] = cells[i - 1];\n    const [bx, by] = cells[i];\n    const [cx, cy] = cells[i + 1];\n    if ((bx - ax) * (cy - by) - (by - ay) * (cx - bx) !== 0) out.push(cells[i]);\n  }\n  out.push(cells[cells.length - 1]);\n  return out;\n}\n\nfunction Grain({ opacity }: { opacity: number }) {\n  const id = `g${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n  return (\n    <svg\n      width=\"100%\"\n      height=\"100%\"\n      style={{ position: \"absolute\", inset: 0, opacity, pointerEvents: \"none\" }}\n      aria-hidden\n    >\n      <filter id={id}>\n        <feTurbulence\n          type=\"fractalNoise\"\n          baseFrequency=\"0.9\"\n          numOctaves={2}\n          seed={29}\n          stitchTiles=\"stitch\"\n        />\n        <feColorMatrix values=\"0 0 0 0 0.5  0 0 0 0 0.5  0 0 0 0 0.5  0 0 0 0.9 0\" />\n      </filter>\n      <rect width=\"100%\" height=\"100%\" filter={`url(#${id})`} />\n    </svg>\n  );\n}\n\nexport function PathSearch({\n  cols = 17,\n  rows = 9,\n  start = [1, 4],\n  goal = [15, 4],\n  walls = DEFAULT_WALLS,\n  startLabel = \"Start\",\n  goalLabel = \"Goal\",\n  accent = \"#3B5BFD\",\n  theme = \"dark\",\n  speed = 1,\n}: PathSearchProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H, fps } = useVideoConfig();\n  const u = Math.min(W / 1280, H / 720);\n  const p = THEMES[theme];\n  const routeId = `route${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n\n  const { blocked, distArr, dGoal, path } = useMemo(\n    () => search(cols, rows, start, goal, walls),\n    [cols, rows, start, goal, walls],\n  );\n\n  const cell = Math.min((W * 0.76) / cols, (H * 0.7) / rows);\n  const gw = cell * cols;\n  const gh = cell * rows;\n  const x0 = (W - gw) / 2;\n  const y0 = (H - gh) / 2;\n  const cx = (c: number) => x0 + (c + 0.5) * cell;\n  const cy = (r: number) => y0 + (r + 0.5) * cell;\n\n  const reach = FLOOD_AT + Math.max(0, dGoal) * STEP;\n  const traceAt = reach + 6;\n  const fadeAt = traceAt + TRACE - 6;\n  const doneAt = fadeAt + 22;\n\n  const gridIn = interpolate(t, [0, 14], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const trace = interpolate(t, [traceAt, traceAt + TRACE], [0, 1], {\n    ...CLAMP,\n    easing: EASE_IO,\n  });\n  const goalHit = spring({\n    frame: t - reach,\n    fps,\n    config: { damping: 14, stiffness: 180, mass: 0.6 },\n  });\n  const startHit = spring({\n    frame: t - (traceAt + TRACE - 2),\n    fps,\n    config: { damping: 14, stiffness: 180, mass: 0.6 },\n  });\n\n  const pathPts = simplify(path).map(([c, r]): Cell => [cx(c), cy(r)]);\n  const pathD = roundedPath(pathPts, cell * 0.5);\n  const evo = pathD ? evolvePath(trace, pathD) : null;\n  const strokeW = cell * 0.24;\n\n  const tints: React.ReactNode[] = [];\n  const inset = 3 * u;\n  for (let r = 0; r < rows; r++) {\n    for (let c = 0; c < cols; c++) {\n      const d = distArr[r * cols + c];\n      if (d < 0 || d > dGoal) continue;\n      const at = FLOOD_AT + d * STEP;\n      if (t < at) continue;\n      const k = t - at;\n      const rise = interpolate(k, [0, 4], [0, 1], {\n        ...CLAMP,\n        easing: EASE_OUT,\n      });\n      const band = Math.floor(d / 3) % 2 === 0 ? 1 : 0.78;\n      const decay = interpolate(k, [3, 16], [p.front, p.tint * band], {\n        ...CLAMP,\n        easing: EASE_OUT,\n      });\n      const fade = interpolate(\n        t,\n        [fadeAt + d * 0.35, fadeAt + d * 0.35 + 16],\n        [1, p.residue],\n        CLAMP,\n      );\n      const o = rise * decay * fade;\n      if (o <= 0.002) continue;\n      const s = 0.82 + 0.18 * rise;\n      const size = (cell - inset * 2) * s;\n      tints.push(\n        <rect\n          key={`${c}-${r}`}\n          x={cx(c) - size / 2}\n          y={cy(r) - size / 2}\n          width={size}\n          height={size}\n          rx={6 * u}\n          fill={sampleRamp(p.ramp, d / Math.max(1, dGoal))}\n          opacity={o}\n        />,\n      );\n    }\n  }\n\n  const gridLines: React.ReactNode[] = [];\n  for (let c = 1; c < cols; c++) {\n    gridLines.push(\n      <line\n        key={`v${c}`}\n        x1={x0 + c * cell}\n        y1={y0}\n        x2={x0 + c * cell}\n        y2={y0 + gh}\n        stroke={p.grid}\n        strokeWidth={Math.max(1, u)}\n      />,\n    );\n  }\n  for (let r = 1; r < rows; r++) {\n    gridLines.push(\n      <line\n        key={`h${r}`}\n        x1={x0}\n        y1={y0 + r * cell}\n        x2={x0 + gw}\n        y2={y0 + r * cell}\n        stroke={p.grid}\n        strokeWidth={Math.max(1, u)}\n      />,\n    );\n  }\n\n  const labelFor = (cellAt: Cell, text: string, at: number) => {\n    const onPath = new Set(path.map(([c, r]) => r * cols + c));\n    const options: Cell[] = [\n      [0, -1],\n      [0, 1],\n      [-1, 0],\n      [1, 0],\n    ];\n    const free = options.find(([dc, dr]) => {\n      const c = cellAt[0] + dc;\n      const r = cellAt[1] + dr;\n      if (c < 0 || r < 0 || c >= cols || r >= rows) return false;\n      const i = r * cols + c;\n      return !blocked.has(i) && !onPath.has(i);\n    }) ?? [0, -1];\n    const a = interpolate(t, [at, at + 14], [0, 1], {\n      ...CLAMP,\n      easing: EASE_OUT,\n    });\n    return (\n      <div\n        key={text}\n        style={{\n          position: \"absolute\",\n          left: cx(cellAt[0] + free[0]) - cell,\n          top: cy(cellAt[1] + free[1]) - cell / 2,\n          width: cell * 2,\n          height: cell,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          fontFamily: mono,\n          fontWeight: 500,\n          fontSize: 14 * u,\n          letterSpacing: \"0.08em\",\n          textTransform: \"uppercase\",\n          color: p.fg,\n          opacity: a,\n          transform: `translateY(${(1 - a) * 6 * u}px)`,\n          whiteSpace: \"nowrap\",\n        }}\n      >\n        {text}\n      </div>\n    );\n  };\n\n  const markerR = cell * 0.2;\n  const startPop = spring({\n    frame: t - MARK_AT,\n    fps,\n    config: { damping: 14, stiffness: 200, mass: 0.6 },\n  });\n  const goalPop = spring({\n    frame: t - MARK_AT - 3,\n    fps,\n    config: { damping: 14, stiffness: 200, mass: 0.6 },\n  });\n  const push = interpolate(t, [0, doneAt + 20], [0, 1], {\n    ...CLAMP,\n    easing: EASE_IO,\n  });\n\n  const drift = interpolate(t, [0, 150], [0, 1], CLAMP);\n  const routeX1 = cx(start[0]);\n  const routeX2 = cx(goal[0]) === routeX1 ? routeX1 + 1 : cx(goal[0]);\n\n  return (\n    <AbsoluteFill style={{ background: p.bg, overflow: \"hidden\" }}>\n      <div\n        style={{\n          position: \"absolute\",\n          inset: 0,\n          background: `radial-gradient(70% 60% at ${44 + 12 * drift}% 112%, ${p.ground} 0%, transparent 72%)`,\n        }}\n      />\n      <AbsoluteFill\n        style={{\n          transform: `scale(${0.985 + 0.025 * push})`,\n          transformOrigin: \"50% 50%\",\n        }}\n      >\n        <svg\n          width={W}\n          height={H}\n          style={{ position: \"absolute\", inset: 0 }}\n          aria-hidden\n        >\n          <defs>\n            <linearGradient\n              id={routeId}\n              gradientUnits=\"userSpaceOnUse\"\n              x1={routeX1}\n              y1={0}\n              x2={routeX2}\n              y2={0}\n            >\n              {[accent, ...p.route.slice(1)].map((c, i) => (\n                <stop key={c} offset={i / (p.route.length - 1)} stopColor={c} />\n              ))}\n            </linearGradient>\n          </defs>\n          <g opacity={gridIn}>\n            {gridLines}\n            <rect\n              x={x0}\n              y={y0}\n              width={gw}\n              height={gh}\n              rx={10 * u}\n              fill=\"none\"\n              stroke={p.frame}\n              strokeWidth={Math.max(1, u)}\n            />\n          </g>\n          {tints}\n          {walls.map((w, i) => {\n            const at = 3 + i * 3;\n            const a = interpolate(t, [at, at + 14], [0, 1], {\n              ...CLAMP,\n              easing: EASE_OUT,\n            });\n            const wx = x0 + w.col * cell + inset;\n            const wy = y0 + w.row * cell + inset;\n            const ww = w.w * cell - inset * 2;\n            const wh = w.h * cell - inset * 2;\n            const grow = 0.9 + 0.1 * a;\n            return (\n              <rect\n                key={`${w.col}-${w.row}-${w.w}-${w.h}`}\n                x={wx + (ww * (1 - grow)) / 2}\n                y={wy + (wh * (1 - grow)) / 2}\n                width={ww * grow}\n                height={wh * grow}\n                rx={7 * u}\n                fill={p.wall}\n                stroke={p.wallEdge}\n                strokeWidth={Math.max(1, u)}\n                opacity={a}\n              />\n            );\n          })}\n          {evo ? (\n            <path\n              d={pathD}\n              fill=\"none\"\n              stroke={`url(#${routeId})`}\n              strokeWidth={strokeW}\n              strokeLinecap=\"round\"\n              strokeLinejoin=\"round\"\n              strokeDasharray={evo.strokeDasharray}\n              strokeDashoffset={evo.strokeDashoffset}\n              opacity={trace > 0 ? 1 : 0}\n            />\n          ) : null}\n          <circle\n            cx={cx(goal[0])}\n            cy={cy(goal[1])}\n            r={markerR * (1.2 + 1.6 * goalHit)}\n            fill=\"none\"\n            stroke={p.route[p.route.length - 1]}\n            strokeWidth={1.5 * u}\n            opacity={t >= reach ? (1 - goalHit) * 0.8 : 0}\n          />\n          <circle\n            cx={cx(start[0])}\n            cy={cy(start[1])}\n            r={\n              markerR *\n              (0.7 + 0.3 * startPop) *\n              (1 + 0.12 * Math.sin(Math.PI * Math.min(1, startHit)))\n            }\n            fill={p.bg}\n            stroke={p.fg}\n            strokeWidth={3 * u}\n            opacity={Math.min(1, startPop * 1.5)}\n          />\n          <circle\n            cx={cx(goal[0])}\n            cy={cy(goal[1])}\n            r={\n              markerR *\n              (0.7 + 0.3 * goalPop) *\n              (1 + 0.12 * Math.sin(Math.PI * Math.min(1, goalHit)))\n            }\n            fill={p.bg}\n            stroke={p.fg}\n            strokeWidth={3 * u}\n            opacity={Math.min(1, goalPop * 1.5)}\n          />\n          <circle\n            cx={cx(goal[0])}\n            cy={cy(goal[1])}\n            r={markerR * 0.42 * goalPop}\n            fill={t >= reach ? p.route[p.route.length - 1] : p.fg}\n            opacity={Math.min(1, goalPop * 1.5)}\n          />\n        </svg>\n        {labelFor(start, startLabel, MARK_AT + 4)}\n        {labelFor(goal, goalLabel, MARK_AT + 7)}\n      </AbsoluteFill>\n      <AbsoluteFill\n        style={{\n          background: `radial-gradient(ellipse 80% 80% at 50% 50%, transparent 62%, ${p.bg} 100%)`,\n          opacity: 0.5,\n          pointerEvents: \"none\",\n        }}\n      />\n      <Grain opacity={p.grain} />\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/path-search.tsx"
    }
  ],
  "type": "registry:component"
}
