{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"blob-lamp","type":"registry:ui","title":"Blob Lamp","description":"Glossy liquid blobs that drift, merge and split, with one that follows your cursor. Raw WebGL.","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"registry/ui/blob-lamp.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface BlobLampProps {\n  /** Number of wandering balls (plus one that follows the cursor). */\n  count?: number;\n  /** Color at the top. */\n  color?: string;\n  /** Color at the bottom. */\n  color2?: string;\n  /** Motion speed multiplier. */\n  speed?: number;\n  /** Ball size multiplier. */\n  size?: number;\n  /** One ball tracks the cursor while it is inside. */\n  followCursor?: boolean;\n  className?: string;\n}\n\nconst MAX = 16;\nconst VERT = `attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}`;\nconst FRAG = `precision highp float;\nuniform vec2 uRes;uniform vec3 uBalls[${MAX}];uniform int uCount;uniform vec3 uC1;uniform vec3 uC2;uniform float uScale;\nvoid main(){\n  vec2 p=gl_FragCoord.xy;\n  float f=0.;vec2 g=vec2(0.);\n  for(int i=0;i<${MAX};i++){\n    if(i>=uCount)break;\n    vec2 d=p-uBalls[i].xy;float r=uBalls[i].z;\n    float q=dot(d,d)+1.;float v=r*r/q;\n    f+=v;g-=2.*v/q*d;\n  }\n  // Anti-alias the iso-line by one pixel of field change.\n  float aa=length(g)+1e-4;\n  float body=smoothstep(1.-aa,1.+aa,f);\n  // Height sqrt(1-1/f) is an exact hemisphere for a lone ball and blends smoothly where balls merge.\n  float H=sqrt(max(1.-1./max(f,1e-4),1e-3));\n  vec2 gH=g/(2.*H*f*f);\n  vec3 n=normalize(vec3(-gH*uScale,1.));\n  vec3 L=normalize(vec3(-.45,.6,.75));\n  float diff=max(dot(n,L),0.);\n  float spec=pow(max(dot(reflect(-L,n),vec3(0,0,1)),0.),28.);\n  float rim=pow(1.-n.z,2.);\n  vec3 base=mix(uC2,uC1,clamp(p.y/uRes.y,0.,1.));\n  vec3 col=base*(.35+.75*diff)+rim*mix(base,vec3(1.),.4)*.9+spec*.85;\n  float halo=smoothstep(.25,1.,f)*(1.-body)*.28;\n  vec3 outc=col*body+base*halo;\n  gl_FragColor=vec4(outc,max(body,halo));\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\nexport function BlobLamp({\n  count = 7,\n  color = \"#c6ff3d\",\n  color2 = \"#0ea5e9\",\n  speed = 1,\n  size = 1,\n  followCursor = true,\n  className,\n}: BlobLampProps) {\n  const ref = useRef<HTMLDivElement>(null);\n\n  useEffect(() => {\n    const host = ref.current!;\n    const canvas = document.createElement(\"canvas\");\n    canvas.style.cssText = \"position:absolute;inset:0;width:100%;height:100%\";\n    host.appendChild(canvas);\n    const gl = canvas.getContext(\"webgl\", { premultipliedAlpha: true, antialias: false });\n    if (!gl) return () => canvas.remove();\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const sh = (type: number, src: string) => {\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      return s;\n    };\n    const prog = gl.createProgram()!;\n    gl.attachShader(prog, sh(gl.VERTEX_SHADER, VERT));\n    gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FRAG));\n    gl.bindAttribLocation(prog, 0, \"p\");\n    gl.linkProgram(prog);\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 u = (n: string) => gl.getUniformLocation(prog, n);\n    const uRes = u(\"uRes\");\n    const uBalls = u(\"uBalls\");\n    const uScale = u(\"uScale\");\n    const uCount = u(\"uCount\");\n    gl.uniform3fv(u(\"uC1\"), hexToRgb(color));\n    gl.uniform3fv(u(\"uC2\"), hexToRgb(color2));\n\n    const n = Math.min(Math.max(1, Math.round(count)), MAX - 1);\n    // Deterministic Lissajous paths per ball.\n    const balls = Array.from({ length: n }, (_, i) => {\n      const r = (k: number) => Math.abs(Math.sin(i * 91.7 + k * 17.3) * 4375.85) % 1;\n      return { ax: 0.18 + r(1) * 0.26, ay: 0.16 + r(2) * 0.24, fx: 0.2 + r(3) * 0.35, fy: 0.2 + r(4) * 0.35, px: r(5) * 6.28, py: r(6) * 6.28, s: 0.55 + r(7) * 0.55 };\n    });\n    const data = new Float32Array(MAX * 3);\n    const m = { x: 0, y: 0, tx: 0, ty: 0, on: 0, ton: 0 };\n    let W = 1;\n    let H = 1;\n    let dpr = 1;\n    let t = 3;\n    let prev = performance.now();\n    let raf = 0;\n    let visible = true;\n\n    const render = () => {\n      const min = Math.min(W, H);\n      const R = min * 0.085 * size;\n      balls.forEach((b, i) => {\n        data[i * 3] = W / 2 + Math.sin(t * b.fx * 1.7 + b.px) * b.ax * W * 0.9;\n        data[i * 3 + 1] = H / 2 + Math.cos(t * b.fy * 1.7 + b.py) * b.ay * H * 1.1;\n        data[i * 3 + 2] = R * b.s;\n      });\n      m.x += (m.tx - m.x) * 0.12;\n      m.y += (m.ty - m.y) * 0.12;\n      m.on += (m.ton - m.on) * 0.06;\n      data[n * 3] = m.x;\n      data[n * 3 + 1] = m.y;\n      data[n * 3 + 2] = R * 0.95 * m.on;\n      gl.uniform3fv(uBalls, data);\n      gl.uniform1i(uCount, n + 1);\n      gl.uniform1f(uScale, R * 0.8);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n    const loop = (now: number) => {\n      t += (Math.min(now - prev, 50) / 1000) * speed;\n      prev = now;\n      render();\n      raf = visible && !reduced ? requestAnimationFrame(loop) : 0;\n    };\n    const resize = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      W = canvas.width = Math.max(1, Math.round(canvas.offsetWidth * dpr));\n      H = canvas.height = Math.max(1, Math.round(canvas.offsetHeight * dpr));\n      gl.viewport(0, 0, W, H);\n      gl.uniform2f(uRes, W, H);\n      render();\n    };\n    const onMove = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      m.tx = (e.clientX - r.left) * dpr;\n      m.ty = (r.bottom - e.clientY) * dpr; // GL origin is bottom-left\n      if (m.on < 0.02) {\n        m.x = m.tx;\n        m.y = m.ty;\n      }\n      m.ton = followCursor ? 1 : 0;\n    };\n    const onLeave = () => (m.ton = 0);\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !raf && !reduced) {\n        prev = performance.now();\n        raf = requestAnimationFrame(loop);\n      }\n    });\n    io.observe(canvas);\n    const area = host.parentElement ?? host;\n    area.addEventListener(\"pointermove\", onMove, { passive: true });\n    area.addEventListener(\"pointerleave\", onLeave);\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      area.removeEventListener(\"pointermove\", onMove);\n      area.removeEventListener(\"pointerleave\", onLeave);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    };\n  }, [count, color, color2, speed, size, followCursor]);\n\n  return <div ref={ref} aria-hidden className={cn(\"pointer-events-none absolute inset-0 overflow-hidden\", className)} />;\n}\n"}]}