{
  "$schema": "https://www.cooki-ui.design/schema/registry-item.json",
  "name": "vector-flow",
  "type": "registry:ui",
  "title": "Vector Flow",
  "description": "Procedural 2D Perlin noise vector flow field with filament particle trails, vortex physics, and cursor interactions.",
  "category": "backgrounds",
  "dependencies": [
    "framer-motion",
    "clsx",
    "tailwind-merge"
  ],
  "tags": [
    "backgrounds",
    "flow-field",
    "particles",
    "perlin-noise",
    "vortex",
    "interactive"
  ],
  "files": [
    {
      "path": "registry/backgrounds/vector-flow/vector-flow.tsx",
      "type": "registry:ui",
      "target": "components/ui/vector-flow.tsx",
      "content": "\"use client\";\n\nimport React, { useEffect, useRef } from \"react\";\nimport { useInView } from \"framer-motion\";\nimport { cn } from \"@/lib/utils\";\n\nexport type InteractionMode = \"attract\" | \"repel\" | \"vortex\" | \"none\";\n\nexport interface VectorFlowProps {\n  colorParticles?: string[];\n  particleCount?: number;\n  particleSpeed?: number;\n  particleSize?: number;\n  interactionMode?: InteractionMode;\n  interactive?: boolean;\n  trailLength?: number;\n  blurAmount?: number;\n  className?: string;\n}\n\ninterface Particle {\n  x: number;\n  y: number;\n  lastX: number;\n  lastY: number;\n  vx: number;\n  vy: number;\n  color: string;\n  size: number;\n  speed: number;\n  life: number;\n  maxLife: number;\n}\n\nconst defaultColors = [\"#00f0ff\", \"#d946ef\", \"#3b82f6\"];\n\nconst perm = new Uint8Array(512);\nconst grad2 = [\n  [1, 1], [-1, 1], [1, -1], [-1, -1],\n  [1, 0], [-1, 0], [0, 1], [0, -1]\n];\n\nconst initNoise = () => {\n  const p = Array.from({ length: 256 }, (_, i) => i);\n  let seed = 42;\n  const rand = () => {\n    seed = (seed * 1103515245 + 12345) & 0x7fffffff;\n    return seed / 0x80000000;\n  };\n  for (let i = 255; i > 0; i--) {\n    const j = Math.floor(rand() * (i + 1));\n    const tmp = p[i];\n    p[i] = p[j];\n    p[j] = tmp;\n  }\n  for (let i = 0; i < 512; i++) {\n    perm[i] = p[i & 255];\n  }\n};\ninitNoise();\n\nconst fade = (t: number) => t * t * t * (t * (t * 6 - 15) + 10);\nconst lerp = (t: number, a: number, b: number) => a + t * (b - a);\n\nconst noise2D = (x: number, y: number) => {\n  const X = Math.floor(x) & 255;\n  const Y = Math.floor(y) & 255;\n  const xf = x - Math.floor(x);\n  const yf = y - Math.floor(y);\n\n  const u = fade(xf);\n  const v = fade(yf);\n\n  const aa = perm[perm[X] + Y];\n  const ab = perm[perm[X] + Y + 1];\n  const ba = perm[perm[X] + 1 + Y];\n  const bb = perm[perm[X] + 1 + Y + 1];\n\n  const g_aa = grad2[aa & 7];\n  const g_ab = grad2[ab & 7];\n  const g_ba = grad2[ba & 7];\n  const g_bb = grad2[bb & 7];\n\n  const n_aa = g_aa[0] * xf + g_aa[1] * yf;\n  const n_ba = g_ba[0] * (xf - 1) + g_ba[1] * yf;\n  const n_ab = g_ab[0] * xf + g_ab[1] * (yf - 1);\n  const n_bb = g_bb[0] * (xf - 1) + g_bb[1] * (yf - 1);\n\n  const x1 = lerp(u, n_aa, n_ba);\n  const x2 = lerp(u, n_ab, n_bb);\n\n  return lerp(v, x1, x2);\n};\n\nconst getThemeColor = (color: string, isDark: boolean): string => {\n  if (isDark) return color;\n\n  const lowColor = color.toLowerCase();\n  if (lowColor === \"#00f0ff\" || lowColor === \"#00ffff\") return \"#0284c7\";\n  if (lowColor === \"#d946ef\") return \"#b55fe6\";\n  if (lowColor === \"#a855f7\") return \"#7c3aed\";\n  if (lowColor === \"#f97316\") return \"#c2410c\";\n  if (lowColor === \"#f59e0b\") return \"#d97706\";\n  if (lowColor === \"#ef4444\") return \"#dc2626\";\n  if (lowColor === \"#14b8a6\") return \"#0d9488\";\n  if (lowColor === \"#3b82f6\") return \"#2563eb\";\n  return color;\n};\n\nexport const VectorFlow: React.FC<VectorFlowProps> = ({\n  colorParticles = defaultColors,\n  particleCount = 900,\n  particleSpeed = 1.0,\n  particleSize = 1.4,\n  interactionMode = \"vortex\",\n  interactive = true,\n  trailLength = 0.08,\n  blurAmount = 1.5,\n  className = \"\",\n}) => {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const containerRef = useRef<HTMLDivElement>(null);\n  const isInView = useInView(containerRef);\n  const isDarkModeRef = useRef(true);\n\n  const mouseRef = useRef({\n    x: 0,\n    y: 0,\n    vx: 0,\n    vy: 0,\n    active: false,\n    radius: 160,\n  });\n\n  useEffect(() => {\n    const checkTheme = () => {\n      if (typeof document !== \"undefined\") {\n        isDarkModeRef.current = document.documentElement.classList.contains(\"dark\");\n      }\n    };\n    checkTheme();\n\n    const observer = new MutationObserver(checkTheme);\n    if (typeof document !== \"undefined\") {\n      observer.observe(document.documentElement, {\n        attributes: true,\n        attributeFilter: [\"class\"],\n      });\n    }\n\n    return () => observer.disconnect();\n  }, []);\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    if (!canvas) return;\n\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n\n    let animationFrameId: number;\n    let particles: Particle[] = [];\n\n    const createParticle = (width: number, height: number): Particle => {\n      const maxLife = 100 + Math.random() * 150;\n      const x = Math.random() * width;\n      const y = Math.random() * height;\n      return {\n        x,\n        y,\n        lastX: x,\n        lastY: y,\n        vx: 0,\n        vy: 0,\n        color: colorParticles[Math.floor(Math.random() * colorParticles.length)],\n        size: (0.4 + Math.random() * 1.0) * particleSize,\n        speed: (0.6 + Math.random() * 1.0) * particleSpeed,\n        life: 0,\n        maxLife,\n      };\n    };\n\n    const resizeCanvas = () => {\n      const rect = containerRef.current?.getBoundingClientRect();\n      const width = Math.max(1, Math.round(rect?.width || window.innerWidth));\n      const height = Math.max(1, Math.round(rect?.height || window.innerHeight));\n\n      if (canvas.width !== width || canvas.height !== height) {\n        canvas.width = width;\n        canvas.height = height;\n\n        particles = [];\n        for (let i = 0; i < particleCount; i++) {\n          particles.push(createParticle(width, height));\n        }\n\n        ctx.fillStyle = isDarkModeRef.current ? \"#000000\" : \"#ffffff\";\n        ctx.fillRect(0, 0, width, height);\n      }\n    };\n\n    resizeCanvas();\n\n    const handleMouseMove = (e: MouseEvent) => {\n      const rect = canvas.getBoundingClientRect();\n      const newX = e.clientX - rect.left;\n      const newY = e.clientY - rect.top;\n\n      const mouse = mouseRef.current;\n      if (mouse.active) {\n        mouse.vx = (newX - mouse.x) * 0.4;\n        mouse.vy = (newY - mouse.y) * 0.4;\n      }\n      mouse.x = newX;\n      mouse.y = newY;\n      mouse.active = true;\n    };\n\n    const handleMouseLeave = () => {\n      const mouse = mouseRef.current;\n      mouse.active = false;\n      mouse.vx = 0;\n      mouse.vy = 0;\n    };\n\n    const handleTouchMove = (e: TouchEvent) => {\n      if (e.touches.length === 0) return;\n      const rect = canvas.getBoundingClientRect();\n      const touch = e.touches[0];\n      const newX = touch.clientX - rect.left;\n      const newY = touch.clientY - rect.top;\n\n      const mouse = mouseRef.current;\n      if (mouse.active) {\n        mouse.vx = (newX - mouse.x) * 0.4;\n        mouse.vy = (newY - mouse.y) * 0.4;\n      }\n      mouse.x = newX;\n      mouse.y = newY;\n      mouse.active = true;\n    };\n\n    const handleTouchEnd = () => {\n      const mouse = mouseRef.current;\n      mouse.active = false;\n      mouse.vx = 0;\n      mouse.vy = 0;\n    };\n\n    if (interactive) {\n      canvas.addEventListener(\"mousemove\", handleMouseMove);\n      canvas.addEventListener(\"mouseleave\", handleMouseLeave);\n      canvas.addEventListener(\"touchmove\", handleTouchMove, { passive: true });\n      canvas.addEventListener(\"touchend\", handleTouchEnd, { passive: true });\n    }\n\n    let lastTime = performance.now();\n    let time = 0;\n\n    const render = () => {\n\n      const width = canvas.width;\n      const height = canvas.height;\n\n      const now = performance.now();\n      const dt = Math.min((now - lastTime) / 1000, 0.1);\n      lastTime = now;\n      time += dt * particleSpeed;\n\n      const isDark = isDarkModeRef.current;\n\n      ctx.globalCompositeOperation = \"destination-out\";\n      ctx.fillStyle = `rgba(0, 0, 0, ${trailLength})`;\n      ctx.fillRect(0, 0, width, height);\n\n      ctx.globalCompositeOperation = isDark ? \"screen\" : \"source-over\";\n\n      const mouse = mouseRef.current;\n      mouse.vx *= 0.95;\n      mouse.vy *= 0.95;\n\n      for (let i = 0; i < particles.length; i++) {\n        const p = particles[i];\n\n        const qx = noise2D(p.x * 0.0025 + 1.2, p.y * 0.0025 + time * 0.04);\n        const qy = noise2D(p.x * 0.0025 + 5.7, p.y * 0.0025 + time * 0.04);\n\n        const baseAngle =\n          noise2D(\n            p.x * 0.0018 + qx * 0.35,\n            p.y * 0.0018 + qy * 0.35 + time * 0.05\n          ) *\n          Math.PI *\n          2.5;\n\n        let fx = Math.cos(baseAngle) * p.speed;\n        let fy = Math.sin(baseAngle) * p.speed;\n\n        if (interactive && mouse.active) {\n          const dx = mouse.x - p.x;\n          const dy = mouse.y - p.y;\n          const dist = Math.sqrt(dx * dx + dy * dy);\n\n          if (dist < mouse.radius && dist > 1) {\n            const normalizedDist = dist / mouse.radius;\n            const force = Math.pow(1.0 - normalizedDist, 1.5);\n\n            const speedSq = mouse.vx * mouse.vx + mouse.vy * mouse.vy;\n            if (speedSq > 0.1) {\n              fx += mouse.vx * force * 1.8;\n              fy += mouse.vy * force * 1.8;\n            }\n\n            if (interactionMode === \"attract\") {\n              fx += (dx / dist) * force * p.speed * 2.5;\n              fy += (dy / dist) * force * p.speed * 2.5;\n            } else if (interactionMode === \"repel\") {\n              fx -= (dx / dist) * force * p.speed * 3.0;\n              fy -= (dy / dist) * force * p.speed * 3.0;\n            } else if (interactionMode === \"vortex\") {\n              const vx = -dy / dist;\n              const vy = dx / dist;\n              fx += vx * force * p.speed * 3.5;\n              fy += vy * force * p.speed * 3.5;\n            }\n          }\n        }\n\n        p.vx += (fx - p.vx) * 0.08;\n        p.vy += (fy - p.vy) * 0.08;\n\n        p.x += p.vx;\n        p.y += p.vy;\n\n        let wrapped = false;\n        if (p.x < 0) {\n          p.x = width;\n          wrapped = true;\n        } else if (p.x > width) {\n          p.x = 0;\n          wrapped = true;\n        }\n        if (p.y < 0) {\n          p.y = height;\n          wrapped = true;\n        } else if (p.y > height) {\n          p.y = 0;\n          wrapped = true;\n        }\n\n        if (\n          Math.abs(p.x - p.lastX) > width * 0.3 ||\n          Math.abs(p.y - p.lastY) > height * 0.3\n        ) {\n          wrapped = true;\n        }\n\n        p.life++;\n        if (p.life >= p.maxLife) {\n          particles[i] = createParticle(width, height);\n          continue;\n        }\n\n        if (wrapped) {\n          p.lastX = p.x;\n          p.lastY = p.y;\n          continue;\n        }\n\n        const alpha = Math.sin((p.life / p.maxLife) * Math.PI) * 0.85;\n        const mappedColor = getThemeColor(p.color, isDark);\n\n        ctx.beginPath();\n        ctx.moveTo(p.lastX, p.lastY);\n        ctx.lineTo(p.x, p.y);\n        ctx.strokeStyle = mappedColor;\n        ctx.lineWidth = p.size * 3.2;\n        ctx.globalAlpha = alpha * 0.14;\n        ctx.stroke();\n\n        ctx.beginPath();\n        ctx.moveTo(p.lastX, p.lastY);\n        ctx.lineTo(p.x, p.y);\n        ctx.strokeStyle = mappedColor;\n        ctx.lineWidth = p.size * 1.4;\n        ctx.globalAlpha = alpha * 0.7;\n        ctx.stroke();\n\n        ctx.beginPath();\n        ctx.moveTo(p.lastX, p.lastY);\n        ctx.lineTo(p.x, p.y);\n        ctx.strokeStyle = \"#ffffff\";\n        ctx.lineWidth = p.size * 0.6;\n        ctx.globalAlpha = alpha * 0.95;\n        ctx.stroke();\n\n        p.lastX = p.x;\n        p.lastY = p.y;\n      }\n\n      ctx.globalAlpha = 1.0;\n      animationFrameId = requestAnimationFrame(render);\n    };\n\n    render();\n\n    const resizeObserver = new ResizeObserver(() => {\n      resizeCanvas();\n    });\n    if (containerRef.current) {\n      resizeObserver.observe(containerRef.current);\n    }\n\n    return () => {\n      cancelAnimationFrame(animationFrameId);\n      resizeObserver.disconnect();\n      if (interactive) {\n        canvas.removeEventListener(\"mousemove\", handleMouseMove);\n        canvas.removeEventListener(\"mouseleave\", handleMouseLeave);\n        canvas.removeEventListener(\"touchmove\", handleTouchMove);\n        canvas.removeEventListener(\"touchend\", handleTouchEnd);\n      }\n    };\n  }, [\n    colorParticles,\n    particleCount,\n    particleSpeed,\n    particleSize,\n    interactionMode,\n    interactive,\n    trailLength,\n    isInView,\n  ]);\n\n  return (\n    <div\n      ref={containerRef}\n      className={cn(\"absolute inset-0 w-full h-full overflow-hidden pointer-events-none\", className)}\n    >\n      <canvas\n        ref={canvasRef}\n        className=\"absolute inset-0 w-full h-full block pointer-events-auto\"\n        style={{\n          filter: blurAmount > 0 ? `blur(${blurAmount}px)` : \"none\",\n        }}\n      />\n    </div>\n  );\n};\n\nexport default VectorFlow;\n"
    }
  ]
}