{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-flow-field",
  "type": "registry:component",
  "title": "Flow Field",
  "description": "Thousands of particles follow a slowly shifting vector field and leave fading trails, tinted from one color to another by their heading. The pointer stirs a local eddy into the current.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "flow field",
    "particles",
    "trails",
    "canvas",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/bg-flow-field",
  "files": [
    {
      "path": "registry/hyperblur/bg-flow-field.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-flow-field.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgFlowFieldProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  background?: string;\n  density?: number;\n  trail?: number;\n  scale?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-ve2 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-ve2-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ve2-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\nexport function BgFlowField({\n  color = \"#2f7bff\",\n  accent = \"#5ff0d2\",\n  background = \"#06070b\",\n  density = 1,\n  trail = 0.9,\n  scale = 1,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgFlowFieldProps) {\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 canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    const host = wrap?.parentElement;\n    if (!wrap || !canvas || !ctx || !host) return;\n    const g: CanvasRenderingContext2D = ctx;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n    const hostEl: HTMLElement = host;\n\n    const ca = rgbOf(color, [47, 123, 255]);\n    const cb = rgbOf(accent, [95, 240, 210]);\n    const bg = rgbOf(background, [6, 7, 11]);\n    const fade = 0.035 + 0.25 * (1 - Math.max(0, Math.min(1, trail)));\n    const palette: string[] = [];\n    for (let i = 0; i < 16; i++) {\n      const k = i / 15;\n      const r = Math.round(ca[0] + (cb[0] - ca[0]) * k);\n      const gg = Math.round(ca[1] + (cb[1] - ca[1]) * k);\n      const bb = Math.round(ca[2] + (cb[2] - ca[2]) * k);\n      palette.push(`rgba(${r},${gg},${bb},0.55)`);\n    }\n\n    let seed = 0x2f7b;\n    function rand() {\n      seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;\n      return seed / 4294967296;\n    }\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let n = 0;\n    let px = new Float32Array(0);\n    let py = new Float32Array(0);\n    let life = new Float32Array(0);\n    let mx = -9999;\n    let my = -9999;\n    let inside = false;\n\n    function angle(x: number, y: number) {\n      const k = 0.0032 / Math.max(0.3, scale);\n      const a = Math.sin(x * k + t * 0.11) * 1.3 + Math.cos(y * k * 1.21 - t * 0.09) * 1.1 + Math.sin((x - y) * k * 0.63 + t * 0.05) * 0.9;\n      return a * 1.6;\n    }\n\n    function spawn(i: number) {\n      px[i] = rand() * w;\n      py[i] = rand() * h;\n      life[i] = 60 + rand() * 220;\n    }\n\n    function setup() {\n      n = Math.max(80, Math.min(2600, Math.round(((w * h) / 520) * density)));\n      px = new Float32Array(n);\n      py = new Float32Array(n);\n      life = new Float32Array(n);\n      seed = 0x2f7b;\n      for (let i = 0; i < n; i++) spawn(i);\n      g.fillStyle = background;\n      g.fillRect(0, 0, w, h);\n    }\n\n    function step(dt: number) {\n      g.fillStyle = `rgba(${bg[0]},${bg[1]},${bg[2]},${fade})`;\n      g.fillRect(0, 0, w, h);\n      g.lineWidth = 1;\n      g.lineCap = \"round\";\n      const v = 1.25 * Math.max(0.3, Math.min(3, dt * 60));\n      const paths: Path2D[] = [];\n      for (let c = 0; c < 16; c++) paths.push(new Path2D());\n      for (let i = 0; i < n; i++) {\n        const x = px[i];\n        const y = py[i];\n        let a = angle(x, y);\n        if (inside) {\n          const dx = x - mx;\n          const dy = y - my;\n          const d2 = dx * dx + dy * dy;\n          if (d2 < 22500) a += (1 - d2 / 22500) * 1.9;\n        }\n        const nx = x + Math.cos(a) * v;\n        const ny = y + Math.sin(a) * v;\n        const k = (Math.sin(a) * 0.5 + 0.5) * 15;\n        const path = paths[k | 0];\n        path.moveTo(x, y);\n        path.lineTo(nx, ny);\n        px[i] = nx;\n        py[i] = ny;\n        life[i] -= 1;\n        if (life[i] <= 0 || nx < -4 || ny < -4 || nx > w + 4 || ny > h + 4) spawn(i);\n      }\n      for (let c = 0; c < 16; c++) {\n        g.strokeStyle = palette[c];\n        g.stroke(paths[c]);\n      }\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 = 3;\n      setup();\n      for (let k = 0; k < 160; k++) step(1 / 60);\n    }\n\n    function resize() {\n      const nw = el.clientWidth;\n      const nh = el.clientHeight;\n      if (!nw || !nh) return;\n      if (Math.abs(nw - w) < 2 && Math.abs(nh - h) < 2 && n) 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      if (motion.matches) still();\n      else {\n        setup();\n        for (let k = 0; k < 40; k++) step(1 / 60);\n      }\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      step(dt * speed);\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    }\n\n    function onMotion() {\n      if (motion.matches) still();\n      sync();\n    }\n\n    function move(e: PointerEvent) {\n      const rect = el.getBoundingClientRect();\n      mx = e.clientX - rect.left;\n      my = e.clientY - rect.top;\n      inside = true;\n    }\n\n    function leave() {\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(el);\n    io.observe(el);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", onMotion);\n    hostEl.addEventListener(\"pointermove\", move, { passive: true });\n    hostEl.addEventListener(\"pointerleave\", 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\", onMotion);\n      hostEl.removeEventListener(\"pointermove\", move);\n      hostEl.removeEventListener(\"pointerleave\", leave);\n    };\n  }, [color, accent, background, density, trail, scale, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ve2 ${className}`} style={{ background, ...style }} {...rest}>\n      <style href=\"hb-bg-flow-field\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ve2-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-ve2-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}