{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-metaballs",
  "type": "registry:component",
  "title": "Metaballs",
  "description": "Glossy two-tone blobs that drift, merge and split like a lava lamp, with a bright rim and a specular edge where they meet the light. The field is computed at low resolution and scaled up smoothly, so it stays light on the CPU.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "metaballs",
    "blobs",
    "liquid",
    "canvas",
    "lava lamp"
  ],
  "docs": "https://hyperblur.dev/effects/bg-metaballs",
  "files": [
    {
      "path": "registry/hyperblur/bg-metaballs.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-metaballs.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgMetaballsProps extends React.HTMLAttributes<HTMLDivElement> {\n  colors?: [string, string];\n  accent?: string;\n  background?: string;\n  count?: number;\n  size?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-ve1 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-ve1-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ve1-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 BgMetaballs({\n  colors = [\"#2f7bff\", \"#b46bff\"],\n  accent = \"#e9f1ff\",\n  background = \"#07080c\",\n  count = 7,\n  size = 1,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgMetaballsProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const c0 = colors[0];\n  const c1 = colors[1];\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !canvas || !ctx) return;\n    const g: CanvasRenderingContext2D = ctx;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n    const buf = document.createElement(\"canvas\");\n    const bctx = buf.getContext(\"2d\");\n    if (!bctx) return;\n    const b: CanvasRenderingContext2D = bctx;\n\n    const ca = rgbOf(c0, [47, 123, 255]);\n    const cb = rgbOf(c1, [180, 107, 255]);\n    const hi = rgbOf(accent, [233, 241, 255]);\n    const bg = rgbOf(background, [7, 8, 12]);\n    const n = Math.max(2, Math.min(16, Math.round(count)));\n    const seeds = Array.from({ length: n }, (_, i) => ({\n      ax: 0.18 + rnd(i * 7 + 1) * 0.3,\n      ay: 0.16 + rnd(i * 7 + 2) * 0.28,\n      fx: 0.12 + rnd(i * 7 + 3) * 0.22,\n      fy: 0.1 + rnd(i * 7 + 4) * 0.2,\n      px: rnd(i * 7 + 5) * 6.283,\n      py: rnd(i * 7 + 6) * 6.283,\n      r: 0.08 + rnd(i * 7 + 7) * 0.07,\n    }));\n    const bx = new Float32Array(n);\n    const by = new Float32Array(n);\n    const br = new Float32Array(n);\n\n    let w = 0;\n    let h = 0;\n    let gw = 0;\n    let gh = 0;\n    let img: ImageData | null = null;\n    let t = 0;\n\n    function place() {\n      const m = Math.min(w, h);\n      for (let i = 0; i < n; i++) {\n        const s = seeds[i];\n        bx[i] = (0.5 + s.ax * Math.sin(t * s.fx + s.px) + 0.08 * Math.sin(t * s.fy * 2.3 + s.py)) * gw;\n        by[i] = (0.5 + s.ay * Math.cos(t * s.fy + s.py) + 0.06 * Math.cos(t * s.fx * 1.7 + s.px)) * gh;\n        const rr = s.r * m * size * (gw / Math.max(1, w)) * (1 + 0.12 * Math.sin(t * 0.9 + s.px));\n        br[i] = rr * rr;\n      }\n    }\n\n    function render() {\n      if (!img || !gw || !gh) return;\n      place();\n      const d = img.data;\n      let p = 0;\n      for (let y = 0; y < gh; y++) {\n        const vy = y / gh;\n        const r0 = ca[0] + (cb[0] - ca[0]) * vy;\n        const g0 = ca[1] + (cb[1] - ca[1]) * vy;\n        const b0 = ca[2] + (cb[2] - ca[2]) * vy;\n        for (let x = 0; x < gw; x++) {\n          let f = 0;\n          let fs = 0;\n          for (let i = 0; i < n; i++) {\n            const dx = x - bx[i];\n            const dy = y - by[i];\n            f += br[i] / (dx * dx + dy * dy + 1);\n            const ex = dx - 2.2;\n            const ey = dy - 2.2;\n            fs += br[i] / (ex * ex + ey * ey + 1);\n          }\n          let r = bg[0];\n          let gg = bg[1];\n          let bb = bg[2];\n          if (f < 1) {\n            const halo = f > 0.45 ? ((f - 0.45) / 0.55) ** 2 * 0.32 : 0;\n            r += (r0 - r) * halo;\n            gg += (g0 - gg) * halo;\n            bb += (b0 - bb) * halo;\n          } else {\n            const edge = Math.min(1, (f - 1) / 0.06);\n            const core = Math.min(1, (f - 1) / 2.4);\n            const shade = 0.42 + 0.58 * core;\n            const rim = Math.max(0, 1 - (f - 1) / 0.35) * 0.55;\n            const spec = Math.max(0, Math.min(1, (f - fs) * 0.9 - 0.15));\n            let cr = r0 * shade + (hi[0] - r0 * shade) * rim * 0.5;\n            let cg = g0 * shade + (hi[1] - g0 * shade) * rim * 0.5;\n            let cbv = b0 * shade + (hi[2] - b0 * shade) * rim * 0.5;\n            cr += (hi[0] - cr) * spec;\n            cg += (hi[1] - cg) * spec;\n            cbv += (hi[2] - cbv) * spec;\n            r += (cr - r) * edge;\n            gg += (cg - gg) * edge;\n            bb += (cbv - bb) * edge;\n          }\n          d[p] = r;\n          d[p + 1] = gg;\n          d[p + 2] = bb;\n          d[p + 3] = 255;\n          p += 4;\n        }\n      }\n      b.putImageData(img, 0, 0);\n      g.imageSmoothingEnabled = true;\n      g.imageSmoothingQuality = \"high\";\n      g.drawImage(buf, 0, 0, w, h);\n    }\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function still() {\n      t = 4.2;\n      render();\n    }\n\n    function resize() {\n      const nw = el.clientWidth;\n      const nh = el.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      cv.width = Math.max(1, Math.round(w * dpr));\n      cv.height = Math.max(1, Math.round(h * dpr));\n      g.setTransform(dpr, 0, 0, dpr, 0, 0);\n      const step = Math.max(3, Math.ceil(Math.sqrt((w * h) / 16000)));\n      gw = Math.max(8, Math.ceil(w / step));\n      gh = Math.max(8, Math.ceil(h / step));\n      buf.width = gw;\n      buf.height = gh;\n      img = b.createImageData(gw, gh);\n      if (motion.matches) still();\n      else render();\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.05, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      render();\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    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    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    };\n  }, [c0, c1, accent, background, count, size, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ve1 ${className}`} style={{ background, ...style }} {...rest}>\n      <style href=\"hb-bg-metaballs\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ve1-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-ve1-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}