{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"rain-on-a-night-window-particle-background-with-city-lights","type":"registry:block","title":"Rain on a Night Window","description":"","dependencies":[],"files":[{"path":"components/we-inc/rain-on-a-night-window-particle-background-with-city-lights.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// Rain on a Night Window: 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 STREAKS_PER_MPX = 240; // falling rain outside, per million pixels\nconst BEADS_PER_MPX = 300; // drops sitting on the glass\nconst CITY = [\"255,178,92\", \"255,214,150\", \"120,186,214\", \"226,96,82\"]; // blurred lights behind the glass\nconst SLANT = 0.14; // rain angle: 0 is straight down\nconst WARMUP = 20; // 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  // City lights behind the glass, drawn once.\n  const city = document.createElement(\"canvas\");\n  city.width = Math.ceil(w / 2);\n  city.height = Math.ceil(h / 2);\n  const g = city.getContext(\"2d\")!;\n  g.scale(0.5, 0.5);\n  const blobs = CITY.map((rgb) => glow(128, rgb, [[0, 0.5], [0.35, 0.22], [1, 0]]));\n  for (let i = 0; i < 40 * Math.max(1, area * 1.5); i++) {\n    const s = 30 + Math.random() * 120;\n    g.globalAlpha = 0.25 + Math.random() * 0.6;\n    g.drawImage(blobs[Math.floor(Math.random() * blobs.length)], Math.random() * w - s / 2, h * (0.35 + Math.random() * 0.7) - s / 2, s, s);\n  }\n  // A water bead: dark lens with a bright lower rim and a small highlight.\n  const bead = document.createElement(\"canvas\");\n  bead.width = bead.height = 32;\n  const q = bead.getContext(\"2d\")!;\n  q.fillStyle = \"rgba(10,16,24,0.35)\";\n  q.beginPath(); q.arc(16, 16, 13, 0, 7); q.fill();\n  q.strokeStyle = \"rgba(255,226,180,0.55)\";\n  q.lineWidth = 2;\n  q.beginPath(); q.arc(16, 16, 12, 0.3, 2.8); q.stroke();\n  q.fillStyle = \"rgba(255,255,255,0.8)\";\n  q.beginPath(); q.arc(11, 10, 2.2, 0, 7); q.fill();\n  const streaks = Array.from({ length: Math.min(500, Math.round(area * STREAKS_PER_MPX)) }, () => ({\n    x: Math.random() * w, y: Math.random() * h, z: Math.random(),\n  }));\n  const beads = Array.from({ length: Math.min(600, Math.round(area * BEADS_PER_MPX)) }, () => ({\n    x: Math.random() * w, y: Math.random() * h, r: 1 + Math.random() ** 3 * 5, vy: 0, top: 0,\n  }));\n  return {\n    update(dt) {\n      for (const s of streaks) {\n        const v = (700 + s.z * 600) * dt;\n        s.y += v;\n        s.x -= v * SLANT;\n        if (s.y > h + 40) { s.y = -40 - Math.random() * 80; s.x = Math.random() * (w + h * SLANT); }\n      }\n      for (const b of beads) {\n        if (b.vy === 0 && b.r > 3.5 && Math.random() < 0.06 * dt) { b.vy = 30; b.top = b.y; }\n        if (b.vy > 0) {\n          b.vy = Math.min(260, b.vy + 220 * dt * (Math.random() < 0.2 ? 0 : 1));\n          b.y += b.vy * dt;\n          b.x += (Math.random() - 0.5) * 30 * dt;\n          if (b.y > h + 20) { b.y = Math.random() * h; b.x = Math.random() * w; b.vy = 0; b.r = 1 + Math.random() ** 3 * 5; }\n        }\n      }\n    },\n    draw(c) {\n      c.clearRect(0, 0, w, h);\n      c.globalAlpha = 1;\n      c.drawImage(city, 0, 0, w, h);\n      c.strokeStyle = \"rgb(190,210,228)\";\n      c.lineCap = \"round\";\n      for (const s of streaks) {\n        const len = 14 + s.z * 30;\n        c.globalAlpha = 0.06 + s.z * 0.2;\n        c.lineWidth = 0.6 + s.z * 0.8;\n        c.beginPath();\n        c.moveTo(s.x, s.y);\n        c.lineTo(s.x + len * SLANT, s.y - len);\n        c.stroke();\n      }\n      for (const b of beads) {\n        if (b.vy > 0) {\n          const tg = c.createLinearGradient(0, b.top, 0, b.y);\n          tg.addColorStop(0, \"rgba(200,220,236,0)\");\n          tg.addColorStop(1, \"rgba(200,220,236,0.28)\");\n          c.globalAlpha = 1;\n          c.strokeStyle = tg;\n          c.lineWidth = b.r * 0.9;\n          c.beginPath();\n          c.moveTo(b.x, b.top);\n          c.lineTo(b.x, b.y);\n          c.stroke();\n        }\n        const s = b.r * 2.4;\n        c.globalAlpha = 0.9;\n        c.drawImage(bead, b.x - s / 2, b.y - s / 2, s, s);\n      }\n      c.globalAlpha = 1;\n    },\n  };\n}\n\nexport default function RainNightWindow() {\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=\"rwn-bg relative isolate flex min-h-[560px] items-center overflow-hidden px-6 py-20 sm:px-12\">\n      <style>{`.rwn-bg{background:linear-gradient(180deg, #0a0f15 0%, #111925 60%, #18212c 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-[#e9bf80]\">Halden Street Records</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.08] tracking-tight text-[#eef2f6] sm:text-5xl\">A listening room for rainy nights.</h2>\n          <p className=\"mt-5 text-lg leading-relaxed text-[#bcc6d1]\">Vinyl on a proper system, a short cocktail list and seats facing the window. Open from six until the last record ends.</p>\n          <a\n            href=\"#\"\n            className=\"mt-8 inline-flex h-12 items-center rounded-full bg-[#e9b872] px-6 text-sm font-semibold text-[#1a1208] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#eef2f6] focus-visible:ring-offset-2 focus-visible:ring-offset-[#0f151d]\"\n          >\n            See this week's sets\n          </a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
