{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"spectrum-burst","type":"registry:ui","title":"Spectrum Burst","description":"A radial burst of light rays pulsing outward from the center, split into rainbow fringes by chromatic dispersion. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/spectrum-burst.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface SpectrumBurstProps {\n  /** Multiplies the ray colors; white keeps the full spectrum. */\n  tint?: string;\n  /** Animation speed multiplier. */\n  speed?: number;\n  /** Number of main rays (rounded). */\n  rays?: number;\n  /** Strength of the chromatic split on ray edges. */\n  dispersion?: number;\n  /** Strength of the outward pulses, 0-1. */\n  pulse?: number;\n  /** Brightness. */\n  intensity?: number;\n  /** How far the burst center drifts toward the cursor, 0-1. */\n  followMouse?: number;\n  className?: string;\n}\n\nconst FRAG = `\nprecision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec2 uMouse;\nuniform float uHover;\nuniform vec3 uTint;\nuniform float uRays;\nuniform float uDisp;\nuniform float uPulse;\nuniform float uIntensity;\nuniform float uFollow;\n\nfloat hash(vec2 p) {\n  vec3 p3 = fract(vec3(p.xyx) * 0.1031);\n  p3 += dot(p3, p3.yzx + 33.33);\n  return fract((p3.x + p3.y) * p3.z);\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(hash(i), hash(i + vec2(1, 0)), f.x), mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), f.x), f.y);\n}\n\n// ray field at angle a / radius r; noise is sampled on a circle so it is seamless\nfloat rays(float a, float r, float t) {\n  vec2 c = vec2(cos(a), sin(a));\n  float n = noise(c * 2.5 + t * 0.3);\n  float n2 = noise(c * 7.0 - t * 0.5 + 11.0);\n  float big = pow(0.5 + 0.5 * sin(a * uRays + n * 6.0 + t * 0.15), 7.0);\n  float thin = pow(0.5 + 0.5 * sin(a * uRays * 3.0 + n2 * 10.0 - t * 0.25), 24.0) * 0.5;\n  float wave = 1.0 + uPulse * 0.55 * sin(r * 10.0 - t * 3.2 + n * 3.0);\n  return (big + thin) * (0.3 + n2) * wave;\n}\n\nvec3 hue(float h) {\n  return clamp(abs(mod(h * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);\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);\n  vec2 m = (uMouse - 0.5) * vec2(asp, 1.0);\n  vec2 q = p - m * uHover * uFollow;\n  float t = uTime;\n  float r = length(q);\n  float a = atan(q.y, q.x);\n\n  // chromatic dispersion: each channel sees the rays slightly rotated and scaled\n  float k = uDisp * 0.02 * (0.2 + r);\n  vec3 col = vec3(\n    rays(a - k, r * (1.0 - 0.04 * uDisp), t),\n    rays(a, r, t),\n    rays(a + k, r * (1.0 + 0.04 * uDisp), t)\n  );\n  col *= mix(vec3(1.0), 0.35 + hue(fract(a / 6.2831853 + t * 0.03)), 0.45);\n  col *= uTint;\n\n  float breath = 1.0 + uPulse * 0.18 * sin(t * 1.6);\n  col *= breath / (1.0 + r * r * 5.0) * smoothstep(0.0, 0.12, r);\n  col += uTint * (0.025 / (r + 0.025)) * 0.55 * breath;\n\n  col = 1.0 - exp(-col * uIntensity * 1.2);\n  gl_FragColor = vec4(col, max(col.r, max(col.g, col.b)));\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\nconst rgb = (hex: string) => {\n  const n = parseInt(hex.replace(\"#\", \"\").padEnd(6, \"0\").slice(0, 6), 16);\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n};\n\nexport function SpectrumBurst({\n  tint = \"#ffffff\",\n  speed = 1,\n  rays = 16,\n  dispersion = 1,\n  pulse = 0.6,\n  intensity = 1,\n  followMouse = 0.2,\n  className,\n}: SpectrumBurstProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const opts = useRef({ tint, speed, rays, dispersion, pulse, intensity, followMouse });\n  const redraw = useRef<(() => void) | undefined>(undefined);\n\n  useEffect(() => {\n    opts.current = { tint, speed, rays, dispersion, pulse, intensity, followMouse };\n    redraw.current?.();\n  }, [tint, speed, rays, dispersion, pulse, intensity, followMouse]);\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.uniform3fv(u(\"uTint\"), rgb(o.tint));\n        // integer count keeps the pattern seamless across the -PI/PI seam\n        gl.uniform1f(u(\"uRays\"), Math.max(1, Math.round(o.rays)));\n        gl.uniform1f(u(\"uDisp\"), o.dispersion);\n        gl.uniform1f(u(\"uPulse\"), o.pulse);\n        gl.uniform1f(u(\"uIntensity\"), o.intensity);\n        gl.uniform1f(u(\"uFollow\"), o.followMouse);\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"}]}