{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-galaxy",
  "type": "registry:component",
  "title": "Galaxy",
  "description": "A tilted spiral galaxy of thousands of twinkling stars with a warm core and dark dust lanes, turning slowly. The pointer shifts the depth layers and parts the stars around it.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "galaxy",
    "stars",
    "space",
    "parallax",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/bg-galaxy",
  "files": [
    {
      "path": "registry/hyperblur/bg-galaxy.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-galaxy.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgGalaxyProps extends React.HTMLAttributes<HTMLDivElement> {\n  stars?: number;\n  arms?: number;\n  twist?: number;\n  accent?: string;\n  coreColor?: string;\n  tilt?: number;\n  parallax?: number;\n  spread?: number;\n  speed?: number;\n  seed?: number;\n}\n\nconst css = `\n.hb-qq1 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qq1-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qq1-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction qq1Hash(n: number) {\n  let x = n | 0;\n  x = (x ^ 61) ^ (x >>> 16);\n  x = Math.imul(x, 9);\n  x ^= x >>> 4;\n  x = Math.imul(x, 0x27d4eb2d);\n  x ^= x >>> 15;\n  return (x >>> 0) / 4294967296;\n}\n\nexport function BgGalaxy({\n  stars = 4200,\n  arms = 2,\n  twist = 3.4,\n  accent = \"#7fb3ff\",\n  coreColor = \"#ffd7a3\",\n  tilt = 0.56,\n  parallax = 16,\n  spread = 24,\n  speed = 1,\n  seed = 7,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgGalaxyProps) {\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 TAU = Math.PI * 2;\n    const armCount = Math.max(1, Math.round(arms));\n    const total = Math.max(200, Math.min(12000, Math.round(stars)));\n    const incline = Math.min(1, Math.max(0.15, tilt));\n    const palette = [coreColor, \"#fff4e6\", \"#eef2ff\", accent, \"#ffb0cf\"];\n\n    const sr = new Float32Array(total);\n    const sa = new Float32Array(total);\n    const sz = new Float32Array(total);\n    const ss = new Float32Array(total);\n    const sb = new Float32Array(total);\n    const sp = new Float32Array(total);\n    const sw = new Float32Array(total);\n    const order = new Uint16Array(total);\n    const bucketEnd = new Int32Array(palette.length);\n\n    const buckets: number[][] = palette.map(() => []);\n    for (let i = 0; i < total; i++) {\n      const k = (seed | 0) * 7919 + i * 13;\n      const r1 = qq1Hash(k + 1);\n      const r2 = qq1Hash(k + 2);\n      const r3 = qq1Hash(k + 3);\n      const r4 = qq1Hash(k + 4);\n      const r5 = qq1Hash(k + 5);\n      const r6 = qq1Hash(k + 6);\n      let r: number;\n      let a: number;\n      let bucket: number;\n      if (r1 < 0.2) {\n        r = 0.2 * Math.pow(r2, 1.6);\n        a = r3 * TAU;\n        bucket = r4 < 0.7 ? 0 : 1;\n      } else {\n        r = 0.07 + 0.93 * Math.pow(r2, 0.85);\n        const arm = i % armCount;\n        const scatter = (r3 + r4 - 1) * (0.5 - r * 0.22);\n        a = (arm / armCount) * TAU + r * twist + scatter;\n        r += (r5 - 0.5) * 0.05;\n        const zone = r * 2.6 + r6 * 1.4;\n        bucket = zone < 1 ? 1 : zone < 2.1 ? 2 : r6 > 0.94 ? 4 : 3;\n      }\n      sr[i] = Math.max(0.005, r);\n      sa[i] = a;\n      sz[i] = r5;\n      ss[i] = 0.45 + Math.pow(r6, 7) * 1.7;\n      sb[i] = 0.35 + 0.65 * Math.pow(r3, 2) * (1.1 - r * 0.4);\n      sp[i] = r4 * TAU;\n      sw[i] = 0.6 + r1 * 2.4;\n      buckets[bucket].push(i);\n    }\n    let cursor = 0;\n    for (let b = 0; b < buckets.length; b++) {\n      for (const i of buckets[b]) order[cursor++] = i;\n      bucketEnd[b] = cursor;\n    }\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 act = 0;\n    let placed = false;\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.3 * Math.sin(tt * 0.31 + 0.6));\n    }\n    function idleY(tt: number) {\n      return h * (0.5 + 0.28 * Math.sin(tt * 0.47 + 1.9));\n    }\n\n    function track(dt: number) {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed) {\n        px = gx;\n        py = gy;\n        placed = true;\n        return;\n      }\n      const k = 1 - Math.exp(-dt * (inside ? 7 : 1.8));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n      act += ((inside ? 1 : 0) - act) * (1 - Math.exp(-dt * 3));\n    }\n\n    function draw() {\n      if (!ctx || !w || !h) return;\n      ctx.clearRect(0, 0, w, h);\n      const cx = w / 2;\n      const cy = h / 2;\n      const R = Math.min(w * 0.5, (h * 0.5) / incline) * 0.98;\n      const nx = (px - cx) / Math.max(1, w / 2);\n      const ny = (py - cy) / Math.max(1, h / 2);\n      const spin = t * 0.06;\n      const roll = -0.32;\n      const cr = Math.cos(roll);\n      const sn = Math.sin(roll);\n      const partR = Math.max(40, R * 0.42);\n      const partR2 = partR * partR;\n      const strength = spread * (0.4 + 0.6 * act);\n\n      ctx.globalCompositeOperation = \"lighter\";\n      const gx0 = cx + nx * parallax * 0.2;\n      const gy0 = cy + ny * parallax * 0.2;\n      const halo = ctx.createRadialGradient(gx0, gy0, 0, gx0, gy0, R * 0.9);\n      halo.addColorStop(0, accent);\n      halo.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.globalAlpha = 0.12;\n      ctx.fillStyle = halo;\n      ctx.fillRect(0, 0, w, h);\n      const core = ctx.createRadialGradient(gx0, gy0, 0, gx0, gy0, R * 0.32);\n      core.addColorStop(0, \"#ffffff\");\n      core.addColorStop(0.18, coreColor);\n      core.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.globalAlpha = 0.85;\n      ctx.fillStyle = core;\n      ctx.save();\n      ctx.translate(gx0, gy0);\n      ctx.rotate(roll);\n      ctx.scale(1, incline * 1.25);\n      ctx.translate(-gx0, -gy0);\n      ctx.fillRect(gx0 - R, gy0 - R, R * 2, R * 2);\n      ctx.restore();\n\n      let start = 0;\n      for (let b = 0; b < palette.length; b++) {\n        ctx.fillStyle = palette[b];\n        const end = bucketEnd[b];\n        for (let o = start; o < end; o++) {\n          const i = order[o];\n          const r = sr[i];\n          const ang = sa[i] + spin + Math.sin(t * 0.2 + sp[i]) * 0.012 / (r + 0.2);\n          const lx = Math.cos(ang) * r * R;\n          const ly = Math.sin(ang) * r * R * incline;\n          const depth = sz[i] - 0.5;\n          let x = cx + lx * cr - ly * sn + nx * parallax * (0.35 + depth * 1.6);\n          let y = cy + lx * sn + ly * cr + ny * parallax * (0.35 + depth * 1.6) * 0.7;\n          const dx = x - px;\n          const dy = y - py;\n          const d2 = dx * dx + dy * dy;\n          if (d2 < partR2 && d2 > 0.01) {\n            const d = Math.sqrt(d2);\n            const f = 1 - d / partR;\n            const push = f * f * strength * (0.6 + sz[i] * 0.8);\n            x += (dx / d) * push;\n            y += (dy / d) * push;\n          }\n          const tw = 0.72 + 0.28 * Math.sin(t * sw[i] + sp[i]);\n          ctx.globalAlpha = Math.min(1, sb[i] * tw);\n          const s = ss[i] * (0.8 + sz[i] * 0.5);\n          ctx.fillRect(x - s / 2, y - s / 2, s, s);\n        }\n        start = end;\n      }\n\n      ctx.globalCompositeOperation = \"source-over\";\n      ctx.lineCap = \"round\";\n      const lx0 = nx * parallax * 0.35;\n      const ly0 = ny * parallax * 0.25;\n      for (let arm = 0; arm < armCount; arm++) {\n        for (let pass = 0; pass < 3; pass++) {\n          ctx.beginPath();\n          for (let s = 0; s <= 40; s++) {\n            const r = 0.1 + (s / 40) * 0.62;\n            const ang = (arm / armCount) * TAU + r * twist + spin - 0.24;\n            const ex = Math.cos(ang) * r * R;\n            const ey = Math.sin(ang) * r * R * incline;\n            const x = cx + ex * cr - ey * sn + lx0;\n            const y = cy + ex * sn + ey * cr + ly0;\n            if (s === 0) ctx.moveTo(x, y);\n            else ctx.lineTo(x, y);\n          }\n          ctx.globalAlpha = [0.1, 0.14, 0.2][pass];\n          ctx.lineWidth = R * [0.075, 0.045, 0.02][pass];\n          ctx.strokeStyle = \"#020205\";\n          ctx.stroke();\n        }\n      }\n      ctx.globalAlpha = 1;\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    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function still() {\n      t = 3;\n      inside = false;\n      placed = false;\n      act = 0;\n      track(0);\n      draw();\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 (!wrap || motion.matches) return;\n      const rect = wrap.getBoundingClientRect();\n      const kx = rect.width ? wrap.clientWidth / rect.width : 1;\n      const ky = rect.height ? wrap.clientHeight / rect.height : 1;\n      tx = (e.clientX - rect.left) * kx;\n      ty = (e.clientY - rect.top) * ky;\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  }, [stars, arms, twist, accent, coreColor, tilt, parallax, spread, speed, seed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qq1 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-galaxy\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qq1-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-qq1-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}