{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"wind-blown-sand-grains-over-desert-dunes-particle-background","type":"registry:block","title":"Wind-Blown Sand over Dunes","description":"","dependencies":[],"files":[{"path":"components/we-inc/wind-blown-sand-grains-over-desert-dunes-particle-background.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// Wind-Blown Sand over Dunes: 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 GRAINS_PER_MPX = 1000; // blowing grains per million pixels\nconst GRAIN = \"128,88,44\"; // grain colour, as r,g,b\nconst DUNES = [\"#e6c99a\", \"#d9b47c\", \"#c99c62\"]; // far, middle and near dune colours\nconst WIND = 1; // wind strength: 0.5 for a breeze, 2 for a sandstorm\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 area = (w * h) / 1e6;\n  // Dune ridges as sums of slow waves; the nearest one is also the ground the grains skim.\n  const ridge = (x: number, i: number) => h * (0.62 + i * 0.1) + Math.sin(x / w * 5.1 + i * 2) * h * 0.05 + Math.sin(x / w * 11.7 + i) * h * 0.018;\n  const dunes = document.createElement(\"canvas\");\n  dunes.width = Math.ceil(w);\n  dunes.height = Math.ceil(h);\n  const g = dunes.getContext(\"2d\")!;\n  DUNES.forEach((col, i) => {\n    g.fillStyle = col;\n    g.beginPath();\n    g.moveTo(0, h);\n    for (let x = 0; x <= w + 8; x += 8) g.lineTo(x, ridge(x, i));\n    g.lineTo(w, h);\n    g.fill();\n    const shade = g.createLinearGradient(0, h * (0.6 + i * 0.1), 0, h);\n    shade.addColorStop(0, \"rgba(255,244,220,0.35)\");\n    shade.addColorStop(1, \"rgba(120,80,40,0.12)\");\n    g.fillStyle = shade;\n    g.fill();\n  });\n  const ps = Array.from({ length: Math.min(1400, Math.round(area * GRAINS_PER_MPX)) }, () => ({\n    x: Math.random() * w, up: Math.random() ** 3 * h * 0.22, z: Math.random(), s: Math.random() * 99,\n  }));\n  let t0 = 0;\n  const wind = (x: number) => (0.55 + 0.45 * Math.sin(x / w * 4 - t0 * 0.9)) * (0.7 + 0.3 * Math.sin(t0 * 0.21));\n  return {\n    update(dt, t) {\n      t0 = t;\n      for (const p of ps) {\n        const near = Math.max(0, 1 - p.up / (h * 0.22));\n        p.x += (60 + 260 * near) * (0.4 + p.z) * wind(p.x) * WIND * dt;\n        p.up += Math.sin(t * 3 + p.s) * 10 * dt;\n        if (p.up < 0) p.up = 0;\n        if (p.x > w + 20) { p.x = -20; p.up = Math.random() ** 3 * h * 0.22; }\n      }\n    },\n    draw(c) {\n      c.clearRect(0, 0, w, h);\n      c.globalAlpha = 1;\n      c.drawImage(dunes, 0, 0, w, h);\n      c.fillStyle = `rgb(${GRAIN})`;\n      for (const p of ps) {\n        const y = ridge(p.x, 2) - p.up - 2;\n        const speed = wind(p.x);\n        const len = 1.2 + p.z * 1.8 + speed * p.z * 10;\n        c.globalAlpha = (0.18 + p.z * 0.5) * (0.5 + speed * 0.5);\n        c.fillRect(p.x - len, y, len, 0.8 + p.z * 1.4);\n      }\n      c.globalAlpha = 1;\n    },\n  };\n}\n\nexport default function WindBlownSandDunes() {\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=\"snd-bg relative isolate flex min-h-[560px] items-center overflow-hidden px-6 py-20 sm:px-12\">\n      <style>{`.snd-bg{background:radial-gradient(60% 50% at 80% 0%, #fbf3e2 0%, rgba(251,243,226,0) 70%), linear-gradient(180deg, #f3e7cf 0%, #efdfc0 55%, #e7d0a6 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-[#7a4a17]\">Dune Line Expeditions</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.08] tracking-tight text-[#2e2112] sm:text-5xl\">Three nights in the dunes, guided by people who grew up there.</h2>\n          <p className=\"mt-5 text-lg leading-relaxed text-[#5a4429]\">Small groups, shaded camps and water carried for you. Routes shift with the wind every season.</p>\n          <a\n            href=\"#\"\n            className=\"mt-8 inline-flex h-12 items-center rounded-md bg-[#2e2112] px-6 text-sm font-semibold text-[#f6ead2] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#2e2112] focus-visible:ring-offset-2 focus-visible:ring-offset-[#f3e7cf]\"\n          >\n            View crossing dates\n          </a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
