{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dust-motes-drifting-in-a-shaft-of-sunlight-background","type":"registry:block","title":"Dust Motes in a Sunlight Shaft","description":"","dependencies":[],"files":[{"path":"components/we-inc/dust-motes-drifting-in-a-shaft-of-sunlight-background.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// Dust Motes in a Sunlight Shaft: 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 MOTES_PER_MPX = 320; // dust density: motes per million pixels of area\nconst LIGHT = \"255,224,168\"; // colour of the sunlight and the lit dust, as r,g,b\nconst DRIFT_SPEED = 1; // 0.5 for still air, 2 for a draughty room\nconst BEAM_STRENGTH = 0.014; // brightness of the light shaft itself\nconst WARMUP = 90; // frames simulated before the first paint, so it never starts empty\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(560, Math.round(((w * h) / 1e6) * MOTES_PER_MPX));\n  const fine = glow(16, LIGHT, [[0, 1], [0.3, 0.75], [1, 0]]);\n  const blur = glow(64, LIGHT, [[0, 0.4], [0.55, 0.14], [1, 0]]);\n  // The shaft enters at the top between tx0 and tx1 and lands on the floor between bx0 and bx1.\n  const tx0 = w * 0.52, tx1 = w * 0.84, bx0 = w * 0.3, bx1 = w * 0.8;\n  const beam = document.createElement(\"canvas\");\n  beam.width = Math.ceil(w / 2);\n  beam.height = Math.ceil(h / 2);\n  const b = beam.getContext(\"2d\")!;\n  b.scale(0.5, 0.5);\n  const m0 = (tx0 + tx1) / 2, m1 = (bx0 + bx1) / 2;\n  for (let i = 0; i < 24; i++) {\n    const k = (i / 24) * 0.85;\n    const g = b.createLinearGradient(0, 0, 0, h);\n    g.addColorStop(0, `rgba(${LIGHT},${BEAM_STRENGTH})`);\n    g.addColorStop(1, `rgba(${LIGHT},${BEAM_STRENGTH * 0.3})`);\n    b.fillStyle = g;\n    b.beginPath();\n    b.moveTo(tx0 + (m0 - tx0) * k, 0);\n    b.lineTo(tx1 - (tx1 - m0) * k, 0);\n    b.lineTo(bx1 - (bx1 - m1) * k, h);\n    b.lineTo(bx0 + (m1 - bx0) * k, h);\n    b.fill();\n  }\n  const pool = b.createRadialGradient(m1, h, 0, m1, h, w * 0.32);\n  pool.addColorStop(0, `rgba(${LIGHT},0.16)`);\n  pool.addColorStop(1, `rgba(${LIGHT},0)`);\n  b.fillStyle = pool;\n  b.fillRect(0, 0, w, h);\n  const ps = Array.from({ length: n }, () => ({\n    x: Math.random() * w, y: Math.random() * h, z: Math.random() ** 1.5, vx: 0, vy: 0, s: Math.random() * 99,\n  }));\n  const lit = (x: number, y: number) => {\n    const f = y / h, l = tx0 + (bx0 - tx0) * f, r = tx1 + (bx1 - tx1) * f;\n    const d = Math.abs(x - (l + r) / 2) / ((r - l) / 2);\n    return d >= 1 ? 0 : 1 - d * d;\n  };\n  return {\n    update(dt, t) {\n      const damp = Math.pow(0.35, dt);\n      for (const p of ps) {\n        p.vx = p.vx * damp + (Math.sin(t * 0.21 + p.s) * 3 + (Math.random() - 0.5) * 36) * dt;\n        p.vy = p.vy * damp + (Math.cos(t * 0.17 + p.s) * 3 - 1 + (Math.random() - 0.5) * 36) * dt;\n        const k = (0.35 + p.z) * DRIFT_SPEED * 5;\n        p.x += p.vx * k * dt;\n        p.y += p.vy * k * dt;\n        if (p.x < -40) p.x += w + 80; else if (p.x > w + 40) p.x -= w + 80;\n        if (p.y < -40) p.y += h + 80; else if (p.y > h + 40) p.y -= h + 80;\n      }\n    },\n    draw(c, t) {\n      c.clearRect(0, 0, w, h);\n      c.globalAlpha = 0.85 + 0.15 * Math.sin(t * 0.35);\n      c.drawImage(beam, 0, 0, w, h);\n      c.globalCompositeOperation = \"lighter\";\n      for (const p of ps) {\n        const a = (0.05 + 0.95 * lit(p.x, p.y)) * (0.5 + 0.5 * Math.sin(t * 1.3 + p.s));\n        if (a < 0.02) continue;\n        if (p.z > 0.9) {\n          const s = 16 + (p.z - 0.9) * 340;\n          c.globalAlpha = a * 0.55;\n          c.drawImage(blur, p.x - s / 2, p.y - s / 2, s, s);\n        } else {\n          const s = 2 + p.z * 6;\n          c.globalAlpha = a * (0.35 + p.z * 0.65);\n          c.drawImage(fine, p.x - s / 2, p.y - s / 2, s, s);\n        }\n      }\n      c.globalAlpha = 1;\n      c.globalCompositeOperation = \"source-over\";\n    },\n  };\n}\n\nexport default function DustMotesSunlightShaft() {\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=\"dms-bg relative isolate flex min-h-[560px] items-center overflow-hidden px-6 py-20 sm:px-12\">\n      <style>{`.dms-bg{background:radial-gradient(70% 55% at 62% 108%, #4b311d 0%, rgba(75,49,29,0) 70%), linear-gradient(180deg, #1b130d 0%, #261a11 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-[#d8c6ac]\">Linden & Oak furniture restoration</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.08] tracking-tight text-[#f6ecdc] sm:text-5xl\">Old wood, ready for its second century.</h2>\n          <p className=\"mt-5 text-lg leading-relaxed text-[#d8c6ac]\">Hand-stripped finishes, rebuilt joints and French polish for the pieces your family plans to keep.</p>\n          <a\n            href=\"#\"\n            className=\"mt-8 inline-flex h-12 items-center rounded-full bg-[#f1d8a6] px-6 text-sm font-semibold text-[#24180f] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#f6ecdc] focus-visible:ring-offset-2 focus-visible:ring-offset-[#1f150e]\"\n          >\n            Book a workshop visit\n          </a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
