{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"fireflies-over-a-dusk-meadow-particle-background","type":"registry:block","title":"Fireflies over a Dusk Meadow","description":"","dependencies":[],"files":[{"path":"components/we-inc/fireflies-over-a-dusk-meadow-particle-background.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// Fireflies over a Dusk Meadow: a canvas 2D particle background behind a small hero.\n// Sprites are drawn once to small offscreen canvases, motion is scaled by frame\n// time, the loop stops off screen, and reduced motion gets one settled frame.\n// Values worth changing:\nconst FLIES_PER_MPX = 120; // fireflies per million pixels\nconst GLOW = \"222,240,128\"; // firefly light, as r,g,b\nconst NEAR_GRASS = \"#0a120e\"; // front grass silhouette\nconst FAR_GRASS = \"#16241e\"; // back grass silhouette\nconst BLINK_RATE = 1; // 0.5 for lazy blinking, 2 for busy\nconst WARMUP = 60; // frames simulated before the first paint\n\ntype Ptr = { x: number; y: number; on: boolean };\ntype Scene = { update: (dt: number, t: number) => void; draw: (c: CanvasRenderingContext2D, t: number) => void };\n\nfunction glow(size: number, rgb: string, stops: number[][]): HTMLCanvasElement {\n  const c = document.createElement(\"canvas\");\n  c.width = c.height = size;\n  const g = c.getContext(\"2d\")!;\n  const r = size / 2;\n  const grd = g.createRadialGradient(r, r, 0, r, r, r);\n  for (const [o, a] of stops) grd.addColorStop(o, `rgba(${rgb},${a})`);\n  g.fillStyle = grd;\n  g.fillRect(0, 0, size, size);\n  return c;\n}\n\nfunction mount(canvas: HTMLCanvasElement, root: HTMLElement): () => void {\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) return () => {};\n  const still = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  const ptr: Ptr = { x: -1e4, y: -1e4, on: false };\n  let w = 0, h = 0, t = 0, last = 0, raf = 0, seen = true;\n  let scene: Scene | null = null;\n  const fit = () => {\n    const r = canvas.getBoundingClientRect();\n    if (scene && Math.abs(r.width - w) < 1 && Math.abs(r.height - h) < 1) return;\n    const dpr = Math.min(window.devicePixelRatio || 1, 1.5);\n    w = Math.max(1, r.width);\n    h = Math.max(1, r.height);\n    canvas.width = Math.round(w * dpr);\n    canvas.height = Math.round(h * dpr);\n    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n    scene = createScene(w, h, ptr);\n    for (let i = 0; i < WARMUP; i++) { t += 1 / 30; scene.update(1 / 30, t); }\n    scene.draw(ctx, t);\n  };\n  const frame = (now: number) => {\n    const dt = Math.min(0.05, Math.max(0, (now - last) / 1000));\n    last = now;\n    t += dt;\n    if (scene) { scene.update(dt, t); scene.draw(ctx, t); }\n    raf = requestAnimationFrame(frame);\n  };\n  const run = () => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n    if (!still && seen && !document.hidden) { last = performance.now(); raf = requestAnimationFrame(frame); }\n  };\n  const ro = new ResizeObserver(fit);\n  ro.observe(canvas);\n  const io = new IntersectionObserver((es) => { seen = es[es.length - 1].isIntersecting; run(); });\n  io.observe(canvas);\n  document.addEventListener(\"visibilitychange\", run);\n  fit();\n  run();\n  return () => {\n    cancelAnimationFrame(raf);\n    ro.disconnect();\n    io.disconnect();\n    document.removeEventListener(\"visibilitychange\", run);\n  };\n}\n\nfunction createScene(w: number, h: number, _ptr: Ptr): Scene {\n  const n = Math.max(14, Math.min(140, Math.round(((w * h) / 1e6) * FLIES_PER_MPX)));\n  const light = glow(96, GLOW, [[0, 1], [0.06, 0.9], [0.18, 0.3], [0.5, 0.06], [1, 0]]);\n  const grass = document.createElement(\"canvas\");\n  grass.width = Math.ceil(w);\n  grass.height = Math.ceil(h);\n  const g = grass.getContext(\"2d\")!;\n  g.lineCap = \"round\";\n  const blades = (count: number, color: string, len: number) => {\n    g.strokeStyle = color;\n    g.fillStyle = color;\n    g.fillRect(0, h - len * 0.12, w, len * 0.12 + 2);\n    for (let i = 0; i < count; i++) {\n      const x = Math.random() * w;\n      const lh = len * (0.45 + Math.random() * 0.75);\n      const bend = (Math.random() - 0.5) * lh * 0.7;\n      g.lineWidth = 1 + Math.random() * 2;\n      g.beginPath();\n      g.moveTo(x, h + 2);\n      g.quadraticCurveTo(x + bend * 0.2, h - lh * 0.6, x + bend, h - lh);\n      g.stroke();\n    }\n  };\n  blades(Math.round(w * 0.55), FAR_GRASS, h * 0.34);\n  blades(Math.round(w * 0.9), NEAR_GRASS, h * 0.2);\n  const ps = Array.from({ length: n }, () => ({\n    x: Math.random() * w, y: h * (0.3 + Math.random() * 0.66), z: Math.random(),\n    a: Math.random() * Math.PI * 2, ph: Math.random() * 9, per: 3 + Math.random() * 4,\n  }));\n  const fly = (c: CanvasRenderingContext2D, p: (typeof ps)[number], t: number) => {\n    const k = ((t * BLINK_RATE) / p.per + p.ph) % 1;\n    const on = k < 0.35 ? Math.sin((k / 0.35) * Math.PI) : 0;\n    const b = 0.12 + 0.88 * on;\n    const s = (14 + p.z * 40) * (0.7 + 0.3 * on);\n    c.globalAlpha = b * (0.45 + p.z * 0.55);\n    c.drawImage(light, p.x - s / 2, p.y - s / 2, s, s);\n  };\n  return {\n    update(dt) {\n      for (const p of ps) {\n        p.a += (Math.random() - 0.5) * 3 * dt;\n        const v = (8 + p.z * 14) * dt;\n        p.x += Math.cos(p.a) * v;\n        p.y += Math.sin(p.a) * v * 0.6;\n        if (p.y < h * 0.25) p.a = Math.abs(p.a);\n        if (p.y > h * 0.98) p.a = -Math.abs(p.a);\n        if (p.x < -30) p.x += w + 60; else if (p.x > w + 30) p.x -= w + 60;\n      }\n    },\n    draw(c, t) {\n      c.clearRect(0, 0, w, h);\n      c.globalCompositeOperation = \"lighter\";\n      for (const p of ps) if (p.z < 0.45) fly(c, p, t);\n      c.globalCompositeOperation = \"source-over\";\n      c.globalAlpha = 1;\n      c.drawImage(grass, 0, 0, w, h);\n      c.globalCompositeOperation = \"lighter\";\n      for (const p of ps) if (p.z >= 0.45) fly(c, p, t);\n      c.globalCompositeOperation = \"source-over\";\n      c.globalAlpha = 1;\n    },\n  };\n}\n\nexport default function FirefliesDuskMeadow() {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const rootRef = useRef<HTMLElement>(null);\n\n  useEffect(() => {\n    if (!canvasRef.current || !rootRef.current) return;\n    return mount(canvasRef.current, rootRef.current);\n  }, []);\n\n  return (\n    <section ref={rootRef} className=\"ffm-bg relative isolate flex min-h-[560px] items-center overflow-hidden px-6 py-20 sm:px-12\">\n      <style>{`.ffm-bg{background:linear-gradient(180deg, #273746 0%, #22332f 45%, #121d18 100%)}`}</style>\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 -z-10 h-full w-full\" />\n      <div className=\"mx-auto w-full max-w-6xl\">\n        <div className=\"max-w-xl\">\n          <p className=\"text-sm font-semibold uppercase tracking-[0.14em] text-[#dfe9a8]\">Wren Hollow Campground</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.08] tracking-tight text-[#f4f1de] sm:text-5xl\">Stay out past dark. The meadow lights up.</h2>\n          <p className=\"mt-5 text-lg leading-relaxed text-[#d5d3bf]\">Quiet walk-in sites along the creek, no generators after nine, and a ranger-led night walk on summer Fridays.</p>\n          <a\n            href=\"#\"\n            className=\"mt-8 inline-flex h-12 items-center rounded-full bg-[#e1ea9b] px-6 text-sm font-semibold text-[#13201a] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#f4f1de] focus-visible:ring-offset-2 focus-visible:ring-offset-[#22332f]\"\n          >\n            Reserve a site\n          </a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
