{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"spiral-stars","type":"registry:ui","title":"Spiral Stars","description":"A slowly spinning spiral galaxy of twinkling stars and glowing dust, with depth parallax under the cursor.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/spiral-stars.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface SpiralStarsProps {\n  /** Base hue of the spiral arms, degrees 0-360. The core gets the complementary warm tone. */\n  hue?: number;\n  /** Star density multiplier. */\n  density?: number;\n  /** Number of spiral arms. */\n  arms?: number;\n  /** Rotation and twinkle speed multiplier. */\n  speed?: number;\n  /** 0 = steady stars, 1 = full twinkle. */\n  twinkle?: number;\n  /** Zoom; above 1 makes the galaxy bigger. */\n  scale?: number;\n  /** How far star layers shift with the cursor. */\n  parallax?: number;\n  className?: string;\n}\n\nconst FRAG = `\nprecision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec2 uMouse;\nuniform float uHover;\nuniform float uHue;\nuniform float uDensity;\nuniform float uArms;\nuniform float uTwinkle;\nuniform float uScale;\nuniform float uParallax;\n\nconst float TILT = 0.55;\n\nfloat h21(vec2 p) {\n  p = fract(p * vec2(233.34, 851.73));\n  p += dot(p, p + 23.45);\n  return fract(p.x * p.y);\n}\nfloat noise(vec2 p) {\n  vec2 i = floor(p), f = fract(p);\n  f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(h21(i), h21(i + vec2(1, 0)), f.x), mix(h21(i + vec2(0, 1)), h21(i + vec2(1, 1)), f.x), f.y);\n}\nfloat fbm(vec2 p) {\n  float v = 0.0, a = 0.5;\n  for (int i = 0; i < 4; i++) { v += a * noise(p); p = p * 2.03 + 11.7; a *= 0.5; }\n  return v;\n}\nvec3 hsv(float h, float s, float v) {\n  vec3 k = clamp(abs(mod(h * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);\n  return v * mix(vec3(1.0), k, s);\n}\nmat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }\n\n// screen -> galaxy disk (tilted, spinning)\nvec2 disk(vec2 p) {\n  p = rot(0.45) * p;\n  p.y /= TILT;\n  return rot(uTime * 0.035) * p;\n}\nfloat arms(vec2 d) {\n  float r = length(d) + 1e-4;\n  float a = atan(d.y, d.x);\n  return pow(0.5 + 0.5 * cos(floor(uArms) * a - log(r) * 5.0), 3.0);\n}\nfloat density(vec2 d) {\n  float r = length(d);\n  return exp(-r * 2.2) * (0.06 + 0.94 * arms(d)) + exp(-r * r * 40.0) * 0.8 + 0.03;\n}\n\nvec3 stars(vec2 sp, float scale, float seed, float bright) {\n  vec2 g = disk(sp) * scale;\n  vec2 id = floor(g), f = fract(g);\n  float h = h21(id + seed);\n  vec2 pos = vec2(h21(id + seed + 7.1), h21(id + seed + 3.7)) * 0.7 + 0.15;\n  vec2 wp = (id + pos) / scale;\n  if (h > density(wp) * uDensity * 1.4) return vec3(0.0);\n  // back to screen space for a round star\n  vec2 dd = rot(-uTime * 0.035) * ((pos - f) / scale);\n  dd.y *= TILT;\n  float dpx = length(dd) * uRes.y;\n  float b = pow(fract(h * 91.7), 5.0);\n  float s = (0.7 + 2.4 * b) * bright;\n  float tw = mix(1.0, 0.35 + 0.65 * (0.5 + 0.5 * sin(uTime * (1.2 + 3.0 * fract(h * 13.3)) + h * 40.0)), uTwinkle);\n  float cell = uRes.y * TILT / (scale * uScale);\n  float I = exp(-dpx * dpx / (s * s)) + b * 0.6 * exp(-dpx / (s * 2.5)) * smoothstep(cell * 0.15, cell * 0.04, dpx);\n  float r = length(wp);\n  vec3 c = hsv(uHue + (fract(h * 5.3) - 0.5) * 0.1 + 0.45 * exp(-r * 5.0), 0.15 + 0.35 * fract(h * 3.1), 1.0);\n  return c * I * tw * (0.55 + 0.45 * b);\n}\n\nvoid main() {\n  float asp = uRes.x / uRes.y;\n  vec2 p = (gl_FragCoord.xy / uRes.y - vec2(0.5 * asp, 0.5)) / uScale;\n  vec2 m = (uMouse - 0.5) * vec2(asp, 1.0) * uHover * uParallax * 0.04;\n\n  // diffuse light: arms, dust, bulge, core\n  vec2 d = disk(p + m * 0.5);\n  float r = length(d);\n  float n = fbm(d * 3.2 + 7.0);\n  float arm = arms(d + (n - 0.5) * 0.08);\n  vec3 col = hsv(uHue, 0.7, 1.0) * arm * exp(-r * 2.6) * (0.2 + 1.1 * n * n) * 0.8;\n  col += hsv(uHue + 0.08, 0.75, 1.0) * pow(n, 3.0) * arm * exp(-r * 1.5) * 0.5;\n  col += hsv(uHue + 0.45, 0.45, 1.0) * exp(-r * r * 38.0) * 0.6;\n  col += vec3(1.0, 0.96, 0.9) * exp(-r * r * 160.0) * 0.9;\n\n  col += stars(p + m * 0.3, 7.0, 1.0, 1.3);\n  col += stars(p + m * 0.6, 15.0, 17.0, 1.0);\n  col += stars(p + m * 1.0, 30.0, 41.0, 0.85);\n  col += stars(p + m * 1.5, 55.0, 73.0, 0.7);\n\n  col = 1.0 - exp(-col * 1.4);\n  float a = clamp(max(col.r, max(col.g, col.b)), 0.0, 1.0);\n  gl_FragColor = vec4(col, a);\n}\n`;\n\nconst VERT = \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\";\n\ntype Ptr = { x: number; y: number; hover: number };\ntype Uni = (name: string) => WebGLUniformLocation | null;\n\n/** Full-screen shader canvas inside `host`: DPR-capped resize, offscreen pause, reduced motion, eased pointer. */\nfunction runShader(host: HTMLElement, frag: string, speed: () => number, update: (gl: WebGLRenderingContext, u: Uni) => void) {\n  const canvas = document.createElement(\"canvas\");\n  canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%;display:block\";\n  const gl = canvas.getContext(\"webgl\", { antialias: false });\n  if (!gl) return null;\n  const prog = gl.createProgram()!;\n  for (const [type, src] of [\n    [gl.VERTEX_SHADER, VERT],\n    [gl.FRAGMENT_SHADER, frag],\n  ] as const) {\n    const sh = gl.createShader(type)!;\n    gl.shaderSource(sh, src);\n    gl.compileShader(sh);\n    if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) console.warn(gl.getShaderInfoLog(sh));\n    gl.attachShader(prog, sh);\n  }\n  gl.bindAttribLocation(prog, 0, \"p\");\n  gl.linkProgram(prog);\n  if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {\n    gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n    return null;\n  }\n  gl.useProgram(prog);\n  gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n  gl.enableVertexAttribArray(0);\n  gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n  host.appendChild(canvas);\n\n  const locs = new Map<string, WebGLUniformLocation | null>();\n  const u: Uni = (n) => {\n    if (!locs.has(n)) locs.set(n, gl.getUniformLocation(prog, n));\n    return locs.get(n) ?? null;\n  };\n  const zone = host.parentElement ?? host;\n  const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  const ptr: Ptr = { x: 0.5, y: 0.5, hover: 0 };\n  const goal: Ptr = { ...ptr };\n  let raf = 0;\n  let last = 0;\n  let time = 0;\n\n  const draw = () => {\n    gl.uniform2f(u(\"uRes\"), canvas.width, canvas.height);\n    gl.uniform1f(u(\"uTime\"), time);\n    gl.uniform2f(u(\"uMouse\"), ptr.x, ptr.y);\n    gl.uniform1f(u(\"uHover\"), ptr.hover);\n    update(gl, u);\n    gl.drawArrays(gl.TRIANGLES, 0, 3);\n  };\n  const loop = (now: number) => {\n    const dt = Math.min((now - last) / 1000, 0.1);\n    last = now;\n    const k = 1 - Math.exp(-dt * 4);\n    for (const key of [\"x\", \"y\", \"hover\"] as const) ptr[key] += (goal[key] - ptr[key]) * k;\n    time += dt * speed();\n    draw();\n    raf = requestAnimationFrame(loop);\n  };\n  const run = (on: boolean) => {\n    if (on && !raf && !reduced) {\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    } else if (!on && raf) {\n      cancelAnimationFrame(raf);\n      raf = 0;\n    }\n  };\n  const resize = () => {\n    const dpr = Math.min(window.devicePixelRatio || 1, 2);\n    canvas.width = Math.max(1, Math.round(host.clientWidth * dpr));\n    canvas.height = Math.max(1, Math.round(host.clientHeight * dpr));\n    gl.viewport(0, 0, canvas.width, canvas.height);\n    draw();\n  };\n  const move = (e: PointerEvent) => {\n    const r = host.getBoundingClientRect();\n    goal.x = (e.clientX - r.left) / r.width;\n    goal.y = 1 - (e.clientY - r.top) / r.height;\n    goal.hover = 1;\n  };\n  const leave = () => {\n    goal.hover = 0;\n  };\n  const ro = new ResizeObserver(resize);\n  const io = new IntersectionObserver(([e]) => run(e.isIntersecting));\n  ro.observe(host);\n  io.observe(host);\n  zone.addEventListener(\"pointermove\", move);\n  zone.addEventListener(\"pointerleave\", leave);\n  return {\n    redraw: () => {\n      if (!raf) draw();\n    },\n    dispose: () => {\n      run(false);\n      ro.disconnect();\n      io.disconnect();\n      zone.removeEventListener(\"pointermove\", move);\n      zone.removeEventListener(\"pointerleave\", leave);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    },\n  };\n}\n\nexport function SpiralStars({\n  hue = 225,\n  density = 1,\n  arms = 2,\n  speed = 1,\n  twinkle = 0.6,\n  scale = 1,\n  parallax = 1,\n  className,\n}: SpiralStarsProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const opts = useRef({ hue, density, arms, speed, twinkle, scale, parallax });\n  const redraw = useRef<(() => void) | undefined>(undefined);\n\n  useEffect(() => {\n    opts.current = { hue, density, arms, speed, twinkle, scale, parallax };\n    redraw.current?.();\n  }, [hue, density, arms, speed, twinkle, scale, parallax]);\n\n  useEffect(() => {\n    const r = runShader(\n      ref.current!,\n      FRAG,\n      () => opts.current.speed,\n      (gl, u) => {\n        const o = opts.current;\n        gl.uniform1f(u(\"uHue\"), (((o.hue % 360) + 360) % 360) / 360);\n        gl.uniform1f(u(\"uDensity\"), o.density);\n        gl.uniform1f(u(\"uArms\"), Math.max(1, o.arms));\n        gl.uniform1f(u(\"uTwinkle\"), o.twinkle);\n        gl.uniform1f(u(\"uScale\"), Math.max(0.1, o.scale));\n        gl.uniform1f(u(\"uParallax\"), o.parallax);\n      },\n    );\n    redraw.current = r?.redraw;\n    return () => r?.dispose();\n  }, []);\n\n  return (\n    <div\n      ref={ref}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}