{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"spike-blob","type":"registry:ui","title":"Spike Blob","description":"A glossy black ferrofluid blob whose spikes rise toward the cursor like it is chasing a magnet.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/spike-blob.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface SpikeBlobProps {\n  /** Rim light and glow color. */\n  color?: string;\n  /** Number of spikes around the blob. */\n  spikes?: number;\n  /** Spike length multiplier. */\n  spikeLength?: number;\n  /** Blob size multiplier. */\n  size?: number;\n  /** Animation speed multiplier. */\n  speed?: 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 uColor;\nuniform float uSpikes;\nuniform float uSpike;\nuniform float uSize;\n\nfloat smin(float a, float b, float k) {\n  float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);\n  return mix(b, a, h) - k * h * (1.0 - h);\n}\n\n// Signed field of the fluid (negative inside). mag = where the magnet pulls.\nfloat field(vec2 p, vec2 mag) {\n  float t = uTime;\n  float R = 0.23 * uSize;\n  vec2 c = mag * 0.07;\n  float d = length(p - c) - R * (1.0 + 0.03 * sin(t * 1.7));\n  for (int i = 0; i < 3; i++) {\n    float fi = float(i);\n    vec2 o = c + R * (1.0 + 0.25 * sin(t * 0.7 + fi)) *\n      vec2(cos(t * (0.45 + 0.1 * fi) + fi * 2.1), sin(t * (0.38 + 0.12 * fi) + fi * 1.3));\n    d = smin(d, length(p - o) - R * (0.42 - 0.07 * fi), 0.14 * uSize);\n  }\n  vec2 q = p - c;\n  float r = length(q);\n  vec2 dir = q / max(r, 1e-4);\n  vec2 tm = mag - c;\n  float near = smoothstep(1.3, 0.15, length(tm));\n  float pull = smoothstep(-0.15, 1.0, dot(dir, normalize(tm + 1e-5)));\n  pull = mix(0.3, pull * pull, smoothstep(0.05, 0.45, length(tm))) * (0.35 + 0.65 * near);\n  float n = floor(uSpikes);\n  float cell = atan(q.y, q.x) / 6.2831853 * n;\n  float id = mod(floor(cell), n) / n * 6.2831853;\n  float f = fract(cell) - 0.5;\n  float jit = 0.8 + 0.2 * sin(id * 5.0 + t * 2.3) * sin(id * 3.0 - t * 1.1);\n  float cone = pow(max(0.0, 1.0 - abs(f) * 2.3), 1.8);\n  float len = uSpike * R * (0.1 + 0.95 * pull) * jit;\n  return d - len * cone * smoothstep(R * 0.7, R * 1.05, r) * smoothstep(R * 3.4, R * 1.6, r);\n}\n\nfloat height(float d) {\n  float x = clamp(-d / (0.16 * uSize), 0.0, 1.0);\n  return x * (2.0 - x);\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 mouse = (uMouse - 0.5) * vec2(asp, 1.0);\n  float t = uTime;\n  vec2 mag = mix(vec2(cos(t * 0.31), sin(t * 0.43)) * 0.5, mouse, uHover);\n\n  float e = 1.0 / uRes.y;\n  float d = field(p, mag);\n  float h = height(d);\n  float hx = height(field(p + vec2(e, 0.0), mag));\n  float hy = height(field(p + vec2(0.0, e), mag));\n  vec3 n = normalize(vec3((h - hx) / e * 0.1, (h - hy) / e * 0.1, 1.0));\n\n  // Glossy black: all color comes from reflected studio lights.\n  vec3 r = reflect(vec3(0.0, 0.0, -1.0), n);\n  float box = smoothstep(0.1, 0.7, r.y) * smoothstep(0.9, 0.2, abs(r.x));\n  float key = pow(max(dot(r, normalize(vec3(-0.55, 0.6, 0.6))), 0.0), 48.0);\n  float fill = pow(max(dot(r, normalize(vec3(0.7, -0.35, 0.6))), 0.0), 6.0);\n  float cur = pow(max(dot(r, normalize(vec3(mag - p, 0.3))), 0.0), 70.0) * uHover;\n  float fres = pow(1.0 - n.z, 2.5);\n  vec3 fluid = vec3(0.008, 0.008, 0.012);\n  fluid += vec3(0.9, 0.93, 1.0) * (box * fres * 0.55 + key * 1.6 + cur * 0.9);\n  fluid += uColor * (fill * 0.55 + fres * 0.25);\n  fluid += vec3(1.0) * pow(max(dot(r, normalize(vec3(-0.2, 0.3, 0.93))), 0.0), 400.0) * 0.6;\n\n  vec3 bg = vec3(0.014, 0.014, 0.02) + uColor * 0.09 * exp(-length(p) * 2.0);\n  bg += uColor * 0.4 * exp(-max(d, 0.0) * 10.0);\n  vec3 col = mix(bg, fluid, smoothstep(e, -e, d));\n  col *= 1.0 - 0.35 * dot(p, p);\n  gl_FragColor = vec4(col, 1.0);\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 SpikeBlob({\n  color = \"#7c6cff\",\n  spikes = 28,\n  spikeLength = 1,\n  size = 1,\n  speed = 1,\n  className,\n}: SpikeBlobProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const opts = useRef({ color, spikes, spikeLength, size, speed });\n  const redraw = useRef<(() => void) | undefined>(undefined);\n\n  useEffect(() => {\n    opts.current = { color, spikes, spikeLength, size, speed };\n    redraw.current?.();\n  }, [color, spikes, spikeLength, size, speed]);\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(\"uColor\"), rgb(o.color));\n        gl.uniform1f(u(\"uSpikes\"), Math.max(3, o.spikes));\n        gl.uniform1f(u(\"uSpike\"), o.spikeLength);\n        gl.uniform1f(u(\"uSize\"), Math.max(0.1, o.size));\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"}]}