{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"rain-on-a-window-at-night-background-with-drops-that-refract-street-lights","type":"registry:block","title":"Rain on a Night Window","description":"","dependencies":[],"files":[{"path":"components/we-inc/rain-on-a-window-at-night-background-with-drops-that-refract-street-lights.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// The values worth changing.\nconst SETTINGS = {\n  night: [\"#0a0f1c\", \"#141b2d\"], // the dark street behind the glass, top to bottom\n  lights: [\"255,176,92\", \"255,214,150\", \"120,200,210\", \"232,96,80\", \"190,170,255\"], // blurred street lights as \"red,green,blue\"\n  runners: 60, // big drops on the pane; most cling, a few run at a time\n  beads: 520, // small drops stuck to the glass\n  rain: 1, // how often big drops break loose and run\n};\n\nfunction streetSprite(w: any, h: any, k: any) {\n  // The view through the glass: out-of-focus lamps, shop signs and car lights on a dark street.\n  const c = document.createElement(\"canvas\");\n  c.width = Math.max(1, Math.round(w * k));\n  c.height = Math.max(1, Math.round(h * k));\n  const g = c.getContext(\"2d\");\n  g.scale(k, k);\n  const bg = g.createLinearGradient(0, 0, 0, h);\n  bg.addColorStop(0, SETTINGS.night[0]);\n  bg.addColorStop(1, SETTINGS.night[1]);\n  g.fillStyle = bg;\n  g.fillRect(0, 0, w, h);\n  let s = 7;\n  const rnd = () => {\n    s = (s * 16807) % 2147483647;\n    return s / 2147483647;\n  };\n  for (let i = 0; i < 46; i++) {\n    const col = SETTINGS.lights[Math.floor(rnd() * SETTINGS.lights.length)];\n    const x = (0.25 + rnd() * 0.8) * w;\n    const y = (0.3 + rnd() * 0.55) * h;\n    const r = 18 + rnd() * 46;\n    const a = 0.1 + rnd() * 0.22;\n    const grad = g.createRadialGradient(x, y, 0, x, y, r);\n    grad.addColorStop(0, `rgba(${col},${a})`);\n    grad.addColorStop(0.75, `rgba(${col},${a * 0.8})`);\n    grad.addColorStop(1, `rgba(${col},0)`);\n    g.fillStyle = grad;\n    g.fillRect(x - r, y - r, r * 2, r * 2);\n  }\n  return c;\n}\n\nconst SCENE: any = {\n  start: 0,\n  still: 0,\n  scale: 0.85,\n  init(ctx: any, w: any, h: any, k: any) {\n    const rand = (a: any, b: any) => a + Math.random() * (b - a);\n    const blur = streetSprite(w, h, k);\n    // Beads: small drops painted once onto their own layer.\n    const beads = document.createElement(\"canvas\");\n    beads.width = blur.width;\n    beads.height = blur.height;\n    const b = beads.getContext(\"2d\");\n    b.scale(k, k);\n    for (let i = 0; i < SETTINGS.beads; i++) {\n      const x = rand(0, w);\n      const y = rand(0, h);\n      const r = rand(0.8, 2.8);\n      b.fillStyle = \"rgba(0,0,0,0.22)\";\n      b.beginPath();\n      b.arc(x, y - r * 0.2, r, 0, 6.29);\n      b.fill();\n      b.fillStyle = \"rgba(255,226,190,0.22)\";\n      b.beginPath();\n      b.arc(x, y + r * 0.25, r * 0.8, 0, 6.29);\n      b.fill();\n      b.fillStyle = \"rgba(255,240,220,0.45)\";\n      b.beginPath();\n      b.arc(x - r * 0.3, y - r * 0.3, r * 0.45, 0, 6.29);\n      b.fill();\n    }\n    const runners: any[] = [];\n    for (let i = 0; i < SETTINGS.runners; i++) runners.push({ x: rand(0, w), y: rand(0, h), r: rand(3, 9), v: 0, wait: i < 8 ? rand(0, 1.5) : rand(3, 24) });\n    return { blur, beads, runners, trail: [], k, rand };\n  },\n  draw(ctx: any, w: any, h: any, t: any, st: any, ptr: any, dt: any) {\n    ctx.drawImage(st.blur, 0, 0, w, h);\n    ctx.drawImage(st.beads, 0, 0, w, h);\n    // Trails of tiny drops left behind by the runners, slowly evaporating.\n    for (const d of st.trail) {\n      d.life -= dt * 0.12;\n      ctx.fillStyle = `rgba(255,232,200,${0.35 * Math.max(0, d.life)})`;\n      ctx.fillRect(d.x, d.y, d.r, d.r);\n    }\n    st.trail = st.trail.filter((d: any) => d.life > 0);\n    const bw = st.blur.width / w;\n    for (const d of st.runners) {\n      // Drops cling, then break loose and run in bursts, wandering a little sideways.\n      if (dt > 0) {\n        if (d.wait > 0) d.wait -= dt * SETTINGS.rain;\n        else {\n          d.v = Math.min(150, d.v + dt * 260 * (0.5 + Math.random()));\n          if (Math.random() < dt * 1.2) d.v *= 0.2;\n          d.y += d.v * dt * (d.r / 6);\n          d.x += (Math.random() - 0.5) * 0.8;\n          if (Math.random() < dt * 20) st.trail.push({ x: d.x + (Math.random() - 0.5) * d.r, y: d.y - d.r * 1.5, r: 0.8 + Math.random() * 1.4, life: 1 });\n          if (d.y - d.r * 2 > h) {\n            // A new drop gathers somewhere else on the pane and clings for a while.\n            d.y = st.rand(0, h * 0.7);\n            d.x = st.rand(0, w);\n            d.r = st.rand(3, 9);\n            d.v = 0;\n            d.wait = st.rand(2, 14);\n          }\n        }\n      }\n      // Each drop is a tiny lens: it gathers a wide patch of the street, upside down and brighter.\n      const rx = d.r;\n      const ry = d.r * 1.12;\n      ctx.save();\n      ctx.beginPath();\n      ctx.ellipse(d.x, d.y, rx, ry, 0, 0, 6.29);\n      ctx.clip();\n      const view = d.r * 14;\n      ctx.translate(d.x, d.y);\n      ctx.scale(-1, -1);\n      ctx.drawImage(st.blur, (d.x - view) * bw, (d.y - view * 0.6) * bw, view * 2 * bw, view * 1.2 * bw, -rx, -ry, rx * 2, ry * 2);\n      ctx.globalCompositeOperation = \"lighter\";\n      ctx.globalAlpha = 0.6;\n      ctx.drawImage(st.blur, (d.x - view) * bw, (d.y - view * 0.6) * bw, view * 2 * bw, view * 1.2 * bw, -rx, -ry, rx * 2, ry * 2);\n      ctx.globalAlpha = 1;\n      ctx.globalCompositeOperation = \"source-over\";\n      ctx.restore();\n      // Light pools along the lower rim and a dark edge sits along the top, as in a real bead of water.\n      const rim = ctx.createLinearGradient(0, d.y - ry, 0, d.y + ry);\n      rim.addColorStop(0, \"rgba(0,0,0,0.5)\");\n      rim.addColorStop(0.45, \"rgba(0,0,0,0)\");\n      rim.addColorStop(0.8, \"rgba(255,236,210,0.08)\");\n      rim.addColorStop(1, \"rgba(255,236,210,0.45)\");\n      ctx.strokeStyle = rim;\n      ctx.lineWidth = Math.max(0.8, d.r * 0.22);\n      ctx.beginPath();\n      ctx.ellipse(d.x, d.y, rx * 0.92, ry * 0.92, 0, 0, 6.29);\n      ctx.stroke();\n      ctx.fillStyle = \"rgba(255,248,236,0.85)\";\n      ctx.beginPath();\n      ctx.ellipse(d.x - rx * 0.32, d.y - ry * 0.45, Math.max(0.6, rx * 0.2), Math.max(0.4, ry * 0.12), -0.5, 0, 6.29);\n      ctx.fill();\n    }\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 RainOnWindowNightBackground() {\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-[#0a0f1c] text-[#f5f1ea]\">\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,12,22,0.82) 0%, rgba(8,12,22,0.5) 45%, rgba(8,12,22,0) 72%)\" }} />\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-[#ffcf8a]\">The Lamplight, Harbour Street</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.05] tracking-tight sm:text-5xl lg:text-6xl\">Rainy night? Your window seat is waiting.</h2>\n          <p className=\"mt-5 text-base leading-relaxed text-[#cdd2dc] sm:text-lg\">Small plates, a long list of bitter aperitifs and a pianist from nine. Walk in, no booking for two.</p>\n          <a href=\"#\" className=\"mt-8 inline-flex h-12 items-center rounded-full bg-[#ffcf8a] px-6 text-sm font-semibold text-[#1a140c] transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#ffcf8a] focus-visible:ring-offset-2 focus-visible:ring-offset-[#0a0f1c]\">See the menu</a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
