{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"beams-upstream","type":"registry:ui","title":"Beams Upstream","description":"Thin light beams rise along the vertical lines of a softly masked grid, trailing a glowing tail. Canvas 2D.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/beams-upstream.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface BeamsUpstreamProps {\n  /** Beam color. */\n  color?: string;\n  /** Grid line color. */\n  gridColor?: string;\n  /** Grid cell size in px. */\n  gap?: number;\n  /** Rise speed multiplier; 0 freezes the beams. */\n  speed?: number;\n  /** Average number of beams on screen per 10 grid columns. */\n  density?: number;\n  /** Beam length as a fraction of the container height. */\n  length?: number;\n  className?: string;\n}\n\ninterface Beam {\n  col: number;\n  y: number;\n  v: number;\n  len: number;\n  life: number;\n}\n\nexport function BeamsUpstream({\n  color = \"#38bdf8\",\n  gridColor = \"#334155\",\n  gap = 56,\n  speed = 1,\n  density = 4,\n  length = 0.28,\n  className,\n}: BeamsUpstreamProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ color, gridColor, gap, speed, density, length });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { color, gridColor, gap, speed, density, length };\n    redraw.current?.();\n  }, [color, gridColor, gap, speed, density, length]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const canvas = el.querySelector(\"canvas\")!;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n\n    let dpr = 1;\n    let raf = 0;\n    let visible = true;\n    let last = 0;\n    let beams: Beam[] = [];\n\n    const geometry = () => {\n      const g = Math.max(12, opts.current.gap) * dpr;\n      const cols = Math.floor(canvas.width / g) + 1;\n      const ox = (canvas.width - (cols - 1) * g) / 2;\n      const oy = (canvas.height % g) / 2;\n      return { g, cols, ox, oy };\n    };\n\n    const spawn = (anywhere: boolean): Beam => {\n      const { cols } = geometry();\n      const H = canvas.height;\n      const len = H * opts.current.length * (0.6 + Math.random() * 0.8);\n      return {\n        col: (Math.random() * cols) | 0,\n        y: anywhere ? Math.random() * (H + len) : H + len * Math.random(),\n        v: H * (0.18 + Math.random() * 0.3),\n        len,\n        life: 0.55 + Math.random() * 0.45,\n      };\n    };\n\n    const target = () => {\n      const { cols } = geometry();\n      return Math.max(1, Math.round((cols / 10) * opts.current.density));\n    };\n\n    const draw = () => {\n      const o = opts.current;\n      const W = canvas.width;\n      const H = canvas.height;\n      const { g, ox, oy, cols } = geometry();\n      ctx.clearRect(0, 0, W, H);\n\n      // Grid.\n      ctx.strokeStyle = o.gridColor;\n      ctx.globalAlpha = 0.55;\n      ctx.lineWidth = dpr;\n      ctx.beginPath();\n      for (let c = 0; c < cols; c++) {\n        const x = Math.round(ox + c * g) + 0.5;\n        ctx.moveTo(x, 0);\n        ctx.lineTo(x, H);\n      }\n      for (let y = oy; y <= H; y += g) {\n        ctx.moveTo(0, Math.round(y) + 0.5);\n        ctx.lineTo(W, Math.round(y) + 0.5);\n      }\n      ctx.stroke();\n      ctx.globalAlpha = 1;\n\n      // Beams: a fading tail below a bright head, drawn additively.\n      ctx.globalCompositeOperation = \"lighter\";\n      for (const b of beams) {\n        const x = Math.round(ox + b.col * g) + 0.5;\n        const top = b.y;\n        const bottom = b.y + b.len;\n        const tail = ctx.createLinearGradient(0, top, 0, bottom);\n        tail.addColorStop(0, o.color);\n        tail.addColorStop(1, \"transparent\");\n        ctx.globalAlpha = b.life;\n        ctx.fillStyle = tail;\n        ctx.fillRect(x - 3 * dpr, top, 6 * dpr, b.len);\n        ctx.globalAlpha = b.life * 0.25;\n        ctx.fillRect(x - 10 * dpr, top, 20 * dpr, b.len * 0.6);\n        ctx.globalAlpha = b.life;\n        ctx.fillStyle = \"#ffffff\";\n        ctx.fillRect(x - 0.75 * dpr, top, 1.5 * dpr, b.len * 0.35);\n        // Head glow.\n        const r = 14 * dpr;\n        const head = ctx.createRadialGradient(x, top, 0, x, top, r);\n        head.addColorStop(0, o.color);\n        head.addColorStop(1, \"transparent\");\n        ctx.fillStyle = head;\n        ctx.fillRect(x - r, top - r, r * 2, r * 2);\n      }\n      ctx.globalAlpha = 1;\n      ctx.globalCompositeOperation = \"source-over\";\n    };\n\n    const seed = () => {\n      beams = Array.from({ length: target() }, () => spawn(true));\n    };\n\n    const loop = (now: number) => {\n      const dt = (Math.min(now - last, 50) / 1000) * opts.current.speed;\n      last = now;\n      for (const b of beams) b.y -= b.v * dt;\n      beams = beams.filter((b) => b.y + b.len > 0);\n      const want = target();\n      // Top up gradually so beams stagger instead of arriving in waves.\n      if (beams.length < want && Math.random() < 0.25) beams.push(spawn(false));\n      if (beams.length > want) beams.length = want;\n      draw();\n      raf = requestAnimationFrame(loop);\n    };\n    const play = () => {\n      if (raf || !visible || reduced) return;\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    };\n    const resize = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.max(1, Math.round(el.clientWidth * dpr));\n      canvas.height = Math.max(1, Math.round(el.clientHeight * dpr));\n      seed();\n      draw();\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    const io = new IntersectionObserver(([e]) => {\n      visible = e.isIntersecting;\n      if (visible) play();\n      else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    redraw.current = draw;\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      redraw.current = null;\n    };\n  }, []);\n\n  return (\n    <div\n      ref={host}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, overflow: \"hidden\", pointerEvents: \"none\" }}\n    >\n      <canvas\n        style={{\n          position: \"absolute\",\n          inset: 0,\n          width: \"100%\",\n          height: \"100%\",\n          display: \"block\",\n          maskImage: \"radial-gradient(ellipse 75% 70% at 50% 50%, #000 35%, transparent 100%)\",\n          WebkitMaskImage: \"radial-gradient(ellipse 75% 70% at 50% 50%, #000 35%, transparent 100%)\",\n        }}\n      />\n    </div>\n  );\n}\n"}]}