{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"swirl","type":"registry:ui","title":"Swirl","description":"A psychedelic paint swirl in two bold colors outlined with ink, with an optional chunky-pixel mode. WebGL.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/swirl.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface SwirlProps {\n  /** First paint color. */\n  color1?: string;\n  /** Second paint color. */\n  color2?: string;\n  /** Ink color that outlines the swirls. */\n  color3?: string;\n  /** Flow speed multiplier; 0 freezes the motion. */\n  speed?: number;\n  /** Zoom; higher means larger swirls. */\n  scale?: number;\n  /** Strength of the spiral twist around the center. */\n  twist?: number;\n  /** Render in chunky pixels. */\n  pixelated?: boolean;\n  /** Pixel size in px when pixelated. */\n  pixelSize?: number;\n  className?: string;\n}\n\nconst VERT = \"attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}\";\n\nconst FRAG = `\nprecision highp float;\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec3 uC0;\nuniform vec3 uC1;\nuniform vec3 uC2;\nuniform float uScale;\nuniform float uTwist;\nuniform float uPixel;\n\nmat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }\n\nvoid main() {\n  vec2 frag = gl_FragCoord.xy;\n  if (uPixel > 1.0) frag = (floor(frag / uPixel) + 0.5) * uPixel;\n  vec2 p = (frag - 0.5 * uRes) / uRes.y * 4.0 / uScale;\n  float t = uTime * 0.35;\n\n  // Spiral twist: the rotation grows with distance, slowly winding over time.\n  float r = length(p);\n  p = rot(uTwist * r * 0.55 - t * 0.35) * p;\n\n  // Stir the paint: repeated sine displacements fold the domain onto itself.\n  vec2 q = p * 1.5;\n  for (int i = 0; i < 6; i++) {\n    float fi = float(i);\n    q += 0.85 * vec2(sin(q.y * 1.25 + t + fi * 1.13), cos(q.x * 1.1 - t * 0.85 + fi * 1.71)) / (1.0 + fi * 0.45);\n    q = rot(0.35) * q;\n  }\n\n  float f = 0.5 + 0.5 * sin(q.x * 1.2 + q.y * 0.9 + t * 0.5);\n  float g = 0.5 + 0.5 * sin(length(q) * 1.15 - t * 0.7);\n\n  vec3 col = mix(uC0, uC1, smoothstep(0.47, 0.53, f));\n  // Dark ink where the two paints meet, plus fainter secondary veins.\n  float fi = (f - 0.5) * 12.0;\n  float ink = exp(-fi * fi * fi * fi);\n  float gi = (g - 0.5) * 16.0;\n  float vein = exp(-gi * gi * gi * gi) * 0.5;\n  col = mix(col, uC2, clamp(ink * 0.9 + vein, 0.0, 1.0));\n  // Glossy body: lighter crests, deeper troughs.\n  col *= 0.78 + 0.4 * g;\n  col += 0.12 * pow(g, 10.0) * (1.0 - ink);\n\n  vec2 v = gl_FragCoord.xy / uRes - 0.5;\n  col *= 1.0 - dot(v, v) * 0.9;\n  gl_FragColor = vec4(col, 1.0);\n}\n`;\n\nfunction rgb(hex: string) {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, \"$&$&\");\n  const n = parseInt(h.padEnd(6, \"0\").slice(0, 6), 16) || 0;\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n}\n\n/** Compiles a full-screen-triangle program and returns a cached uniform lookup, or null. */\nfunction fullscreen(gl: WebGLRenderingContext, frag: string) {\n  const prog = gl.createProgram();\n  if (!prog) return null;\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    if (!sh) return null;\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)) return null;\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  const cache = new Map<string, WebGLUniformLocation | null>();\n  return (name: string) => {\n    if (!cache.has(name)) cache.set(name, gl.getUniformLocation(prog, name));\n    return cache.get(name) ?? null;\n  };\n}\n\nexport function Swirl({\n  color1 = \"#ff4b3e\",\n  color2 = \"#1f7ae0\",\n  color3 = \"#0f1a24\",\n  speed = 1,\n  scale = 1,\n  twist = 1,\n  pixelated = false,\n  pixelSize = 5,\n  className,\n}: SwirlProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ color1, color2, color3, speed, scale, twist, pixelated, pixelSize });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { color1, color2, color3, speed, scale, twist, pixelated, pixelSize };\n    redraw.current?.();\n  }, [color1, color2, color3, speed, scale, twist, pixelated, pixelSize]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const canvas = document.createElement(\"canvas\");\n    canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%;display:block\";\n    el.appendChild(canvas);\n    const gl = canvas.getContext(\"webgl\", { antialias: false, powerPreference: \"low-power\" });\n    const u = gl && fullscreen(gl, FRAG);\n    if (!gl || !u) {\n      canvas.remove();\n      return;\n    }\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let raf = 0;\n    let visible = true;\n    let time = 10;\n    let last = 0;\n    let dpr = 1;\n\n    const draw = () => {\n      const o = opts.current;\n      gl.uniform2f(u(\"uRes\"), canvas.width, canvas.height);\n      gl.uniform1f(u(\"uTime\"), time);\n      gl.uniform3fv(u(\"uC0\"), rgb(o.color1));\n      gl.uniform3fv(u(\"uC1\"), rgb(o.color2));\n      gl.uniform3fv(u(\"uC2\"), rgb(o.color3));\n      gl.uniform1f(u(\"uScale\"), Math.max(o.scale, 0.05));\n      gl.uniform1f(u(\"uTwist\"), o.twist);\n      gl.uniform1f(u(\"uPixel\"), o.pixelated ? Math.max(o.pixelSize, 1) * dpr : 0);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n    const loop = (now: number) => {\n      time += (Math.min(now - last, 50) / 1000) * opts.current.speed;\n      last = now;\n      draw();\n      raf = requestAnimationFrame(loop);\n    };\n    const play = () => {\n      if (raf || !visible || reduced) return;\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    };\n    const resize = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.max(1, Math.round(el.clientWidth * dpr));\n      canvas.height = Math.max(1, Math.round(el.clientHeight * dpr));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      draw();\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) play();\n      else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    redraw.current = draw;\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      redraw.current = null;\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\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"}]}