{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-rune-rings",
  "type": "registry:component",
  "title": "Rune Rings",
  "description": "Concentric glowing rings carrying orbiting glyph marks, each ring spinning at its own speed. The pointer tilts the whole system in 3D.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "rings",
    "glyphs",
    "3d",
    "canvas",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/bg-rune-rings",
  "files": [
    {
      "path": "registry/hyperblur/bg-rune-rings.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-rune-rings.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgRuneRingsProps extends React.HTMLAttributes<HTMLDivElement> {\n  accent?: string;\n  color?: string;\n  background?: string;\n  rings?: number;\n  tilt?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-qp3 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qp3-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qp3-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction rnd(i: number) {\n  let x = Math.imul(i ^ 0x9e3779b9, 0x85ebca6b);\n  x ^= x >>> 13;\n  x = Math.imul(x, 0xc2b2ae35);\n  x ^= x >>> 16;\n  return (x >>> 0) / 4294967296;\n}\n\ninterface Ring {\n  r: number;\n  spin: number;\n  ticks: number;\n  glyphs: number[][];\n  dashed: boolean;\n}\n\nfunction buildRings(count: number): Ring[] {\n  const out: Ring[] = [];\n  for (let k = 0; k < count; k++) {\n    const r = 0.3 + (0.7 * (k + 1)) / count;\n    const dir = k % 2 === 0 ? 1 : -1;\n    const spin = dir * (0.05 + 0.1 * rnd(k * 13 + 1)) * (1.4 - r * 0.6);\n    const ticks = Math.round(10 + r * 26);\n    const glyphs: number[][] = [];\n    for (let g = 0; g < ticks; g++) {\n      const seed = k * 977 + g * 31;\n      const strokes: number[] = [];\n      const n = 2 + Math.floor(rnd(seed) * 3);\n      for (let s = 0; s < n; s++) {\n        const pick = Math.floor(rnd(seed + s * 7 + 1) * 6);\n        const a = rnd(seed + s * 7 + 2);\n        if (pick === 0) strokes.push(-0.5, -1, -0.5, 1);\n        else if (pick === 1) strokes.push(0.5, -1, 0.5, 1);\n        else if (pick === 2) strokes.push(-0.5, -1 + a, 0.5, 1 - a);\n        else if (pick === 3) strokes.push(-0.5, 0, 0.5, -1 + a * 0.5);\n        else if (pick === 4) strokes.push(0, -1, 0, 1);\n        else strokes.push(-0.5, 1 - a, 0.5, 1);\n      }\n      glyphs.push(strokes);\n    }\n    out.push({ r, spin, ticks, glyphs, dashed: k % 3 === 1 });\n  }\n  return out;\n}\n\nexport function BgRuneRings({\n  accent = \"#2f7bff\",\n  color = \"#a9c8ff\",\n  background = \"#05060a\",\n  rings = 5,\n  tilt = 1,\n  speed = 1,\n  children,\n  className = \"\",\n  style,\n  ...rest\n}: BgRuneRingsProps) {\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 host = wrap?.parentElement;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !ctx) return;\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const list = buildRings(Math.max(2, Math.min(9, Math.round(rings))));\n    const SEG = 120;\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let inside = false;\n    let tx = 0;\n    let ty = 0;\n    let px = 0;\n    let py = 0;\n    let placed = false;\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n    let cx = 0;\n    let cy = 0;\n    let R = 1;\n    let rx = 0;\n    let ry = 0;\n    let cosX = 1;\n    let sinX = 0;\n    let cosY = 1;\n    let sinY = 0;\n    let outX = 0;\n    let outY = 0;\n    let outZ = 0;\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.3 * Math.sin(tt * 0.29));\n    }\n    function idleY(tt: number) {\n      return h * (0.5 + 0.3 * Math.sin(tt * 0.37 + 1.3));\n    }\n\n    function track(dt: number) {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed || dt === 0) {\n        px = gx;\n        py = gy;\n        placed = true;\n        return;\n      }\n      const k = 1 - Math.exp(-dt * (inside ? 5 : 1.6));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n    }\n\n    function project(x: number, y: number, z: number) {\n      const y1 = y * cosX - z * sinX;\n      const z1 = y * sinX + z * cosX;\n      const x2 = x * cosY + z1 * sinY;\n      const z2 = -x * sinY + z1 * cosY;\n      const f = 3.2 * R;\n      const s = f / (f + z2);\n      outX = cx + x2 * s;\n      outY = cy + y1 * s;\n      outZ = z2;\n    }\n\n    function draw() {\n      if (!w || !h) return;\n      cx = w / 2;\n      cy = h / 2;\n      R = Math.min(w * 0.46, h * 0.95);\n      const nx = (px / Math.max(1, w) - 0.5) * 2;\n      const ny = (py / Math.max(1, h) - 0.5) * 2;\n      rx = (0.95 + ny * 0.4) * tilt;\n      ry = nx * 0.55 * tilt;\n      cosX = Math.cos(rx);\n      sinX = Math.sin(rx);\n      cosY = Math.cos(ry);\n      sinY = Math.sin(ry);\n\n      ctx!.globalCompositeOperation = \"source-over\";\n      ctx!.globalAlpha = 1;\n      ctx!.fillStyle = background;\n      ctx!.fillRect(0, 0, w, h);\n\n      ctx!.globalCompositeOperation = \"lighter\";\n      const core = ctx!.createRadialGradient(cx, cy, 0, cx, cy, R * 0.9);\n      core.addColorStop(0, accent);\n      core.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx!.globalAlpha = 0.22 + 0.05 * Math.sin(t * 1.3);\n      ctx!.fillStyle = core;\n      ctx!.fillRect(0, 0, w, h);\n\n      ctx!.lineCap = \"round\";\n      for (let k = 0; k < list.length; k++) {\n        const ring = list[k];\n        const r = ring.r * R;\n        const rot = t * ring.spin;\n        ctx!.strokeStyle = accent;\n        ctx!.beginPath();\n        for (let i = 0; i <= SEG; i++) {\n          const a = (i / SEG) * Math.PI * 2;\n          project(Math.cos(a) * r, Math.sin(a) * r, 0);\n          if (i === 0) ctx!.moveTo(outX, outY);\n          else ctx!.lineTo(outX, outY);\n        }\n        ctx!.globalAlpha = 0.18;\n        ctx!.lineWidth = 5;\n        ctx!.stroke();\n        ctx!.strokeStyle = color;\n        ctx!.globalAlpha = 0.55;\n        ctx!.lineWidth = 1;\n        if (ring.dashed) ctx!.setLineDash([2, 6]);\n        ctx!.stroke();\n        ctx!.setLineDash([]);\n\n        const gs = Math.max(2.5, R * 0.028) * (0.8 + ring.r * 0.4);\n        ctx!.lineWidth = 1.1;\n        ctx!.beginPath();\n        for (let g = 0; g < ring.ticks; g++) {\n          const a = (g / ring.ticks) * Math.PI * 2 + rot;\n          const ca = Math.cos(a);\n          const sa = Math.sin(a);\n          const strokes = ring.glyphs[g];\n          const off = r + (k % 2 === 0 ? gs * 1.8 : -gs * 1.8);\n          for (let s = 0; s < strokes.length; s += 4) {\n            const u0 = strokes[s] * gs;\n            const v0 = strokes[s + 1] * gs;\n            const u1 = strokes[s + 2] * gs;\n            const v1 = strokes[s + 3] * gs;\n            project(ca * (off + v0) - sa * u0, sa * (off + v0) + ca * u0, 0);\n            ctx!.moveTo(outX, outY);\n            project(ca * (off + v1) - sa * u1, sa * (off + v1) + ca * u1, 0);\n            ctx!.lineTo(outX, outY);\n          }\n        }\n        ctx!.strokeStyle = color;\n        ctx!.globalAlpha = 0.75;\n        ctx!.stroke();\n\n        const ma = rot * 2.3 + k;\n        project(Math.cos(ma) * r, Math.sin(ma) * r, 0);\n        const depth = 0.65 + 0.35 * Math.max(-1, Math.min(1, -outZ / Math.max(1, r)));\n        const glow = ctx!.createRadialGradient(outX, outY, 0, outX, outY, gs * 5);\n        glow.addColorStop(0, color);\n        glow.addColorStop(1, \"rgba(0,0,0,0)\");\n        ctx!.globalAlpha = 0.8 * depth;\n        ctx!.fillStyle = glow;\n        ctx!.fillRect(outX - gs * 5, outY - gs * 5, gs * 10, gs * 10);\n      }\n\n      ctx!.globalAlpha = 0.9;\n      ctx!.fillStyle = color;\n      ctx!.beginPath();\n      project(0, 0, 0);\n      ctx!.arc(outX, outY, Math.max(1.5, R * 0.012), 0, Math.PI * 2);\n      ctx!.fill();\n      ctx!.globalAlpha = 1;\n      ctx!.globalCompositeOperation = \"source-over\";\n    }\n\n    function still() {\n      t = 4;\n      inside = false;\n      placed = false;\n      track(0);\n      draw();\n    }\n\n    function resize() {\n      if (!wrap || !canvas || !ctx) return;\n      const nw = wrap.clientWidth;\n      const nh = wrap.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      if (motion.matches) still();\n      else {\n        track(0);\n        draw();\n      }\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.033, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      track(dt);\n      draw();\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      if (motion.matches) still();\n    }\n\n    function move(e: PointerEvent) {\n      if (!host || motion.matches) return;\n      const rect = host.getBoundingClientRect();\n      const sx = rect.width ? host.clientWidth / rect.width : 1;\n      const sy = rect.height ? host.clientHeight / rect.height : 1;\n      tx = (e.clientX - rect.left) * sx;\n      ty = (e.clientY - rect.top) * sy;\n      inside = true;\n    }\n\n    function leave(e: PointerEvent) {\n      if (e.type === \"pointerup\" && e.pointerType === \"mouse\") return;\n      inside = false;\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(wrap);\n    io.observe(wrap);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    host.addEventListener(\"pointermove\", move, { passive: true });\n    host.addEventListener(\"pointerdown\", move, { passive: true });\n    host.addEventListener(\"pointerleave\", leave);\n    host.addEventListener(\"pointerup\", leave);\n    host.addEventListener(\"pointercancel\", leave);\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      host.removeEventListener(\"pointermove\", move);\n      host.removeEventListener(\"pointerdown\", move);\n      host.removeEventListener(\"pointerleave\", leave);\n      host.removeEventListener(\"pointerup\", leave);\n      host.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [accent, color, background, rings, tilt, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qp3 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-rune-rings\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qp3-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-qp3-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}