{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"rising-embers-over-a-dark-hearth-particle-background","type":"registry:block","title":"Rising Embers over a Dark Hearth","description":"","dependencies":[],"files":[{"path":"components/we-inc/rising-embers-over-a-dark-hearth-particle-background.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// Rising Embers over a Dark Hearth: 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 EMBERS_PER_MPX = 220; // embers in the air per million pixels\nconst HOT = \"255,236,190\"; // fresh ember colour\nconst WARM = \"255,150,62\"; // cooling ember colour\nconst COOL = \"196,58,24\"; // nearly spent ember colour\nconst HEAT = 1; // rise speed: 0.6 for a dying fire, 1.6 for a roaring one\nconst WARMUP = 180; // 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(40, Math.min(420, Math.round(((w * h) / 1e6) * EMBERS_PER_MPX)));\n  const tints = [HOT, WARM, COOL].map((rgb) => glow(32, rgb, [[0, 1], [0.15, 0.9], [0.4, 0.25], [1, 0]]));\n  const bed = glow(256, WARM, [[0, 0.32], [0.4, 0.12], [1, 0]]);\n  const spawn = (p: { x: number; y: number; vy: number; life: number; age: number; z: number; s: number }) => {\n    p.x = w * (0.5 + (Math.random() - 0.5) * 0.8 * (0.4 + Math.random() * 0.6));\n    p.y = h + 10;\n    p.z = Math.random();\n    p.vy = -(35 + Math.random() * 70) * (0.5 + p.z * 0.7);\n    p.life = 2.5 + Math.random() * 4;\n    p.age = 0;\n    p.s = Math.random() * 99;\n  };\n  const ps = Array.from({ length: n }, () => {\n    const p = { x: 0, y: 0, vy: 0, life: 1, age: 0, z: 0, s: 0 };\n    spawn(p);\n    p.age = Math.random() * p.life;\n    p.y = h + p.vy * p.age;\n    return p;\n  });\n  return {\n    update(dt, t) {\n      for (const p of ps) {\n        p.age += dt;\n        if (p.age > p.life || p.y < -20) { spawn(p); continue; }\n        p.y += p.vy * HEAT * dt;\n        p.x += (Math.sin(t * 1.1 + p.y * 0.012 + p.s) * 22 + Math.sin(t * 3.1 + p.s * 2) * 8) * (0.4 + p.z) * dt;\n      }\n    },\n    draw(c, t) {\n      c.clearRect(0, 0, w, h);\n      c.globalCompositeOperation = \"lighter\";\n      const breathe = 0.8 + 0.12 * Math.sin(t * 1.7) + 0.08 * Math.sin(t * 4.3);\n      c.globalAlpha = breathe;\n      c.drawImage(bed, w * 0.05, h * 0.62, w * 0.9, h * 0.76);\n      for (const p of ps) {\n        const f = p.age / p.life;\n        const tint = tints[f < 0.25 ? 0 : f < 0.6 ? 1 : 2];\n        const flick = 0.65 + 0.35 * Math.sin(t * 17 + p.s * 7);\n        const s = (4 + p.z * 12) * (1 - f * 0.5);\n        c.globalAlpha = Math.min(1, (1 - f) * 1.4) * flick * (0.35 + p.z * 0.65);\n        c.drawImage(tint, p.x - s / 2, p.y - s * 0.8, s, s * 1.6);\n      }\n      c.globalAlpha = 1;\n      c.globalCompositeOperation = \"source-over\";\n    },\n  };\n}\n\nexport default function RisingEmbersDarkHearth() {\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=\"emb-bg relative isolate flex min-h-[560px] items-center overflow-hidden px-6 py-20 sm:px-12\">\n      <style>{`.emb-bg{background:radial-gradient(70% 45% at 50% 110%, #5a2210 0%, rgba(90,34,16,0) 70%), linear-gradient(180deg, #0c0806 0%, #160c08 70%, #23110a 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-[#f2b98a]\">Ashgrove Wood-Fired Kitchen</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.08] tracking-tight text-[#fbeee0] sm:text-5xl\">Everything on the menu meets the fire first.</h2>\n          <p className=\"mt-5 text-lg leading-relaxed text-[#e0c8b0]\">Bread from the oak oven, fish cooked over embers and vegetables pulled straight from the coals. Open Wednesday to Sunday.</p>\n          <a\n            href=\"#\"\n            className=\"mt-8 inline-flex h-12 items-center rounded-full bg-[#f0a35e] px-6 text-sm font-semibold text-[#1a0d07] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#fbeee0] focus-visible:ring-offset-2 focus-visible:ring-offset-[#140b07]\"\n          >\n            Book a table\n          </a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
