{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-circuit",
  "type": "registry:component",
  "title": "Circuit Traces",
  "description": "A procedurally routed circuit board with 45-degree traces, pads and chip packages. Pulses of light travel along the traces and flash the pad they arrive at.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "circuit",
    "pcb",
    "pulses",
    "canvas",
    "tech"
  ],
  "docs": "https://hyperblur.dev/effects/bg-circuit",
  "files": [
    {
      "path": "registry/hyperblur/bg-circuit.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-circuit.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgCircuitProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  background?: string;\n  pitch?: number;\n  pulses?: number;\n  chips?: boolean;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-ve6 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-ve6-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ve6-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction rgbOf(value: string, fallback: [number, number, number]): [number, number, number] {\n  const raw = value.trim().replace(\"#\", \"\");\n  const hex = raw.length === 3 ? raw.split(\"\").map((c) => c + c).join(\"\") : raw;\n  const n = parseInt(hex.slice(0, 6), 16);\n  if (hex.length < 6 || Number.isNaN(n)) return fallback;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nconst DIRS: [number, number][] = [\n  [1, 0],\n  [1, 1],\n  [0, 1],\n  [-1, 1],\n  [-1, 0],\n  [-1, -1],\n  [0, -1],\n  [1, -1],\n];\n\ninterface Trace {\n  xs: number[];\n  ys: number[];\n  acc: number[];\n  len: number;\n  flash: number;\n}\n\ninterface Pulse {\n  trace: number;\n  s: number;\n  v: number;\n  wait: number;\n}\n\nexport function BgCircuit({\n  color = \"#1d2636\",\n  accent = \"#2f7bff\",\n  background = \"#05070b\",\n  pitch = 22,\n  pulses = 1,\n  chips = true,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgCircuitProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !canvas || !ctx) return;\n    const g: CanvasRenderingContext2D = ctx;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n    const layer = document.createElement(\"canvas\");\n    const lctx = layer.getContext(\"2d\");\n    if (!lctx) return;\n    const L: CanvasRenderingContext2D = lctx;\n\n    const lc = rgbOf(color, [29, 38, 54]);\n    const ac = rgbOf(accent, [47, 123, 255]);\n    const glow = document.createElement(\"canvas\");\n    glow.width = 64;\n    glow.height = 64;\n    const gc = glow.getContext(\"2d\");\n    if (gc) {\n      const rg = gc.createRadialGradient(32, 32, 0, 32, 32, 32);\n      rg.addColorStop(0, `rgba(${ac[0]},${ac[1]},${ac[2]},0.9)`);\n      rg.addColorStop(0.25, `rgba(${ac[0]},${ac[1]},${ac[2]},0.35)`);\n      rg.addColorStop(1, `rgba(${ac[0]},${ac[1]},${ac[2]},0)`);\n      gc.fillStyle = rg;\n      gc.fillRect(0, 0, 64, 64);\n    }\n\n    let seed = 0xc1c;\n    function rand() {\n      seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;\n      return seed / 4294967296;\n    }\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let traces: Trace[] = [];\n    let list: Pulse[] = [];\n\n    function build(dpr: number) {\n      seed = 0xc1c;\n      const p = Math.max(12, pitch);\n      const cols = Math.ceil(w / p) + 1;\n      const rows = Math.ceil(h / p) + 1;\n      const occ = new Uint8Array(cols * rows);\n      const ox = (w - (cols - 1) * p) / 2;\n      const oy = (h - (rows - 1) * p) / 2;\n      const pins: [number, number, number][] = [];\n      const boxes: [number, number, number, number][] = [];\n      if (chips) {\n        const count = Math.max(1, Math.round((cols * rows) / 260));\n        for (let k = 0; k < count * 4 && boxes.length < count; k++) {\n          const bw = 3 + Math.floor(rand() * 3);\n          const bh = 2 + Math.floor(rand() * 3);\n          const bx = 1 + Math.floor(rand() * Math.max(1, cols - bw - 2));\n          const by = 1 + Math.floor(rand() * Math.max(1, rows - bh - 2));\n          let free = true;\n          for (let y = by - 1; y <= by + bh && free; y++) for (let x = bx - 1; x <= bx + bw && free; x++) if (occ[y * cols + x]) free = false;\n          if (!free) continue;\n          for (let y = by; y < by + bh; y++) for (let x = bx; x < bx + bw; x++) occ[y * cols + x] = 1;\n          boxes.push([bx, by, bw, bh]);\n          for (let x = bx; x < bx + bw; x++) {\n            pins.push([x, by - 1, 6]);\n            pins.push([x, by + bh, 2]);\n          }\n          for (let y = by; y < by + bh; y++) {\n            pins.push([bx - 1, y, 4]);\n            pins.push([bx + bw, y, 0]);\n          }\n        }\n      }\n      traces = [];\n      const attempts = Math.round((cols * rows) / 5);\n      for (let a = 0; a < attempts; a++) {\n        let cx: number;\n        let cy: number;\n        let dir: number;\n        if (pins.length && rand() < 0.35) {\n          const pin = pins[Math.floor(rand() * pins.length)];\n          cx = pin[0];\n          cy = pin[1];\n          dir = pin[2];\n        } else {\n          cx = Math.floor(rand() * cols);\n          cy = Math.floor(rand() * rows);\n          dir = Math.floor(rand() * 4) * 2;\n        }\n        if (cx < 0 || cy < 0 || cx >= cols || cy >= rows || occ[cy * cols + cx]) continue;\n        const cells: [number, number][] = [[cx, cy]];\n        const target = 3 + Math.floor(rand() * 12);\n        const taken = [cy * cols + cx];\n        occ[cy * cols + cx] = 1;\n        for (let s = 0; s < target; s++) {\n          let nd = dir;\n          const r = rand();\n          if (r < 0.14) nd = (dir + 1) % 8;\n          else if (r < 0.28) nd = (dir + 7) % 8;\n          const [dx, dy] = DIRS[nd];\n          const nx = cx + dx;\n          const ny = cy + dy;\n          if (nx < 0 || ny < 0 || nx >= cols || ny >= rows || occ[ny * cols + nx]) break;\n          if (dx && dy && occ[cy * cols + nx] && occ[ny * cols + cx]) break;\n          occ[ny * cols + nx] = 1;\n          taken.push(ny * cols + nx);\n          cells.push([nx, ny]);\n          cx = nx;\n          cy = ny;\n          dir = nd;\n        }\n        if (cells.length < 3) {\n          if (cells.length < 2) for (const i of taken) occ[i] = 0;\n          continue;\n        }\n        const xs = cells.map((c) => ox + c[0] * p);\n        const ys = cells.map((c) => oy + c[1] * p);\n        const acc = [0];\n        for (let i = 1; i < xs.length; i++) acc.push(acc[i - 1] + Math.hypot(xs[i] - xs[i - 1], ys[i] - ys[i - 1]));\n        traces.push({ xs, ys, acc, len: acc[acc.length - 1], flash: -9 });\n      }\n\n      layer.width = Math.max(1, Math.round(w * dpr));\n      layer.height = Math.max(1, Math.round(h * dpr));\n      L.setTransform(dpr, 0, 0, dpr, 0, 0);\n      L.clearRect(0, 0, w, h);\n      L.fillStyle = `rgba(${lc[0]},${lc[1]},${lc[2]},0.5)`;\n      for (let y = 0; y < rows; y++) for (let x = 0; x < cols; x++) if (!occ[y * cols + x] && (x + y) % 2 === 0) L.fillRect(ox + x * p - 0.5, oy + y * p - 0.5, 1, 1);\n      L.lineCap = \"round\";\n      L.lineJoin = \"round\";\n      L.lineWidth = 1.5;\n      L.strokeStyle = `rgb(${lc[0]},${lc[1]},${lc[2]})`;\n      for (const tr of traces) {\n        L.beginPath();\n        L.moveTo(tr.xs[0], tr.ys[0]);\n        for (let i = 1; i < tr.xs.length; i++) L.lineTo(tr.xs[i], tr.ys[i]);\n        L.stroke();\n      }\n      L.fillStyle = background;\n      for (const tr of traces) {\n        for (const i of [0, tr.xs.length - 1]) {\n          L.beginPath();\n          L.arc(tr.xs[i], tr.ys[i], 2.8, 0, 6.2832);\n          L.fill();\n          L.stroke();\n        }\n      }\n      for (const [bx, by, bw, bh] of boxes) {\n        const x = ox + bx * p - p * 0.42;\n        const y = oy + by * p - p * 0.42;\n        const ww = (bw - 1) * p + p * 0.84;\n        const hh = (bh - 1) * p + p * 0.84;\n        const lg = L.createLinearGradient(x, y, x, y + hh);\n        lg.addColorStop(0, `rgb(${Math.min(255, lc[0] + 10)},${Math.min(255, lc[1] + 12)},${Math.min(255, lc[2] + 16)})`);\n        lg.addColorStop(1, `rgb(${lc[0] >> 1},${lc[1] >> 1},${lc[2] >> 1})`);\n        L.fillStyle = lg;\n        L.beginPath();\n        L.roundRect(x, y, ww, hh, 3);\n        L.fill();\n        L.strokeStyle = `rgba(${ac[0]},${ac[1]},${ac[2]},0.28)`;\n        L.lineWidth = 1;\n        L.stroke();\n        L.fillStyle = `rgba(${ac[0]},${ac[1]},${ac[2]},0.4)`;\n        L.beginPath();\n        L.arc(x + 6, y + 6, 1.6, 0, 6.2832);\n        L.fill();\n        L.strokeStyle = `rgb(${lc[0]},${lc[1]},${lc[2]})`;\n        L.lineWidth = 1.5;\n      }\n\n      const count = Math.max(1, Math.round(traces.length * 0.28 * pulses));\n      list = [];\n      for (let i = 0; i < count && traces.length; i++) {\n        const ti = Math.floor(rand() * traces.length);\n        list.push({ trace: ti, s: rand() * traces[ti].len, v: 50 + rand() * 90, wait: 0 });\n      }\n    }\n\n    function at(tr: Trace, s: number): [number, number] {\n      if (s <= 0) return [tr.xs[0], tr.ys[0]];\n      if (s >= tr.len) return [tr.xs[tr.xs.length - 1], tr.ys[tr.ys.length - 1]];\n      let i = 1;\n      while (i < tr.acc.length - 1 && tr.acc[i] < s) i++;\n      const k = (s - tr.acc[i - 1]) / (tr.acc[i] - tr.acc[i - 1] || 1);\n      return [tr.xs[i - 1] + (tr.xs[i] - tr.xs[i - 1]) * k, tr.ys[i - 1] + (tr.ys[i] - tr.ys[i - 1]) * k];\n    }\n\n    function render(dt: number) {\n      if (!w || !h) return;\n      g.clearRect(0, 0, w, h);\n      g.drawImage(layer, 0, 0, w, h);\n      g.globalCompositeOperation = \"lighter\";\n      g.lineCap = \"round\";\n      const tail = 46;\n      for (const pu of list) {\n        if (pu.wait > 0) {\n          pu.wait -= dt;\n          continue;\n        }\n        const tr = traces[pu.trace];\n        if (!tr) continue;\n        const before = pu.s;\n        pu.s += pu.v * dt;\n        if (before < tr.len && pu.s >= tr.len) tr.flash = t;\n        if (pu.s - tail > tr.len) {\n          pu.trace = Math.floor(rand() * traces.length);\n          pu.s = 0;\n          pu.v = 50 + rand() * 90;\n          pu.wait = rand() * 1.4;\n          continue;\n        }\n        const steps = 10;\n        let [px, py] = at(tr, pu.s - tail);\n        for (let k = 1; k <= steps; k++) {\n          const [qx, qy] = at(tr, pu.s - tail + (tail * k) / steps);\n          g.strokeStyle = `rgba(${ac[0]},${ac[1]},${ac[2]},${((k / steps) ** 2 * 0.95).toFixed(3)})`;\n          g.lineWidth = 1 + (k / steps) * 1.2;\n          g.beginPath();\n          g.moveTo(px, py);\n          g.lineTo(qx, qy);\n          g.stroke();\n          px = qx;\n          py = qy;\n        }\n        if (pu.s <= tr.len) {\n          g.drawImage(glow, px - 14, py - 14, 28, 28);\n          g.fillStyle = \"rgba(255,255,255,0.9)\";\n          g.beginPath();\n          g.arc(px, py, 1.3, 0, 6.2832);\n          g.fill();\n        }\n      }\n      for (const tr of traces) {\n        const age = t - tr.flash;\n        if (age < 0 || age > 1.1) continue;\n        const k = 1 - age / 1.1;\n        const ex = tr.xs[tr.xs.length - 1];\n        const ey = tr.ys[tr.ys.length - 1];\n        g.globalAlpha = k;\n        g.drawImage(glow, ex - 18, ey - 18, 36, 36);\n        g.strokeStyle = `rgba(${ac[0]},${ac[1]},${ac[2]},${(k * 0.9).toFixed(3)})`;\n        g.lineWidth = 1.2;\n        g.beginPath();\n        g.arc(ex, ey, 2.8 + (1 - k) * 7, 0, 6.2832);\n        g.stroke();\n        g.globalAlpha = 1;\n      }\n      g.globalCompositeOperation = \"source-over\";\n    }\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function resize() {\n      const nw = el.clientWidth;\n      const nh = el.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      cv.width = Math.max(1, Math.round(w * dpr));\n      cv.height = Math.max(1, Math.round(h * dpr));\n      g.setTransform(dpr, 0, 0, dpr, 0, 0);\n      build(dpr);\n      render(0);\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.05, (now - prev) / 1000) * speed;\n      prev = now;\n      t += dt;\n      render(dt);\n      raf = requestAnimationFrame(frame);\n    }\n\n    function sync() {\n      const run = visible && !document.hidden && !motion.matches;\n      if (run && !raf) {\n        prev = performance.now();\n        raf = requestAnimationFrame(frame);\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    }\n\n    const ro = new ResizeObserver(resize);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      sync();\n    });\n    ro.observe(el);\n    io.observe(el);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    resize();\n    sync();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", sync);\n      motion.removeEventListener(\"change\", sync);\n    };\n  }, [color, accent, background, pitch, pulses, chips, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ve6 ${className}`} style={{ background, ...style }} {...rest}>\n      <style href=\"hb-bg-circuit\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ve6-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-ve6-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}