{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"falling-snow-particle-background-with-depth-and-wind-gusts","type":"registry:block","title":"Falling Snow with Depth and Gusts","description":"","dependencies":[],"files":[{"path":"components/we-inc/falling-snow-particle-background-with-depth-and-wind-gusts.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// Falling Snow with Depth and Gusts: 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 FLAKES_PER_MPX = 420; // snowfall density per million pixels\nconst SNOW = \"238,243,250\"; // flake colour, as r,g,b\nconst WIND = 1; // gust strength: 0 for still air, 2 for a blizzard\nconst FALL_SPEED = 1; // how fast flakes sink\nconst WARMUP = 150; // 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.min(760, Math.round(((w * h) / 1e6) * FLAKES_PER_MPX));\n  const crisp = glow(16, SNOW, [[0, 1], [0.4, 0.85], [1, 0]]);\n  const soft = glow(64, SNOW, [[0, 0.55], [0.45, 0.22], [1, 0]]);\n  const ps = Array.from({ length: n }, () => ({ x: Math.random() * w, y: Math.random() * h, z: Math.random() ** 1.4, s: Math.random() * 99 }));\n  let gust = 0;\n  return {\n    update(dt, t) {\n      // Wind is a mix of slow swells plus a rare, strong gust.\n      const target = (Math.sin(t * 0.13) * 0.5 + Math.sin(t * 0.37 + 1.3) * 0.3 + Math.max(0, Math.sin(t * 0.11 - 1)) ** 6 * 2.4) * 46 * WIND;\n      gust += (target - gust) * Math.min(1, dt * 0.9);\n      for (const p of ps) {\n        const depth = 0.25 + p.z * 1.25;\n        p.y += (12 + p.z * 48) * FALL_SPEED * dt;\n        p.x += (gust * depth + Math.sin(t * 0.7 + p.s) * (5 + p.z * 12)) * dt;\n        if (p.y > h + 40) { p.y = -40; p.x = Math.random() * w; }\n        if (p.x > w + 40) p.x -= w + 80; else if (p.x < -40) p.x += w + 80;\n      }\n    },\n    draw(c) {\n      c.clearRect(0, 0, w, h);\n      for (const p of ps) {\n        if (p.z > 0.9) {\n          const s = 14 + (p.z - 0.9) * 280;\n          c.globalAlpha = 0.5;\n          c.drawImage(soft, p.x - s / 2, p.y - s / 2, s, s);\n        } else {\n          const s = 1.6 + p.z * 5.5;\n          c.globalAlpha = 0.3 + p.z * 0.65;\n          c.drawImage(crisp, p.x - s / 2, p.y - s / 2, s, s);\n        }\n      }\n      c.globalAlpha = 1;\n    },\n  };\n}\n\nexport default function FallingSnowDepthGusts() {\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=\"snw-bg relative isolate flex min-h-[560px] items-center overflow-hidden px-6 py-20 sm:px-12\">\n      <style>{`.snw-bg{background:radial-gradient(90% 60% at 50% 115%, #5b6878 0%, rgba(91,104,120,0) 70%), linear-gradient(180deg, #19212c 0%, #283342 60%, #3a4655 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-[#cbd5e1]\">Hollow Pine Lodge</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.08] tracking-tight text-[#f1f5f9] sm:text-5xl\">Wake up to fresh snow and a fire already lit.</h2>\n          <p className=\"mt-5 text-lg leading-relaxed text-[#cbd5e1]\">Timber cabins with wood stoves, a cedar sauna and groomed trails that start at your door.</p>\n          <a\n            href=\"#\"\n            className=\"mt-8 inline-flex h-12 items-center rounded-full bg-[#f8fafc] px-6 text-sm font-semibold text-[#1e293b] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#f1f5f9] focus-visible:ring-offset-2 focus-visible:ring-offset-[#1f2833]\"\n          >\n            Check winter dates\n          </a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
