{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-ferrofluid",
  "type": "registry:component",
  "title": "Ferrofluid",
  "description": "A dark, glossy liquid-metal pool whose blobs rise toward the pointer and grow a crown of magnetic spikes, shaded with specular highlights.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "ferrofluid",
    "metaballs",
    "liquid",
    "canvas",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/bg-ferrofluid",
  "files": [
    {
      "path": "registry/hyperblur/bg-ferrofluid.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-ferrofluid.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgFerrofluidProps extends React.HTMLAttributes<HTMLDivElement> {\n  accent?: string;\n  background?: string;\n  spikes?: number;\n  size?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-qp1 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qp1-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qp1-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction rgbOf(value: string, fallback: [number, number, number]): [number, number, number] {\n  const raw = value.trim().replace(\"#\", \"\");\n  const hex = raw.length === 3 ? raw.split(\"\").map((c) => c + c).join(\"\") : raw;\n  const n = parseInt(hex.slice(0, 6), 16);\n  if (hex.length < 6 || Number.isNaN(n)) return fallback;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nfunction rnd(i: number) {\n  let x = Math.imul(i ^ 0x9e3779b9, 0x85ebca6b);\n  x ^= x >>> 13;\n  x = Math.imul(x, 0xc2b2ae35);\n  x ^= x >>> 16;\n  return (x >>> 0) / 4294967296;\n}\n\nexport function BgFerrofluid({\n  accent = \"#2f7bff\",\n  background = \"#060709\",\n  spikes = 12,\n  size = 1,\n  speed = 1,\n  children,\n  className = \"\",\n  style,\n  ...rest\n}: BgFerrofluidProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const host = wrap?.parentElement;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    const off = document.createElement(\"canvas\");\n    const octx = off.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !ctx || !octx) return;\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const [ar, ag, ab] = rgbOf(accent, [47, 123, 255]);\n    const [br, bg, bb] = rgbOf(background, [6, 7, 9]);\n    const N = Math.max(3, Math.min(24, Math.round(spikes)));\n    const POOL = 7;\n    const total = POOL + 1;\n    const bx = new Float32Array(total);\n    const by = new Float32Array(total);\n    const brr = new Float32Array(total);\n    const ax0 = new Float32Array(N);\n    const ay0 = new Float32Array(N);\n    const adx = new Float32Array(N);\n    const ady = new Float32Array(N);\n    const al2 = new Float32Array(N);\n    let r0 = 1;\n    let r1 = 1;\n    const lx = -0.48;\n    const ly = -0.62;\n    const lz = 0.62;\n    const hl = Math.hypot(lx, ly, lz + 1);\n    const hx = lx / hl;\n    const hy = ly / hl;\n    const hz = (lz + 1) / hl;\n    const h2l = Math.hypot(0.55, 0.5, 1.6);\n    const h2x = 0.55 / h2l;\n    const h2y = 0.5 / h2l;\n    const h2z = 1.6 / h2l;\n\n    let w = 0;\n    let h = 0;\n    let cs = 4;\n    let gw = 0;\n    let gh = 0;\n    let field = new Float32Array(0);\n    let height = new Float32Array(0);\n    let img: ImageData | null = null;\n    let t = 0;\n    let inside = false;\n    let tx = 0;\n    let ty = 0;\n    let px = 0;\n    let py = 0;\n    let placed = false;\n    let lift = 0;\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.3 * Math.sin(tt * 0.31 + 1.2) + 0.06 * Math.sin(tt * 0.83));\n    }\n    function idleY(tt: number) {\n      return h * (0.46 + 0.14 * Math.sin(tt * 0.47 + 0.4));\n    }\n\n    function track(dt: number) {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed || dt === 0) {\n        px = gx;\n        py = gy;\n        placed = true;\n        lift = inside ? 1 : 0.7;\n        return;\n      }\n      const k = 1 - Math.exp(-dt * (inside ? 7 : 1.8));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n      lift += ((inside ? 1 : 0.7) - lift) * Math.min(1, dt * 3);\n    }\n\n    function layout() {\n      cs = Math.max(3, Math.ceil(Math.sqrt((w * h) / 16000)));\n      gw = Math.max(2, Math.ceil(w / cs));\n      gh = Math.max(2, Math.ceil(h / cs));\n      off.width = gw;\n      off.height = gh;\n      field = new Float32Array(gw * gh);\n      height = new Float32Array(gw * gh);\n      img = octx!.createImageData(gw, gh);\n    }\n\n    function place() {\n      const m = Math.min(w, h) * size;\n      const reach = Math.max(w, h) * 0.38;\n      let k = 0;\n      for (let i = 0; i < POOL; i++) {\n        let x = w * (0.08 + (0.84 * i) / (POOL - 1)) + (rnd(i * 3 + 1) - 0.5) * w * 0.08;\n        let y = h * (0.92 + 0.1 * rnd(i * 3 + 2));\n        x += Math.sin(t * 0.33 + i * 1.7) * w * 0.035;\n        y += Math.cos(t * 0.41 + i * 2.3) * h * 0.04;\n        const dx = px - x;\n        const dy = py - y;\n        const pull = Math.exp(-(dx * dx + dy * dy) / (reach * reach)) * 0.42 * lift;\n        bx[k] = x + dx * pull;\n        by[k] = y + dy * pull;\n        brr[k] = m * (0.1 + 0.05 * rnd(i * 3 + 3));\n        k++;\n      }\n      const core = m * 0.11 * (0.92 + 0.08 * Math.sin(t * 1.9));\n      bx[k] = px;\n      by[k] = py;\n      brr[k] = core;\n      r0 = m * 0.05;\n      r1 = m * 0.004;\n      for (let i = 0; i < N; i++) {\n        const a = (i / N) * Math.PI * 2 + t * 0.22 + Math.sin(t * 0.7 + i) * 0.06;\n        const ca = Math.cos(a);\n        const sa = Math.sin(a);\n        const len = m * 0.2 * (0.5 + 0.5 * lift) * (0.7 + 0.3 * Math.sin(t * 2.4 + i * 2.31));\n        ax0[i] = px + ca * core * 0.5;\n        ay0[i] = py + sa * core * 0.5;\n        adx[i] = ca * (core * 0.5 + len);\n        ady[i] = sa * (core * 0.5 + len);\n        al2[i] = adx[i] * adx[i] + ady[i] * ady[i] || 1;\n      }\n      for (let i = 0; i < total; i++) brr[i] = brr[i] * brr[i];\n    }\n\n    function draw() {\n      if (!img || !w || !h) return;\n      place();\n      for (let j = 0; j < gh; j++) {\n        const y = (j + 0.5) * cs;\n        for (let i = 0; i < gw; i++) {\n          const x = (i + 0.5) * cs;\n          let f = 0;\n          for (let b = 0; b < total; b++) {\n            const dx = x - bx[b];\n            const dy = y - by[b];\n            f += brr[b] / (dx * dx + dy * dy + 1);\n          }\n          for (let a = 0; a < N; a++) {\n            const qx = x - ax0[a];\n            const qy = y - ay0[a];\n            let u = (qx * adx[a] + qy * ady[a]) / al2[a];\n            if (u < 0) u = 0;\n            else if (u > 1) u = 1;\n            const dx = qx - adx[a] * u;\n            const dy = qy - ady[a] * u;\n            const r = r0 + (r1 - r0) * u;\n            f += (r * r) / (dx * dx + dy * dy + 1);\n          }\n          const idx = j * gw + i;\n          field[idx] = f;\n          height[idx] = f > 0.2 ? 1 - 0.2 / f : 0;\n        }\n      }\n      const S = Math.min(w, h) * 0.22 * size;\n      const inv = S / (2 * cs);\n      const data = img.data;\n      for (let j = 0; j < gh; j++) {\n        const j0 = j > 0 ? j - 1 : j;\n        const j1 = j < gh - 1 ? j + 1 : j;\n        for (let i = 0; i < gw; i++) {\n          const idx = j * gw + i;\n          const f = field[idx];\n          const o = idx * 4;\n          const glow = f < 0.3 ? 0 : Math.min(1, (f - 0.3) / 0.7);\n          const g2 = glow * glow * 0.16;\n          let r = br + ar * g2;\n          let g = bg + ag * g2;\n          let bl = bb + ab * g2;\n          if (f > 0.85) {\n            const i0 = i > 0 ? i - 1 : i;\n            const i1 = i < gw - 1 ? i + 1 : i;\n            const nxr = -(height[j * gw + i1] - height[j * gw + i0]) * inv;\n            const nyr = -(height[j1 * gw + i] - height[j0 * gw + i]) * inv;\n            const nl = 1 / Math.sqrt(nxr * nxr + nyr * nyr + 1);\n            const nx = nxr * nl;\n            const ny = nyr * nl;\n            const nz = nl;\n            const diff = Math.max(0, nx * lx + ny * ly + nz * lz);\n            let s = Math.max(0, nx * hx + ny * hy + nz * hz);\n            s *= s;\n            s *= s;\n            s *= s;\n            s *= s;\n            s *= s;\n            s *= s;\n            s *= s;\n            let s2 = Math.max(0, nx * h2x + ny * h2y + nz * h2z);\n            s2 *= s2;\n            s2 *= s2;\n            s2 *= s2;\n            const fr = 1 - nz;\n            const lr = 5 + diff * 10 + ar * (fr * fr * 0.85 + s2 * 0.35) + 255 * s * 0.95;\n            const lg = 6 + diff * 11 + ag * (fr * fr * 0.85 + s2 * 0.35) + 255 * s * 0.95;\n            const lb = 8 + diff * 13 + ab * (fr * fr * 0.85 + s2 * 0.35) + 255 * s * 0.95;\n            const e = f >= 1.12 ? 1 : (f - 0.85) / 0.27;\n            const a = e * e * (3 - 2 * e);\n            r += (lr - r) * a;\n            g += (lg - g) * a;\n            bl += (lb - bl) * a;\n          }\n          data[o] = r > 255 ? 255 : r;\n          data[o + 1] = g > 255 ? 255 : g;\n          data[o + 2] = bl > 255 ? 255 : bl;\n          data[o + 3] = 255;\n        }\n      }\n      octx!.putImageData(img, 0, 0);\n      ctx!.imageSmoothingEnabled = true;\n      ctx!.imageSmoothingQuality = \"high\";\n      ctx!.drawImage(off, 0, 0, gw, gh, 0, 0, gw * cs, gh * cs);\n    }\n\n    function still() {\n      t = 2.6;\n      inside = false;\n      placed = false;\n      track(0);\n      draw();\n    }\n\n    function resize() {\n      if (!wrap || !canvas || !ctx) return;\n      const nw = wrap.clientWidth;\n      const nh = wrap.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      layout();\n      if (motion.matches) still();\n      else {\n        track(0);\n        draw();\n      }\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.033, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      track(dt);\n      draw();\n      raf = requestAnimationFrame(frame);\n    }\n\n    function sync() {\n      const run = visible && !document.hidden && !motion.matches;\n      if (run && !raf) {\n        prev = performance.now();\n        raf = requestAnimationFrame(frame);\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n      if (motion.matches) still();\n    }\n\n    function move(e: PointerEvent) {\n      if (!host || motion.matches) return;\n      const rect = host.getBoundingClientRect();\n      const sx = rect.width ? host.clientWidth / rect.width : 1;\n      const sy = rect.height ? host.clientHeight / rect.height : 1;\n      tx = (e.clientX - rect.left) * sx;\n      ty = (e.clientY - rect.top) * sy;\n      inside = true;\n    }\n\n    function leave(e: PointerEvent) {\n      if (e.type === \"pointerup\" && e.pointerType === \"mouse\") return;\n      inside = false;\n    }\n\n    const ro = new ResizeObserver(resize);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      sync();\n    });\n    ro.observe(wrap);\n    io.observe(wrap);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    host.addEventListener(\"pointermove\", move, { passive: true });\n    host.addEventListener(\"pointerdown\", move, { passive: true });\n    host.addEventListener(\"pointerleave\", leave);\n    host.addEventListener(\"pointerup\", leave);\n    host.addEventListener(\"pointercancel\", leave);\n    resize();\n    sync();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", sync);\n      motion.removeEventListener(\"change\", sync);\n      host.removeEventListener(\"pointermove\", move);\n      host.removeEventListener(\"pointerdown\", move);\n      host.removeEventListener(\"pointerleave\", leave);\n      host.removeEventListener(\"pointerup\", leave);\n      host.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [accent, background, spikes, size, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qp1 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-ferrofluid\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qp1-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-qp1-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}