{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"window-light-drift","type":"registry:component","title":"Window Light Drift","description":"Afternoon sun through a window, crossing a plaster wall over ninety seconds — and your pointer casts a shadow in it.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/window-light-drift.tsx","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\n/**\n * WindowLightDrift — afternoon sun through a window, drifting across a wall.\n *\n * One slanted shaft of light, broken by the shadows of the window bars, crosses\n * the wall and back over a minute and a half, and breathes very slightly as if\n * clouds were passing. It is a background for a café, a salon or a studio: a\n * room, not a graphic. Your pointer is a hand in the light — it casts a soft\n * shadow on the wall, and only where the light falls.\n *\n * Everything is drawn on one canvas. The light is built on an offscreen layer\n * (a gradient-edged shaft, cut by the bars and the pointer shadow with\n * destination-out compositing), laid over the wall, then dusted with a fixed\n * grain tile so the wall reads as plaster rather than a flat fill. The loop is\n * time-based, sleeps off-screen and in hidden tabs, and under\n * prefers-reduced-motion it draws one still frame — the pointer shadow still\n * works there, because the reader is driving it.\n *\n * Place it as the FIRST child of a position: relative section. Pointer events\n * are read from that parent, so copy and buttons above it never block the\n * shadow.\n */\n\nconst HEX = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;\n\nfunction hex(v: string, fallback: string) {\n  return HEX.test(v) ? v : fallback;\n}\n\nfunction rgba(colour: string, alpha: number) {\n  let h = colour.slice(1);\n  if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];\n  const n = parseInt(h, 16);\n  return \"rgba(\" + ((n >> 16) & 255) + \",\" + ((n >> 8) & 255) + \",\" + (n & 255) + \",\" + alpha + \")\";\n}\n\n/** A 160px tile of fine grain, drawn once and repeated. */\nfunction makeGrain() {\n  const tile = document.createElement(\"canvas\");\n  tile.width = 160;\n  tile.height = 160;\n  const c = tile.getContext(\"2d\");\n  if (!c) return null;\n  const img = c.createImageData(160, 160);\n  const d = img.data;\n  for (let i = 0; i < d.length; i += 4) {\n    const v = 90 + Math.floor(Math.random() * 120);\n    d[i] = v;\n    d[i + 1] = v;\n    d[i + 2] = v;\n    d[i + 3] = 255;\n  }\n  c.putImageData(img, 0, 0);\n  return tile;\n}\n\nexport function WindowLightDrift({\n  wall = \"#151412\",\n  light = \"#f4d9a6\",\n  angle = 22,\n  bars = 2,\n  speed = 1,\n  grain = 0.5,\n  shadow = true,\n  className = \"\",\n}: {\n  /** Wall colour. Hex only. */\n  wall?: string;\n  /** Colour of the light. Hex only — warm for afternoon, blue-white for a north window. */\n  light?: string;\n  /** Slant of the shaft in degrees, 0–40. */\n  angle?: number;\n  /** Vertical window bars crossing the shaft, 0–4. */\n  bars?: number;\n  /** Drift speed multiplier. 1 crosses the wall and back in 90 seconds; 0 holds still. */\n  speed?: number;\n  /** Plaster grain, 0–1. */\n  grain?: number;\n  /** The pointer casts a soft shadow where it crosses the light. */\n  shadow?: boolean;\n  className?: string;\n}) {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n\n  // Props are read by the render loop through a ref, so a change adjusts the\n  // next frame instead of tearing the loop down. Synced from an effect.\n  const opts = useRef({ wall, light, angle, bars, speed, grain, shadow });\n  const refresh = useRef<() => void>(() => {});\n  useEffect(() => {\n    opts.current = { wall, light, angle, bars, speed, grain, shadow };\n    refresh.current();\n  }, [wall, light, angle, bars, speed, grain, shadow]);\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    const host = canvas ? canvas.parentElement : null;\n    const ctx = canvas ? canvas.getContext(\"2d\") : null;\n    if (!canvas || !host || !ctx) return;\n    const layer = document.createElement(\"canvas\");\n    const lctx = layer.getContext(\"2d\");\n    if (!lctx) return;\n    const grainTile = makeGrain();\n    const grainPattern = grainTile ? ctx.createPattern(grainTile, \"repeat\") : null;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n\n    let W = 0;\n    let H = 0;\n    let dpr = 1;\n    let raf = 0;\n    let onScreen = true;\n    let hidden = document.visibilityState === \"hidden\";\n    let last = 0;\n    let t = 0; // seconds of drift, only advanced while motion is allowed\n    const pointer = { x: 0, y: 0, tx: 0, ty: 0, a: 0, ta: 0 };\n\n    function layout() {\n      // Layout size, not the bounding rect: the rect includes ancestor\n      // transforms, so inside a scaled card the light would fill only part of\n      // the box, and a ResizeObserver never re-fires to correct it.\n      W = host!.clientWidth;\n      H = host!.clientHeight;\n      dpr = Math.min(2, window.devicePixelRatio || 1);\n      // Keep the backing store under ~8 megapixels on huge screens.\n      while (W * H * dpr * dpr > 8e6 && dpr > 1) dpr = Math.max(1, dpr - 0.25);\n      canvas!.width = layer.width = Math.max(1, Math.round(W * dpr));\n      canvas!.height = layer.height = Math.max(1, Math.round(H * dpr));\n      canvas!.style.width = W + \"px\";\n      canvas!.style.height = H + \"px\";\n    }\n\n    function paintLight() {\n      const o = opts.current;\n      const c = hex(o.light, \"#f4d9a6\");\n      const T = 90 / Math.max(0.05, o.speed);\n      const sway = Math.sin((t / T) * Math.PI * 2);\n      const breathe = 0.84 + 0.16 * Math.sin((t / 41) * Math.PI * 2 + 1);\n      const sw = Math.min(W * 0.46, Math.max(160, H * 0.75));\n      const cx = W * 0.5 + sway * Math.max(0, W * 0.5 - sw * 0.35);\n      const k = Math.tan((Math.max(0, Math.min(40, o.angle)) * Math.PI) / 180);\n\n      lctx!.setTransform(dpr, 0, 0, dpr, 0, 0);\n      lctx!.globalCompositeOperation = \"source-over\";\n      lctx!.clearRect(0, 0, W, H);\n\n      // The shaft: a soft-edged band in a space skewed about the wall's middle.\n      lctx!.transform(1, 0, -k, 1, k * H * 0.5, 0);\n      const g = lctx!.createLinearGradient(cx - sw / 2, 0, cx + sw / 2, 0);\n      g.addColorStop(0, rgba(c, 0));\n      g.addColorStop(0.16, rgba(c, breathe));\n      g.addColorStop(0.84, rgba(c, breathe));\n      g.addColorStop(1, rgba(c, 0));\n      lctx!.fillStyle = g;\n      lctx!.fillRect(cx - sw / 2, -H * 0.2, sw, H * 1.4);\n\n      // Fade at the top and the floor, so the shaft does not read as a stripe.\n      lctx!.setTransform(dpr, 0, 0, dpr, 0, 0);\n      lctx!.globalCompositeOperation = \"destination-in\";\n      const v = lctx!.createLinearGradient(0, 0, 0, H);\n      v.addColorStop(0, \"rgba(0,0,0,0.5)\");\n      v.addColorStop(0.35, \"rgba(0,0,0,1)\");\n      v.addColorStop(0.78, \"rgba(0,0,0,1)\");\n      v.addColorStop(1, \"rgba(0,0,0,0.25)\");\n      lctx!.fillStyle = v;\n      lctx!.fillRect(0, 0, W, H);\n\n      // The window bars: cut out of the light, in the skewed space.\n      lctx!.globalCompositeOperation = \"destination-out\";\n      lctx!.transform(1, 0, -k, 1, k * H * 0.5, 0);\n      const n = Math.max(0, Math.min(4, Math.round(o.bars)));\n      const bw = sw * 0.045;\n      for (let i = 1; i <= n; i++) {\n        const bx = cx - sw / 2 + (sw * i) / (n + 1);\n        const bg = lctx!.createLinearGradient(bx - bw, 0, bx + bw, 0);\n        bg.addColorStop(0, \"rgba(0,0,0,0)\");\n        bg.addColorStop(0.38, \"rgba(0,0,0,0.9)\");\n        bg.addColorStop(0.62, \"rgba(0,0,0,0.9)\");\n        bg.addColorStop(1, \"rgba(0,0,0,0)\");\n        lctx!.fillStyle = bg;\n        lctx!.fillRect(bx - bw, -H * 0.2, bw * 2, H * 1.4);\n      }\n\n      // The transom: one horizontal bar, level on the wall.\n      lctx!.setTransform(dpr, 0, 0, dpr, 0, 0);\n      const ty = H * 0.44;\n      const th = Math.max(5, H * 0.012);\n      const tg = lctx!.createLinearGradient(0, ty - th, 0, ty + th);\n      tg.addColorStop(0, \"rgba(0,0,0,0)\");\n      tg.addColorStop(0.4, \"rgba(0,0,0,0.88)\");\n      tg.addColorStop(0.6, \"rgba(0,0,0,0.88)\");\n      tg.addColorStop(1, \"rgba(0,0,0,0)\");\n      lctx!.fillStyle = tg;\n      lctx!.fillRect(0, ty - th, W, th * 2);\n\n      // The hand: a soft shadow that only exists where there is light to block.\n      if (pointer.a > 0.002) {\n        const r = Math.max(60, Math.min(W, H) * 0.16);\n        const sg = lctx!.createRadialGradient(pointer.x, pointer.y, 0, pointer.x, pointer.y, r);\n        sg.addColorStop(0, \"rgba(0,0,0,\" + 0.62 * pointer.a + \")\");\n        sg.addColorStop(0.55, \"rgba(0,0,0,\" + 0.3 * pointer.a + \")\");\n        sg.addColorStop(1, \"rgba(0,0,0,0)\");\n        lctx!.fillStyle = sg;\n        lctx!.fillRect(pointer.x - r, pointer.y - r, r * 2, r * 2);\n      }\n      lctx!.globalCompositeOperation = \"source-over\";\n    }\n\n    function draw() {\n      if (!W || !H) return;\n      const o = opts.current;\n      ctx!.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx!.globalCompositeOperation = \"source-over\";\n      ctx!.globalAlpha = 1;\n      ctx!.fillStyle = hex(o.wall, \"#151412\");\n      ctx!.fillRect(0, 0, W, H);\n      paintLight();\n      ctx!.drawImage(layer, 0, 0, W, H);\n      if (grainPattern && o.grain > 0) {\n        // Device pixels, so the grain stays fine on a retina screen.\n        ctx!.setTransform(1, 0, 0, 1, 0, 0);\n        ctx!.globalCompositeOperation = \"overlay\";\n        ctx!.globalAlpha = Math.min(1, o.grain) * 0.34;\n        ctx!.fillStyle = grainPattern;\n        ctx!.fillRect(0, 0, canvas!.width, canvas!.height);\n        ctx!.globalAlpha = 1;\n        ctx!.globalCompositeOperation = \"source-over\";\n      }\n    }\n\n    function frame(now: number) {\n      raf = 0;\n      // Time-based and clamped, so a long pause never jumps the light.\n      const dt = last ? Math.min(0.1, (now - last) / 1000) : 1 / 60;\n      last = now;\n      if (!reduce.matches) t += dt;\n      const kp = 1 - Math.exp(-dt / 0.22);\n      pointer.x += (pointer.tx - pointer.x) * kp;\n      pointer.y += (pointer.ty - pointer.y) * kp;\n      pointer.a += (pointer.ta - pointer.a) * kp;\n      draw();\n      const settling =\n        Math.abs(pointer.a - pointer.ta) > 0.003 ||\n        (pointer.ta > 0 && (Math.abs(pointer.tx - pointer.x) > 0.4 || Math.abs(pointer.ty - pointer.y) > 0.4));\n      const ambient = !reduce.matches && opts.current.speed > 0;\n      if (onScreen && !hidden && (ambient || settling)) raf = requestAnimationFrame(frame);\n      else last = 0;\n    }\n    function start() {\n      if (!raf) raf = requestAnimationFrame(frame);\n    }\n    refresh.current = () => {\n      draw();\n      start();\n    };\n\n    function toLocal(e: PointerEvent) {\n      const rect = host!.getBoundingClientRect();\n      // Back out any ancestor scale: rect is projected, W/H are layout.\n      return {\n        x: ((e.clientX - rect.left) * W) / Math.max(1, rect.width),\n        y: ((e.clientY - rect.top) * H) / Math.max(1, rect.height),\n      };\n    }\n    function onMove(e: PointerEvent) {\n      if (!opts.current.shadow) return;\n      const p = toLocal(e);\n      if (pointer.ta === 0) {\n        // First entry: appear in place rather than sweeping in from the corner.\n        pointer.x = p.x;\n        pointer.y = p.y;\n      }\n      pointer.tx = p.x;\n      pointer.ty = p.y;\n      pointer.ta = 1;\n      start();\n    }\n    function onLeave() {\n      pointer.ta = 0;\n      start();\n    }\n    function onVisibility() {\n      hidden = document.visibilityState === \"hidden\";\n      if (!hidden) start();\n    }\n    function onReduce() {\n      start();\n    }\n\n    layout();\n    draw();\n    const ro = new ResizeObserver(() => {\n      layout();\n      draw();\n    });\n    ro.observe(host);\n    const io = new IntersectionObserver((entries) => {\n      onScreen = entries[entries.length - 1].isIntersecting;\n      if (onScreen) start();\n    });\n    io.observe(host);\n    host.addEventListener(\"pointermove\", onMove);\n    host.addEventListener(\"pointerleave\", onLeave);\n    document.addEventListener(\"visibilitychange\", onVisibility);\n    reduce.addEventListener(\"change\", onReduce);\n    start();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      host.removeEventListener(\"pointermove\", onMove);\n      host.removeEventListener(\"pointerleave\", onLeave);\n      document.removeEventListener(\"visibilitychange\", onVisibility);\n      reduce.removeEventListener(\"change\", onReduce);\n      refresh.current = () => {};\n    };\n  }, []);\n\n  return (\n    <canvas\n      ref={canvasRef}\n      aria-hidden=\"true\"\n      className={\"pointer-events-none absolute inset-0 block \" + className}\n    />\n  );\n}\n","type":"registry:component","target":"components/viberdy/window-light-drift.tsx"}],"meta":{"tier":"free","tags":["background","canvas","light","ambient","cursor"],"href":"https://viberdy.dev/library/window-light-drift","aiPrompt":"Create a React + Tailwind component named WindowLightDrift: a full-bleed canvas background of afternoon sunlight through a window, drifting slowly across a plaster wall, with the pointer casting a soft shadow in the light.\n\nLook:\n- The wall is a flat colour (`wall`, hex, default #151412) dusted with fine grain: build a 160x160 tile of random grey pixels once, and fill the whole canvas with it as a repeating pattern using \"overlay\" compositing at alpha = grain * 0.34, in DEVICE pixels so it stays fine on retina.\n- The light is ONE slanted shaft: a band about 46% of the width wide (at least 160px, at most 75% of the height), drawn in a coordinate space skewed by `angle` degrees (0–40, default 22) about the wall's vertical middle. Its horizontal edges are gradient ramps (transparent → light over the first and last 16%), and a vertical destination-in gradient fades the top (50%) and the floor (25%) so it never reads as a stripe.\n- Window bars: `bars` (0–4, default 2) vertical shadows cut out of the shaft with destination-out gradient bars about 4.5% of the shaft width each, plus one level horizontal transom at 44% of the height.\n- The shaft drifts: centre x = W/2 + sin(2π · t / T) · (W/2 − 0.35 · shaftWidth), where T = 90 s / speed, so one crossing and back takes a minute and a half at speed 1. Its brightness breathes between 0.68 and 1.0 on a 41-second sine, like passing cloud.\n- Colours are hex only (validate with a regex, fall back); `light` defaults to #f4d9a6.\n\nInteraction:\n- The pointer is a hand in the light: a radial shadow (radius = 16% of the smaller dimension, at least 60px; 0.62 alpha at the centre, 0.3 at 55%, 0 at the edge) cut OUT of the light layer with destination-out, so it only exists where light falls. Ease its position and strength with time-based exponential smoothing (tau 0.22 s); on first entry appear in place rather than sweep in; fade out on pointerleave. `shadow` (boolean) turns it off.\n- Listen for pointer events on the canvas's PARENT element and convert clientX/Y into layout pixels (offset from the bounding rect scaled by clientWidth / rect.width), so it works inside scaled containers and copy above it never blocks the shadow. The canvas is pointer-events: none and aria-hidden.\n\nImplementation requirements:\n- One <canvas> absolutely positioned at inset 0 of its parent; measure the parent with clientWidth/clientHeight (layout size, not getBoundingClientRect); size the backing store by devicePixelRatio capped at 2 and lowered until it stays under ~8 megapixels; re-layout with a ResizeObserver.\n- Build the light on an OFFSCREEN canvas of the same size (shaft, vertical fade, bars, transom, pointer shadow), then draw the wall, the light layer, and the grain onto the visible canvas.\n- Hold props in a ref synced from an effect (no ref writes during render); the sync effect redraws a still frame and restarts the loop.\n- requestAnimationFrame loop with a clamped time delta (≤ 100 ms); run it only while the parent is on screen (IntersectionObserver, reading entries[entries.length - 1]), the tab is visible, and either motion is allowed with speed > 0 or the pointer shadow is still settling.\n- prefers-reduced-motion: the drift and breathing stop (t no longer advances) and a single still frame is drawn; the pointer shadow keeps working because the reader drives it. Listen for the media query changing.\n- Clean up the frame, observers and listeners on unmount.\n\nProps: wall (hex), light (hex), angle (number), bars (number), speed (number), grain (0–1), shadow (boolean), className.\n\nOutput one self-contained \"use client\" component with no dependencies beyond React.","license":"https://viberdy.dev/license"}}