{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"light-beams-through-fog-background-with-dust-glinting-in-the-shafts","type":"registry:block","title":"Light Beams Through Fog","description":"","dependencies":[],"files":[{"path":"components/we-inc/light-beams-through-fog-background-with-dust-glinting-in-the-shafts.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// The values worth changing.\nconst SETTINGS = {\n  beams: 6, // shafts of light from the high windows\n  light: [255, 236, 196], // colour of the light as [red, green, blue]\n  fog: [120, 160, 150], // colour of the fog banks\n  tilt: 0.32, // lean of the shafts in radians; 0 falls straight down\n  motes: 150, // specks of dust that glint inside the light\n};\n\nfunction beamSprite() {\n  // One soft shaft, narrow at the window and wider at the floor, fading as it falls.\n  const sw = 96;\n  const sh = 256;\n  const c = document.createElement(\"canvas\");\n  c.width = sw;\n  c.height = sh;\n  const g = c.getContext(\"2d\");\n  const img = g.createImageData(sw, sh);\n  const [r, gr, b] = SETTINGS.light;\n  for (let y = 0; y < sh; y++) {\n    const v = y / sh;\n    const width = 0.16 + v * 0.3;\n    const fall = Math.pow(1 - v, 1.4) * Math.min(1, v * 12);\n    for (let x = 0; x < sw; x++) {\n      const u = (x / sw - 0.5) / width;\n      const a = Math.exp(-u * u * 3) * fall;\n      const o = (y * sw + x) * 4;\n      img.data[o] = r;\n      img.data[o + 1] = gr;\n      img.data[o + 2] = b;\n      img.data[o + 3] = a * 255;\n    }\n  }\n  g.putImageData(img, 0, 0);\n  return c;\n}\n\nfunction fogSprite() {\n  const c = document.createElement(\"canvas\");\n  c.width = c.height = 128;\n  const g = c.getContext(\"2d\");\n  const [r, gr, b] = SETTINGS.fog;\n  const grad = g.createRadialGradient(64, 64, 0, 64, 64, 64);\n  grad.addColorStop(0, `rgba(${r},${gr},${b},0.55)`);\n  grad.addColorStop(0.5, `rgba(${r},${gr},${b},0.22)`);\n  grad.addColorStop(1, `rgba(${r},${gr},${b},0)`);\n  g.fillStyle = grad;\n  g.fillRect(0, 0, 128, 128);\n  return c;\n}\n\nconst SCENE: any = {\n  start: 6,\n  still: 6,\n  scale: 0.7,\n  init(ctx: any, w: any, h: any) {\n    const rand = (a: any, b: any) => a + Math.random() * (b - a);\n    const beams: any[] = [];\n    for (let i = 0; i < SETTINGS.beams; i++) {\n      beams.push({ x: (0.3 + (i / SETTINGS.beams) * 0.85) * w + rand(-30, 30), width: rand(120, 230), strength: rand(0.35, 0.75), phase: rand(0, 6.28), rate: rand(0.15, 0.35) });\n    }\n    const fogs: any[] = [];\n    for (let i = 0; i < 16; i++) fogs.push({ x: rand(-0.2, 1.2) * w, y: rand(0.25, 1.05) * h, s: rand(0.35, 0.8) * Math.max(w, h), v: rand(4, 14), a: rand(0.25, 0.6) });\n    const motes: any[] = [];\n    for (let i = 0; i < SETTINGS.motes; i++) motes.push({ x: rand(0, w), y: rand(0, h), v: rand(2, 9), p: rand(0, 6.28), s: rand(0.6, 1.6) });\n    return { beam: beamSprite(), fog: fogSprite(), beams, fogs, motes, last: 0 };\n  },\n  draw(ctx: any, w: any, h: any, t: any, st: any, ptr: any, dt: any) {\n    ctx.globalCompositeOperation = \"source-over\";\n    const bg = ctx.createLinearGradient(0, 0, 0, h);\n    bg.addColorStop(0, \"#0d1b19\");\n    bg.addColorStop(1, \"#081110\");\n    ctx.fillStyle = bg;\n    ctx.fillRect(0, 0, w, h);\n    // Fog banks drift slowly sideways and wrap around.\n    for (const f of st.fogs) {\n      const x = ((f.x + t * f.v + f.s) % (w + f.s * 2)) - f.s;\n      ctx.globalAlpha = f.a;\n      ctx.drawImage(st.fog, x - f.s / 2, f.y - f.s * 0.25, f.s, f.s * 0.5);\n    }\n    // Shafts of light, added on top, breathing as unseen clouds pass the windows.\n    ctx.globalCompositeOperation = \"lighter\";\n    const len = h * 1.5;\n    for (const b of st.beams) {\n      const breathe = 0.65 + 0.35 * Math.sin(t * b.rate + b.phase) * Math.sin(t * b.rate * 0.37 + b.phase * 2);\n      ctx.globalAlpha = b.strength * breathe;\n      ctx.save();\n      ctx.translate(b.x + Math.sin(t * 0.05 + b.phase) * 12, -h * 0.08);\n      ctx.rotate(SETTINGS.tilt);\n      ctx.drawImage(st.beam, -b.width, 0, b.width * 2, len);\n      ctx.restore();\n    }\n    // Dust: bright only where it floats inside a shaft.\n    const [r, g, bl] = SETTINGS.light;\n    const sin = Math.sin(SETTINGS.tilt);\n    const cos = Math.cos(SETTINGS.tilt);\n    for (const m of st.motes) {\n      const y = (m.y + t * m.v) % h;\n      const x = m.x + Math.sin(t * 0.4 + m.p) * 10;\n      let lit = 0;\n      for (const b of st.beams) {\n        const ox = x - b.x;\n        const oy = y + h * 0.08;\n        const across = ox * cos - oy * sin;\n        const along = ox * sin + oy * cos;\n        const half = b.width * (0.16 + (along / len) * 0.3);\n        lit = Math.max(lit, Math.exp(-(across * across) / (half * half * 0.5)) * b.strength);\n      }\n      if (lit < 0.05) continue;\n      ctx.globalAlpha = Math.min(1, lit * (0.6 + 0.4 * Math.sin(t * 2 + m.p)));\n      ctx.fillStyle = `rgb(${r},${g},${bl})`;\n      ctx.fillRect(x, y, m.s, m.s);\n    }\n    ctx.globalAlpha = 1;\n    ctx.globalCompositeOperation = \"source-over\";\n  },\n};\n\n// Runs a scene on a canvas: sizes it to the block, pauses off screen and in hidden tabs, cleans up after itself.\nfunction runScene(root: any, canvas: any, scene: any, reduced: boolean) {\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) return () => {};\n  let w = 0;\n  let h = 0;\n  let st: any = null;\n  let raf = 0;\n  let last = 0;\n  let acc = 0;\n  let onScreen = true;\n  let time = reduced ? (scene.still ?? scene.start ?? 0) : (scene.start ?? 0);\n  const ptr: any = { x: 0, y: 0, on: false, down: 0 };\n  const step = 1 / (scene.fps || 60);\n  const resize = () => {\n    const r = root.getBoundingClientRect();\n    const nw = Math.max(1, Math.round(r.width));\n    const nh = Math.max(1, Math.round(r.height));\n    if (nw === w && nh === h && st) return;\n    w = nw;\n    h = nh;\n    // Device pixels are capped at 1.5 and scaled by the scene's own quality factor.\n    const d = Math.min(window.devicePixelRatio || 1, 1.5) * (scene.scale || 1);\n    canvas.width = Math.max(1, Math.round(w * d));\n    canvas.height = Math.max(1, Math.round(h * d));\n    ctx.setTransform(canvas.width / w, 0, 0, canvas.height / h, 0, 0);\n    st = scene.init(ctx, w, h, canvas.width / w);\n    scene.draw(ctx, w, h, time, st, ptr, 0);\n  };\n  const tick = (now: number) => {\n    raf = requestAnimationFrame(tick);\n    const dt = last ? Math.min(0.1, (now - last) / 1000) : step;\n    last = now;\n    acc += dt;\n    if (acc < step * 0.95) return;\n    const used = Math.min(acc, 0.1);\n    acc = 0;\n    time += used;\n    scene.draw(ctx, w, h, time, st, ptr, used);\n  };\n  const play = () => {\n    if (raf || reduced || !onScreen || document.hidden) return;\n    last = 0;\n    raf = requestAnimationFrame(tick);\n  };\n  const stop = () => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n  };\n  const ro = new ResizeObserver(resize);\n  ro.observe(root);\n  const io = new IntersectionObserver((entries: any) => {\n    onScreen = entries[0].isIntersecting;\n    if (onScreen) play();\n    else stop();\n  });\n  io.observe(root);\n  const onVis = () => (document.hidden ? stop() : play());\n  const onMove = (e: any) => {\n    const r = root.getBoundingClientRect();\n    ptr.x = e.clientX - r.left;\n    ptr.y = e.clientY - r.top;\n    ptr.on = true;\n  };\n  const onLeave = () => {\n    ptr.on = false;\n  };\n  const onDown = (e: any) => {\n    onMove(e);\n    ptr.down += 1;\n  };\n  document.addEventListener(\"visibilitychange\", onVis);\n  root.addEventListener(\"pointermove\", onMove);\n  root.addEventListener(\"pointerleave\", onLeave);\n  root.addEventListener(\"pointerdown\", onDown);\n  resize();\n  play();\n  return () => {\n    stop();\n    ro.disconnect();\n    io.disconnect();\n    document.removeEventListener(\"visibilitychange\", onVis);\n    root.removeEventListener(\"pointermove\", onMove);\n    root.removeEventListener(\"pointerleave\", onLeave);\n    root.removeEventListener(\"pointerdown\", onDown);\n  };\n}\n\nexport default function LightBeamsThroughFogBackground() {\n  const rootRef = useRef<HTMLElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    if (!root || !canvas) return;\n    // Visitors who prefer reduced motion get one settled frame and no loop.\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    return runScene(root, canvas, SCENE, reduced);\n  }, []);\n\n  return (\n    <section ref={rootRef} className=\"relative h-[640px] w-full overflow-hidden bg-[#0b1715] text-[#f4efe4]\">\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 block h-full w-full\" />\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ background: \"linear-gradient(90deg, rgba(8,17,16,0.85) 0%, rgba(8,17,16,0.55) 40%, rgba(8,17,16,0) 65%)\" }} />\n      <div className=\"relative z-10 mx-auto flex h-full max-w-6xl flex-col justify-center items-start text-left px-6 py-14 sm:px-10\">\n        <div className=\"max-w-xl\">\n          <p className=\"text-xs font-semibold uppercase tracking-[0.2em] text-[#f1dfb8]\">Saint Brannoc’s, autumn season</p>\n          <h2 className=\"font-serif mt-4 text-4xl font-semibold leading-[1.05] tracking-tight sm:text-5xl lg:text-6xl\">Evensong in the old nave.</h2>\n          <p className=\"mt-5 text-base leading-relaxed text-[#cbd3cc] sm:text-lg\">Choral evenings under the high windows, Thursdays at seven. Doors open half an hour before.</p>\n          <a href=\"#\" className=\"mt-8 inline-flex h-12 items-center rounded-full bg-[#f1dfb8] px-6 text-sm font-semibold text-[#1b1a14] transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#f1dfb8] focus-visible:ring-offset-2 focus-visible:ring-offset-[#0b1715]\">Reserve a seat</a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
