{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-warp-stars",
  "type": "registry:component",
  "title": "Warp Stars",
  "description": "A 3D starfield that cruises, spools up into streaking light speed with a blue-shifted core, then drops out with a flash. Can also hold a fixed warp level.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "stars",
    "space",
    "warp",
    "canvas",
    "launch"
  ],
  "docs": "https://hyperblur.dev/effects/bg-warp-stars",
  "files": [
    {
      "path": "registry/hyperblur/bg-warp-stars.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-warp-stars.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgWarpStarsProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  background?: string;\n  stars?: number;\n  cycle?: boolean;\n  warp?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-vd4 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-vd4-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-vd4-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction hash(i: number) {\n  let x = Math.imul(i ^ 0x6a09e667, 0xbb67ae85);\n  x ^= x >>> 15;\n  x = Math.imul(x, 0x3c6ef372);\n  x ^= x >>> 13;\n  return (x >>> 0) / 4294967296;\n}\n\nfunction rgb(hex: string): [number, number, number] {\n  let s = hex.replace(\"#\", \"\");\n  if (s.length === 3) s = s.split(\"\").map((c) => c + c).join(\"\");\n  const n = parseInt(s.slice(0, 6), 16);\n  if (Number.isNaN(n)) return [255, 255, 255];\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nfunction smooth(a: number, b: number, x: number) {\n  const k = Math.max(0, Math.min(1, (x - a) / (b - a)));\n  return k * k * (3 - 2 * k);\n}\n\nexport function BgWarpStars({\n  color = \"#ffffff\",\n  accent = \"#7fb3ff\",\n  background = \"#020308\",\n  stars = 420,\n  cycle = true,\n  warp = 0.6,\n  speed = 1,\n  children,\n  className = \"\",\n  style,\n  ...rest\n}: BgWarpStarsProps) {\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 context = canvas?.getContext(\"2d\");\n    if (!wrap || !canvas || !context) return;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n    const g: CanvasRenderingContext2D = context;\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const N = Math.max(40, Math.min(1500, Math.round(stars)));\n    const xs = new Float32Array(N);\n    const ys = new Float32Array(N);\n    const zs = new Float32Array(N);\n    const tint = new Float32Array(N);\n    let seed = 0;\n    function place(i: number, z: number) {\n      seed++;\n      const a = hash(seed * 3 + 1) * Math.PI * 2;\n      const r = 0.04 + Math.sqrt(hash(seed * 3 + 2)) * 1.35;\n      xs[i] = Math.cos(a) * r;\n      ys[i] = Math.sin(a) * r;\n      zs[i] = z;\n      tint[i] = hash(seed * 3 + 3);\n    }\n    for (let i = 0; i < N; i++) place(i, 0.05 + hash(i + 7000) * 0.95);\n    const cA = rgb(color);\n    const cB = rgb(accent);\n    const shades = Array.from({ length: 6 }, (_, i) => {\n      const k = i / 5;\n      return `rgb(${Math.round(cA[0] + (cB[0] - cA[0]) * k)},${Math.round(cA[1] + (cB[1] - cA[1]) * k)},${Math.round(cA[2] + (cB[2] - cA[2]) * k)})`;\n    });\n    const PERIOD = 13;\n    const CRUISE = 0.07;\n    const TOP = 2.4;\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let raf = 0;\n    let prev = 0;\n    let visible = true;\n    let v = CRUISE;\n    let flash = 0;\n\n    function profile(time: number) {\n      if (!cycle) {\n        const lv = Math.max(0, Math.min(1, warp));\n        return { v: CRUISE + (TOP - CRUISE) * lv * lv, f: 0 };\n      }\n      const p = (time % PERIOD) / PERIOD;\n      const up = smooth(0.42, 0.6, p);\n      const down = smooth(0.84, 0.93, p);\n      const vel = CRUISE + (TOP - CRUISE) * up * (1 - down);\n      const f = p > 0.84 ? Math.exp(-(p - 0.845) * 60) * smooth(0.835, 0.845, p) : 0;\n      return { v: vel, f };\n    }\n\n    function step(dt: number) {\n      const pr = profile(t);\n      v = pr.v;\n      flash = pr.f;\n      const dz = v * dt * 0.55;\n      for (let i = 0; i < N; i++) {\n        zs[i] -= dz;\n        if (zs[i] <= 0.02) place(i, 0.96 + hash(seed + 99) * 0.04);\n      }\n    }\n\n    function draw() {\n      if (!w || !h) return;\n      const cx = w / 2;\n      const cy = h / 2;\n      const fov = Math.max(w, h) * 0.55;\n      const lv = Math.max(0, Math.min(1, (v - CRUISE) / (TOP - CRUISE)));\n      g.globalCompositeOperation = \"source-over\";\n      g.globalAlpha = 1;\n      g.fillStyle = background;\n      g.fillRect(0, 0, w, h);\n      if (lv > 0.01) {\n        const glow = g.createRadialGradient(cx, cy, 0, cx, cy, Math.max(w, h) * 0.6);\n        glow.addColorStop(0, accent);\n        glow.addColorStop(1, \"rgba(0,0,0,0)\");\n        g.globalAlpha = 0.28 * lv;\n        g.fillStyle = glow;\n        g.fillRect(0, 0, w, h);\n      }\n      g.globalCompositeOperation = \"lighter\";\n      g.lineCap = \"round\";\n      const tail = v * 0.075 + 0.003;\n      for (let i = 0; i < N; i++) {\n        const z = zs[i];\n        const zt = Math.min(1.2, z + tail);\n        const x1 = cx + (xs[i] / z) * fov;\n        const y1 = cy + (ys[i] / z) * fov;\n        if (x1 < -40 || x1 > w + 40 || y1 < -40 || y1 > h + 40) continue;\n        const x0 = cx + (xs[i] / zt) * fov;\n        const y0 = cy + (ys[i] / zt) * fov;\n        const near = 1 - z;\n        const fadeIn = Math.min(1, (1 - z) * 6);\n        const radial = Math.min(1, Math.hypot(xs[i], ys[i]) / z / 1.2);\n        const shade = Math.min(5, Math.round((lv * (1 - radial) * 0.9 + tint[i] * 0.35) * 5));\n        g.strokeStyle = shades[shade];\n        g.globalAlpha = Math.min(1, (0.25 + near * 0.85) * fadeIn);\n        g.lineWidth = 0.4 + near * near * 2.2;\n        g.beginPath();\n        g.moveTo(x0, y0);\n        g.lineTo(x1 + 0.01, y1 + 0.01);\n        g.stroke();\n      }\n      g.globalCompositeOperation = \"source-over\";\n      if (flash > 0.01) {\n        g.globalAlpha = Math.min(0.55, flash * 0.55);\n        g.fillStyle = \"#ffffff\";\n        g.fillRect(0, 0, w, h);\n      }\n      const vig = g.createRadialGradient(cx, cy, Math.min(w, h) * 0.3, cx, cy, Math.max(w, h) * 0.75);\n      vig.addColorStop(0, \"rgba(0,0,0,0)\");\n      vig.addColorStop(1, \"rgba(0,0,0,0.55)\");\n      g.globalAlpha = 1;\n      g.fillStyle = vig;\n      g.fillRect(0, 0, w, h);\n    }\n\n    function still() {\n      v = CRUISE * 3;\n      flash = 0;\n      draw();\n    }\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      if (motion.matches) still();\n      else draw();\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.05, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      step(dt * speed);\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    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, stars, cycle, warp, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-vd4 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-warp-stars\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-vd4-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-vd4-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}