{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dual-sparks","type":"registry:ui","title":"Dual Sparks","description":"Two streams of sparks rush in from opposite edges and burst against each other in a glowing core. Canvas 2D.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/dual-sparks.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface DualSparksProps {\n  /** Color of the stream entering from the left. */\n  leftColor?: string;\n  /** Color of the stream entering from the right. */\n  rightColor?: string;\n  /** Sparks spawned per second on each side. */\n  density?: number;\n  /** Flow speed multiplier; 0 freezes the sparks. */\n  speed?: number;\n  /** Brightness of the collision glow. */\n  glow?: number;\n  /** How far the streams fan out vertically, 0 to 1. */\n  spread?: number;\n  className?: string;\n}\n\nfunction rgb(hex: string) {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, \"$&$&\");\n  const n = parseInt(h.padEnd(6, \"0\").slice(0, 6), 16) || 0;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\ninterface Spark {\n  x: number;\n  y: number;\n  vx: number;\n  vy: number;\n  life: number;\n  max: number;\n  side: 0 | 1;\n  burst: boolean;\n  seed: number;\n  width: number;\n}\n\nconst MAX_SPARKS = 1400;\n\nexport function DualSparks({\n  leftColor = \"#38bdf8\",\n  rightColor = \"#f472b6\",\n  density = 90,\n  speed = 1,\n  glow = 1,\n  spread = 0.5,\n  className,\n}: DualSparksProps) {\n  const ref = useRef<HTMLCanvasElement>(null);\n  const opts = useRef({ l: rgb(leftColor), r: rgb(rightColor), density, speed, glow, spread });\n  const redraw = useRef<() => void>(undefined);\n\n  useEffect(() => {\n    opts.current = { l: rgb(leftColor), r: rgb(rightColor), density, speed, glow, spread };\n    redraw.current?.();\n  }, [leftColor, rightColor, density, speed, glow, spread]);\n\n  useEffect(() => {\n    const canvas = ref.current!;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const sparks: Spark[] = [];\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let heat = 0;\n    let carry = 0;\n    let raf = 0;\n    let last = 0;\n\n    const gauss = () => (Math.random() + Math.random() + Math.random() - 1.5) / 1.5;\n\n    const spawn = (side: 0 | 1) => {\n      if (sparks.length >= MAX_SPARKS) return;\n      const o = opts.current;\n      const v = (260 + Math.random() * 220) * (w / 1280 + 0.35);\n      sparks.push({\n        x: side ? w + 8 : -8,\n        y: h / 2 + gauss() * h * 0.45 * o.spread,\n        vx: side ? -v : v,\n        vy: gauss() * 30,\n        life: 0,\n        max: 6,\n        side,\n        burst: false,\n        seed: Math.random() * 100,\n        width: 0.6 + Math.random() * 1.3,\n      });\n    };\n\n    const step = (dt: number) => {\n      const o = opts.current;\n      t += dt;\n      const cx = w / 2;\n      const cy = h / 2;\n      carry += o.density * dt * (h / 800 + 0.4);\n      while (carry >= 1) {\n        carry -= 1;\n        spawn(0);\n        spawn(1);\n      }\n      heat *= Math.exp(-dt * 3);\n      for (let i = sparks.length - 1; i >= 0; i--) {\n        const s = sparks[i];\n        s.life += dt;\n        if (!s.burst) {\n          // Funnel toward the center line while wobbling like a current.\n          const dx = Math.abs(cx - s.x) / Math.max(cx, 1);\n          s.vy += ((cy - s.y) * (2.4 - dx * 1.6) + Math.sin(s.x * 0.012 + t * 2.2 + s.seed) * 70) * dt;\n          s.vy *= Math.exp(-dt * 1.4);\n          s.vx *= 1 + dt * 0.35;\n          const reached = s.side ? s.x <= cx + 6 * Math.sin(s.seed) : s.x >= cx - 6 * Math.sin(s.seed);\n          if (reached) {\n            s.burst = true;\n            s.life = 0;\n            s.max = 0.35 + Math.random() * 0.8;\n            const a = Math.random() * Math.PI * 2;\n            const sp = 60 + Math.random() * Math.random() * 520;\n            // Sparks keep a little of their momentum, so each side sprays past the other.\n            s.vx = Math.cos(a) * sp + s.vx * 0.25;\n            s.vy = Math.sin(a) * sp * 0.75 + s.vy * 0.3;\n            heat = Math.min(heat + 0.012, 1.6);\n          }\n        } else {\n          s.vx *= Math.exp(-dt * 2.6);\n          s.vy = s.vy * Math.exp(-dt * 2.6) + 40 * dt;\n        }\n        s.x += s.vx * dt;\n        s.y += s.vy * dt;\n        if (s.life > s.max || s.x < -40 || s.x > w + 40) sparks.splice(i, 1);\n      }\n    };\n\n    const render = () => {\n      const o = opts.current;\n      const cx = w / 2;\n      const cy = h / 2;\n      ctx.clearRect(0, 0, w, h);\n      ctx.globalCompositeOperation = \"lighter\";\n      const [lr, lg, lb] = o.l;\n      const [rr, rg, rb] = o.r;\n      const mr = (lr + rr) >> 1;\n      const mg = (lg + rg) >> 1;\n      const mb = (lb + rb) >> 1;\n\n      // Soft elliptical haze behind each stream.\n      const squash = Math.max(0.12, o.spread * 0.9) * (h / Math.max(w, 1));\n      for (const [side, [r, g, b]] of [o.l, o.r].entries()) {\n        const hx = side ? cx * 1.45 : cx * 0.55;\n        ctx.save();\n        ctx.translate(hx, cy);\n        ctx.scale(1, squash);\n        const haze = ctx.createRadialGradient(0, 0, 0, 0, 0, cx * 0.75);\n        haze.addColorStop(0, `rgba(${r},${g},${b},${0.14 * o.glow})`);\n        haze.addColorStop(1, `rgba(${r},${g},${b},0)`);\n        ctx.fillStyle = haze;\n        ctx.fillRect(-cx, -cx, cx * 2, cx * 2);\n        ctx.restore();\n      }\n\n      ctx.lineCap = \"round\";\n      for (const s of sparks) {\n        const fade = s.burst ? 1 - s.life / s.max : Math.min(1, s.life * 3);\n        if (fade <= 0) continue;\n        const c = s.side ? o.r : o.l;\n        const whiten = s.burst ? 0.55 * (1 - s.life / s.max) : 0;\n        const r = Math.round(c[0] + (255 - c[0]) * whiten);\n        const g = Math.round(c[1] + (255 - c[1]) * whiten);\n        const b = Math.round(c[2] + (255 - c[2]) * whiten);\n        const tail = s.burst ? 0.045 : 0.03;\n        ctx.strokeStyle = `rgba(${r},${g},${b},${fade * 0.9})`;\n        ctx.lineWidth = s.width * (s.burst ? 1.1 : 1);\n        ctx.beginPath();\n        ctx.moveTo(s.x - s.vx * tail, s.y - s.vy * tail);\n        ctx.lineTo(s.x, s.y);\n        ctx.stroke();\n      }\n\n      // Collision core.\n      const pulse = 0.85 + 0.15 * Math.sin(t * 5.3) * Math.sin(t * 3.1);\n      const core = Math.min(w, h) * (0.16 + 0.06 * Math.min(heat, 1)) * pulse;\n      const g = ctx.createRadialGradient(cx, cy, 0, cx, cy, core * 2.4);\n      const k = o.glow * (0.55 + 0.35 * Math.min(heat, 1));\n      g.addColorStop(0, `rgba(255,255,255,${Math.min(1, 0.9 * k)})`);\n      g.addColorStop(0.08, `rgba(${(mr + 255) >> 1},${(mg + 255) >> 1},${(mb + 255) >> 1},${Math.min(1, 0.6 * k)})`);\n      g.addColorStop(0.3, `rgba(${mr},${mg},${mb},${0.12 * k})`);\n      g.addColorStop(1, `rgba(${mr},${mg},${mb},0)`);\n      ctx.fillStyle = g;\n      ctx.fillRect(cx - core * 2.4, cy - core * 2.4, core * 4.8, core * 4.8);\n      ctx.globalCompositeOperation = \"source-over\";\n    };\n\n    const loop = (now: number) => {\n      const dt = last ? Math.min((now - last) / 1000, 0.05) : 0;\n      last = now;\n      step(dt * opts.current.speed);\n      render();\n      raf = requestAnimationFrame(loop);\n    };\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      const first = !w;\n      w = canvas.offsetWidth;\n      h = canvas.offsetHeight;\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      // Pre-warm so the streams are already flowing on the first frame.\n      if (first && w) for (let i = 0; i < 150; i++) step(1 / 60);\n      render();\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    const io = new IntersectionObserver(([e]) => {\n      cancelAnimationFrame(raf);\n      raf = 0;\n      last = 0;\n      if (e.isIntersecting && !reduced) raf = requestAnimationFrame(loop);\n    });\n    io.observe(canvas);\n    redraw.current = () => {\n      if (!raf) render();\n    };\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      redraw.current = undefined;\n    };\n  }, []);\n\n  return (\n    <canvas\n      ref={ref}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\", pointerEvents: \"none\" }}\n    />\n  );\n}\n"}]}