{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"glow-eddies","type":"registry:ui","title":"Glow Eddies","description":"Luminous, colorful ether that flows on its own and swirls into eddies wherever the cursor moves. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/glow-eddies.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface GlowEddiesProps {\n  /** Three colors blended through the flow. */\n  colors?: string[];\n  /** Animation speed multiplier. */\n  speed?: number;\n  /** Noise scale; higher gives finer wisps. */\n  scale?: number;\n  /** How hard the cursor stirs the ether. */\n  swirl?: number;\n  intensity?: number;\n  /** When the cursor is away, an invisible hand keeps stirring. */\n  autoStir?: boolean;\n  className?: string;\n}\n\nconst TRAIL = 16;\n\nconst FRAG = `precision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec3 uC1;\nuniform vec3 uC2;\nuniform vec3 uC3;\nuniform float uScale;\nuniform float uSwirl;\nuniform float uIntensity;\nuniform vec4 uTrail[${TRAIL}];\n\nfloat hash(vec2 p){ return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }\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}\nfloat fbm(vec2 p){\n  float v = 0.0, a = 0.5;\n  mat2 m = mat2(1.6, 1.2, -1.2, 1.6);\n  for (int i = 0; i < 5; i++) { v += a * noise(p); p = m * p; a *= 0.5; }\n  return v;\n}\n\nvoid main(){\n  vec2 p = gl_FragCoord.xy / uRes.y;\n  vec2 w = p;\n  float energy = 0.0;\n  // Each trail point drags the domain along its motion and curls it around itself.\n  for (int i = 0; i < ${TRAIL}; i++) {\n    vec4 s = uTrail[i];\n    vec2 d = p - s.xy;\n    float f = exp(-dot(d, d) * 28.0);\n    float sp = length(s.zw);\n    w -= s.zw * f * 0.12 * uSwirl;\n    w += vec2(-d.y, d.x) * f * sp * uSwirl * 0.7;\n    energy += f * sp;\n  }\n\n  vec2 q = w * uScale;\n  float t = uTime * 0.22;\n  vec2 a = vec2(fbm(q + vec2(0.0, t)), fbm(q + vec2(5.2, 1.3) - t));\n  vec2 b = vec2(fbm(q + 3.5 * a + vec2(1.7, 9.2) + t * 0.7), fbm(q + 3.5 * a + vec2(8.3, 2.8) - t * 0.6));\n  float f = fbm(q + 3.0 * b);\n\n  vec3 col = mix(uC1, uC2, smoothstep(0.35, 0.7, a.y));\n  col = mix(col, uC3, smoothstep(0.45, 1.0, length(b) * 0.85 + (a.x - 0.5)));\n  float lum = smoothstep(0.3, 1.05, f * f * 1.7 + 0.25 * b.y);\n  col *= lum * lum * (1.0 + min(energy, 1.5) * 0.6) * uIntensity;\n  col += (hash(gl_FragCoord.xy) - 0.5) / 255.0;\n  col = clamp(col, 0.0, 1.0);\n  gl_FragColor = vec4(col, max(col.r, max(col.g, col.b)));\n}`;\n\nfunction hexToRgb(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\ntype SetUniform = (name: string, v: number | number[], size?: number) => void;\n\n/** Full-screen-triangle WebGL runner: sizes to its host, pauses offscreen, one static frame under reduced motion. */\nfunction mountShader(host: HTMLElement, frag: string, frame: (set: SetUniform, dt: number) => 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, premultipliedAlpha: true });\n  if (!gl) return null;\n  const prog = gl.createProgram()!;\n  const stages: [number, string][] = [\n    [gl.VERTEX_SHADER, \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\"],\n    [gl.FRAGMENT_SHADER, frag],\n  ];\n  for (const [type, src] of stages) {\n    const s = gl.createShader(type)!;\n    gl.shaderSource(s, src);\n    gl.compileShader(s);\n    if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) console.warn(gl.getShaderInfoLog(s));\n    gl.attachShader(prog, s);\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 setters = [gl.uniform1fv, gl.uniform2fv, gl.uniform3fv, gl.uniform4fv];\n  const set: SetUniform = (name, v, size) => {\n    if (!locs.has(name)) locs.set(name, gl.getUniformLocation(prog, name));\n    const arr = typeof v === \"number\" ? [v] : v;\n    setters[(size ?? arr.length) - 1].call(gl, locs.get(name)!, arr);\n  };\n\n  const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  let raf = 0;\n  let last = 0;\n  const render = (dt: number) => {\n    set(\"uRes\", [canvas.width, canvas.height]);\n    frame(set, dt);\n    gl.drawArrays(gl.TRIANGLES, 0, 3);\n  };\n  const loop = (now: number) => {\n    render(last ? Math.min((now - last) / 1000, 0.1) : 0);\n    last = now;\n    raf = requestAnimationFrame(loop);\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    render(0);\n  };\n  const ro = new ResizeObserver(resize);\n  ro.observe(host);\n  const io = new IntersectionObserver(([e]) => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n    last = 0;\n    if (e.isIntersecting && !reduced) raf = requestAnimationFrame(loop);\n  });\n  io.observe(host);\n\n  return {\n    redraw: () => {\n      if (!raf) render(0);\n    },\n    destroy: () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    },\n  };\n}\n\nconst DEFAULT_COLORS = [\"#7c3aed\", \"#22d3ee\", \"#f472b6\"];\nconst toColors = (c: string[]) => [0, 1, 2].map((i) => hexToRgb(c[i] ?? c[0] ?? \"#ffffff\"));\n\nexport function GlowEddies({\n  colors = DEFAULT_COLORS,\n  speed = 1,\n  scale = 2.2,\n  swirl = 1,\n  intensity = 1.2,\n  autoStir = true,\n  className,\n}: GlowEddiesProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ colors: toColors(colors), speed, scale, swirl, intensity, autoStir });\n  const redraw = useRef<() => void>(undefined);\n  const colorKey = colors.join(\",\");\n\n  useEffect(() => {\n    opts.current = { colors: toColors(colorKey.split(\",\")), speed, scale, swirl, intensity, autoStir };\n    redraw.current?.();\n  }, [colorKey, speed, scale, swirl, intensity, autoStir]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const area = el.parentElement ?? el;\n    // Trail ring buffer: x, y (container-height units, y up), vx, vy (units per second).\n    const trail = new Float32Array(TRAIL * 4).fill(-10);\n    let next = 0;\n    let t = 8;\n    let idle = 10;\n    let ghost = 0;\n    const last = { x: 0, y: 0, time: 0 };\n\n    const push = (x: number, y: number, now: number) => {\n      const dt = Math.max((now - last.time) / 1000, 1 / 120);\n      const vx = Math.max(-3, Math.min(3, (x - last.x) / dt));\n      const vy = Math.max(-3, Math.min(3, (y - last.y) / dt));\n      const fresh = now - last.time > 200;\n      last.x = x;\n      last.y = y;\n      last.time = now;\n      if (fresh) return;\n      trail.set([x, y, vx, vy], next * 4);\n      next = (next + 1) % TRAIL;\n    };\n\n    const shader = mountShader(el, FRAG, (set, dt) => {\n      const o = opts.current;\n      t += dt * o.speed;\n      const decay = Math.exp(-dt * 1.2);\n      for (let i = 0; i < TRAIL; i++) {\n        trail[i * 4 + 2] *= decay;\n        trail[i * 4 + 3] *= decay;\n      }\n      idle += dt;\n      if (o.autoStir && idle > 2.5 && dt > 0) {\n        ghost += dt;\n        const aspect = el.clientWidth / Math.max(el.clientHeight, 1);\n        const gx = aspect * (0.5 + 0.32 * Math.sin(ghost * 0.7));\n        const gy = 0.5 + 0.28 * Math.sin(ghost * 1.1 + 1);\n        const now = performance.now();\n        if (now - last.time > 45) push(gx, gy, now);\n      }\n      const [c1, c2, c3] = o.colors;\n      set(\"uTime\", t);\n      set(\"uC1\", c1);\n      set(\"uC2\", c2);\n      set(\"uC3\", c3);\n      set(\"uScale\", Math.max(o.scale, 0.2));\n      set(\"uSwirl\", o.swirl);\n      set(\"uIntensity\", o.intensity);\n      set(\"uTrail\", Array.from(trail), 4);\n    });\n    if (!shader) return;\n    redraw.current = shader.redraw;\n\n    const onMove = (e: PointerEvent) => {\n      const r = el.getBoundingClientRect();\n      idle = 0;\n      push((e.clientX - r.left) / r.height, 1 - (e.clientY - r.top) / r.height, performance.now());\n    };\n    area.addEventListener(\"pointermove\", onMove, { passive: true });\n    return () => {\n      redraw.current = undefined;\n      area.removeEventListener(\"pointermove\", onMove);\n      shader.destroy();\n    };\n  }, []);\n\n  return (\n    <div\n      ref={host}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}