{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-fireflies",
  "type": "registry:component",
  "title": "Fireflies",
  "description": "Warm fireflies wander over a dusk meadow and blink on their own rhythms. Distant ones glow behind the grass and near ones float in front of it.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "fireflies",
    "night",
    "glow",
    "canvas",
    "nature"
  ],
  "docs": "https://hyperblur.dev/effects/bg-fireflies",
  "files": [
    {
      "path": "registry/hyperblur/bg-fireflies.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-fireflies.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgFirefliesProps extends React.HTMLAttributes<HTMLDivElement> {\n  accent?: string;\n  background?: string;\n  horizon?: string;\n  color?: string;\n  count?: number;\n  meadow?: boolean;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-ve11 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-ve11-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ve11-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 BgFireflies({\n  accent = \"#ffd36b\",\n  background = \"#060912\",\n  horizon = \"#0f1c1a\",\n  color = \"#020403\",\n  count = 46,\n  meadow = true,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgFirefliesProps) {\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    if (!wrap || !canvas || !ctx) return;\n    const g: CanvasRenderingContext2D = ctx;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n    const land = document.createElement(\"canvas\");\n    const lctx = land.getContext(\"2d\");\n    if (!lctx) return;\n    const L: CanvasRenderingContext2D = lctx;\n\n    const ac = rgbOf(accent, [255, 211, 107]);\n    const sprite = document.createElement(\"canvas\");\n    sprite.width = 96;\n    sprite.height = 96;\n    const sp = sprite.getContext(\"2d\");\n    if (sp) {\n      const rg = sp.createRadialGradient(48, 48, 0, 48, 48, 48);\n      rg.addColorStop(0, \"rgba(255,255,240,1)\");\n      rg.addColorStop(0.08, `rgba(${ac[0]},${ac[1]},${ac[2]},0.95)`);\n      rg.addColorStop(0.3, `rgba(${ac[0]},${ac[1]},${ac[2]},0.28)`);\n      rg.addColorStop(1, `rgba(${ac[0]},${ac[1]},${ac[2]},0)`);\n      sp.fillStyle = rg;\n      sp.fillRect(0, 0, 96, 96);\n    }\n\n    const n = Math.max(4, Math.min(220, Math.round(count)));\n    const flies = Array.from({ length: n }, (_, i) => ({\n      x: rnd(i * 9 + 1),\n      y: 0.28 + rnd(i * 9 + 2) * 0.66,\n      z: 0.35 + rnd(i * 9 + 3) * 0.65,\n      ax: 0.03 + rnd(i * 9 + 4) * 0.07,\n      ay: 0.02 + rnd(i * 9 + 5) * 0.05,\n      fx: 0.12 + rnd(i * 9 + 6) * 0.25,\n      fy: 0.15 + rnd(i * 9 + 7) * 0.3,\n      ph: rnd(i * 9 + 8) * 6.283,\n      period: 2.4 + rnd(i * 9 + 9) * 3.6,\n      on: 0.5 + rnd(i * 13 + 2) * 0.6,\n    }));\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n\n    function paintLand(dpr: number) {\n      land.width = Math.max(1, Math.round(w * dpr));\n      land.height = Math.max(1, Math.round(h * dpr));\n      L.setTransform(dpr, 0, 0, dpr, 0, 0);\n      L.clearRect(0, 0, w, h);\n      if (!meadow) return;\n      L.fillStyle = color;\n      L.beginPath();\n      L.moveTo(0, h);\n      const base = h * 0.8;\n      for (let x = 0, i = 0; x <= w + 12; x += 12, i++) {\n        const y = base - (Math.sin(x * 0.011 + 1.3) * 0.5 + 0.5) * h * 0.08 - rnd(i * 5 + 700) * h * 0.05;\n        L.lineTo(x, y);\n      }\n      L.lineTo(w, h);\n      L.closePath();\n      L.globalAlpha = 0.55;\n      L.fill();\n      L.globalAlpha = 1;\n      L.beginPath();\n      L.moveTo(0, h);\n      const blades = Math.round(w / 3);\n      for (let i = 0; i < blades; i++) {\n        const x = (i / blades) * w + (rnd(i + 91) - 0.5) * 4;\n        const bh = h * (0.05 + rnd(i * 3 + 17) * 0.11);\n        const lean = (rnd(i * 7 + 5) - 0.5) * bh * 0.6;\n        const y0 = h * 0.93;\n        L.moveTo(x - 1.6, y0);\n        L.quadraticCurveTo(x + lean * 0.3, y0 - bh * 0.6, x + lean, y0 - bh);\n        L.quadraticCurveTo(x + lean * 0.3 + 0.6, y0 - bh * 0.55, x + 1.6, y0);\n      }\n      L.rect(0, h * 0.93 - 1, w, h * 0.07 + 2);\n      L.fill();\n    }\n\n    function glow(f: (typeof flies)[number]) {\n      const cyc = (t + f.ph * f.period) % f.period;\n      const k = cyc < f.on ? Math.sin((cyc / f.on) * Math.PI) : 0;\n      const x = (f.x + f.ax * Math.sin(t * f.fx + f.ph) + 0.02 * Math.sin(t * 1.3 + f.ph * 2)) * w;\n      const y = (f.y + f.ay * Math.cos(t * f.fy + f.ph) + 0.015 * Math.sin(t * 1.7 + f.ph)) * h;\n      const b = 0.12 + k * 0.88;\n      const s = (10 + 26 * k) * f.z;\n      g.globalAlpha = b * (0.45 + f.z * 0.55);\n      g.drawImage(sprite, x - s, y - s, s * 2, s * 2);\n    }\n\n    function render() {\n      if (!w || !h) return;\n      const sky = g.createLinearGradient(0, 0, 0, h);\n      sky.addColorStop(0, background);\n      sky.addColorStop(1, horizon);\n      g.globalAlpha = 1;\n      g.globalCompositeOperation = \"source-over\";\n      g.fillStyle = sky;\n      g.fillRect(0, 0, w, h);\n      g.globalCompositeOperation = \"lighter\";\n      for (const f of flies) if (f.z < 0.62) glow(f);\n      g.globalCompositeOperation = \"source-over\";\n      g.globalAlpha = 1;\n      g.drawImage(land, 0, 0, w, h);\n      g.globalCompositeOperation = \"lighter\";\n      for (const f of flies) if (f.z >= 0.62) glow(f);\n      g.globalCompositeOperation = \"source-over\";\n      g.globalAlpha = 1;\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 = 7.3;\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      paintLand(dpr);\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(el);\n    io.observe(el);\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  }, [accent, background, horizon, color, count, meadow, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ve11 ${className}`} style={{ background, ...style }} {...rest}>\n      <style href=\"hb-bg-fireflies\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ve11-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-ve11-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}