{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "matrix-rain",
  "type": "registry:ui",
  "title": "Matrix Rain",
  "author": "Rishabh Bose <https://theb0se.vercel.app/>",
  "description": "Canvas 2D digital rain cascade with incandescent white leading glyphs, phosphor trail decay, and real-time character mutation across Katakana, Binary, and Hexadecimal character sets.",
  "dependencies": [
    "lucide-react",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "components/ui/matrix-rain.tsx",
      "content": "\"use client\";\n\nimport React, { useEffect, useRef, useState } from \"react\";\nimport { Terminal, Zap, Cpu, RefreshCw, Layers } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface MatrixRainProps {\n  /** Primary phosphor color in hex format (e.g., \"#10B981\") */\n  color?: string;\n  /** Fall speed multiplier */\n  speed?: number;\n  /** Stream density multiplier (0.3 to 2.0) */\n  density?: number;\n  /** Glyph character set */\n  charset?: \"katakana\" | \"binary\" | \"hex\";\n  /** Font size in pixels */\n  fontSize?: number;\n  /** Optional overlay content */\n  children?: React.ReactNode;\n  /** Additional container classes */\n  className?: string;\n}\n\nconst CHARSETS: Record<\"katakana\" | \"binary\" | \"hex\", string> = {\n  katakana:\n    \"アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズブヅプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ<>|=+*:\",\n  binary: \"01\",\n  hex: \"0123456789ABCDEF\",\n};\n\ninterface StreamDrop {\n  layer: 0 | 1 | 2; // 0 = far, 1 = mid, 2 = near\n  x: number;\n  y: number;\n  speed: number;\n  length: number;\n  fontSize: number;\n  chars: string[];\n  glitchTimers: number[];\n  mutationRate: number;\n}\n\nfunction hexToRgb(hex: string): { r: number; g: number; b: number } {\n  const clean = hex.replace(\"#\", \"\");\n  const full =\n    clean.length === 3\n      ? clean\n          .split(\"\")\n          .map((c) => c + c)\n          .join(\"\")\n      : clean;\n  const num = parseInt(full, 16);\n  if (Number.isNaN(num)) return { r: 16, g: 185, b: 129 };\n  return {\n    r: (num >> 16) & 255,\n    g: (num >> 8) & 255,\n    b: num & 255,\n  };\n}\n\nexport function MatrixRain({\n  color: propColor = \"#10B981\",\n  speed: propSpeed = 1.0,\n  density: propDensity = 1.0,\n  charset: propCharset = \"katakana\",\n  fontSize: propFontSize = 15,\n  children,\n  className,\n}: MatrixRainProps) {\n  const containerRef = useRef<HTMLDivElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const pointerRef = useRef<{ x: number; y: number; active: boolean }>({\n    x: -9999,\n    y: -9999,\n    active: false,\n  });\n\n  const [activeCharset, setActiveCharset] = useState<\n    \"katakana\" | \"binary\" | \"hex\"\n  >(propCharset);\n  const [activeColor, setActiveColor] = useState<string>(propColor);\n  const [activeSpeed, setActiveSpeed] = useState<number>(propSpeed);\n  const [activeDensity, setActiveDensity] = useState<number>(propDensity);\n  const [streamCount, setStreamCount] = useState<number>(0);\n\n  useEffect(() => {\n    setActiveCharset(propCharset);\n  }, [propCharset]);\n\n  useEffect(() => {\n    setActiveColor(propColor);\n  }, [propColor]);\n\n  useEffect(() => {\n    setActiveSpeed(propSpeed);\n  }, [propSpeed]);\n\n  useEffect(() => {\n    setActiveDensity(propDensity);\n  }, [propDensity]);\n\n  useEffect(() => {\n    const container = containerRef.current;\n    const canvas = canvasRef.current;\n    if (!container || !canvas) return;\n\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n\n    let animationFrameId = 0;\n    let width = 0;\n    let height = 0;\n    let drops: StreamDrop[] = [];\n\n    const glyphPool = CHARSETS[activeCharset] || CHARSETS.katakana;\n    const randomGlyph = () =>\n      glyphPool[Math.floor(Math.random() * glyphPool.length)] || \"0\";\n\n    const createLayerDrops = (\n      layer: 0 | 1 | 2,\n      layerFontSize: number,\n      densityFactor: number,\n      speedRange: [number, number],\n      lengthRange: [number, number]\n    ): StreamDrop[] => {\n      const colStep = Math.max(9, layerFontSize);\n      const totalCols = Math.ceil(width / colStep);\n      const count = Math.max(\n        4,\n        Math.floor(\n          totalCols *\n            densityFactor *\n            Math.min(2.2, Math.max(0.3, activeDensity))\n        )\n      );\n\n      return Array.from({ length: count }, (_, i) => {\n        const colIndex = Math.floor((i / count) * totalCols);\n        const length = Math.floor(\n          lengthRange[0] + Math.random() * (lengthRange[1] - lengthRange[0])\n        );\n        return {\n          layer,\n          x:\n            colIndex * colStep +\n            layerFontSize * 0.5 +\n            (Math.random() - 0.5) * 4,\n          y: Math.random() * height * 1.4 - height * 0.7,\n          speed:\n            (speedRange[0] + Math.random() * (speedRange[1] - speedRange[0])) *\n            layerFontSize *\n            0.12,\n          length,\n          fontSize: layerFontSize,\n          chars: Array.from({ length: length + 8 }, randomGlyph),\n          glitchTimers: Array.from({ length: length + 8 }, () =>\n            Math.random()\n          ),\n          mutationRate:\n            layer === 1\n              ? 0.03 + Math.random() * 0.06\n              : 0.015 + Math.random() * 0.04,\n        };\n      });\n    };\n\n    const initStreams = () => {\n      const farFont = Math.max(10, Math.round(propFontSize * 0.72));\n      const midFont = Math.max(12, propFontSize);\n      const nearFont = Math.max(18, Math.round(propFontSize * 1.48));\n\n      const farDrops = createLayerDrops(0, farFont, 0.75, [0.45, 0.85], [14, 28]);\n      const midDrops = createLayerDrops(1, midFont, 0.95, [0.85, 1.55], [14, 30]);\n      const nearDrops = createLayerDrops(2, nearFont, 0.22, [1.45, 2.25], [8, 16]);\n\n      drops = [...farDrops, ...midDrops, ...nearDrops];\n      setStreamCount(drops.length);\n    };\n\n    const updateSize = () => {\n      const rect = container.getBoundingClientRect();\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      width = Math.max(1, Math.floor(rect.width));\n      height = Math.max(1, Math.floor(rect.height));\n      canvas.width = width * dpr;\n      canvas.height = height * dpr;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      initStreams();\n    };\n\n    updateSize();\n    const observer = new ResizeObserver(updateSize);\n    observer.observe(container);\n\n    const rgb = hexToRgb(activeColor);\n    const brightR = Math.min(255, rgb.r + 135);\n    const brightG = Math.min(255, rgb.g + 135);\n    const brightB = Math.min(255, rgb.b + 135);\n\n    const render = () => {\n      // Crisp full-frame dark phosphor clear + subtle ambient depth gradient\n      ctx.fillStyle = \"#02060A\";\n      ctx.fillRect(0, 0, width, height);\n\n      const bgGlow = ctx.createRadialGradient(\n        width * 0.5,\n        height * 0.45,\n        20,\n        width * 0.5,\n        height * 0.5,\n        Math.max(width, height) * 0.75\n      );\n      bgGlow.addColorStop(0, `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 0.07)`);\n      bgGlow.addColorStop(1, \"rgba(2, 6, 10, 0)\");\n      ctx.fillStyle = bgGlow;\n      ctx.fillRect(0, 0, width, height);\n\n      const ptr = pointerRef.current;\n      const parallaxOffset = ptr.active\n        ? (ptr.x / Math.max(1, width) - 0.5) * 28\n        : 0;\n\n      ctx.textAlign = \"center\";\n      ctx.textBaseline = \"middle\";\n\n      let currentLayer = -1;\n\n      for (let i = 0; i < drops.length; i++) {\n        const drop = drops[i];\n\n        if (drop.layer !== currentLayer) {\n          currentLayer = drop.layer;\n          const weight = currentLayer === 0 ? \"500\" : \"700\";\n          ctx.font = `${weight} ${drop.fontSize}px ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace`;\n        }\n\n        drop.y += drop.speed * activeSpeed;\n\n        const layerShift =\n          drop.layer === 0\n            ? -parallaxOffset * 0.35\n            : drop.layer === 1\n            ? parallaxOffset * 0.25\n            : parallaxOffset * 0.85;\n\n        const drawX = ((drop.x + layerShift) % width + width) % width;\n        const stepY = drop.fontSize * 1.05;\n        const headRow = Math.floor(drop.y / stepY);\n\n        // Soft vertical phosphor bloom spine behind mid & near streams\n        if (drop.layer >= 1) {\n          const headY = headRow * stepY;\n          const tailTopY = (headRow - drop.length * 0.75) * stepY;\n          if (headY > 0 && tailTopY < height) {\n            const spineGrad = ctx.createLinearGradient(\n              drawX,\n              Math.max(0, tailTopY),\n              drawX,\n              Math.min(height, headY + stepY)\n            );\n            const maxSpineAlpha = drop.layer === 2 ? 0.14 : 0.09;\n            spineGrad.addColorStop(0, `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 0)`);\n            spineGrad.addColorStop(\n              0.85,\n              `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${maxSpineAlpha})`\n            );\n            spineGrad.addColorStop(\n              1,\n              `rgba(${brightR}, ${brightG}, ${brightB}, ${maxSpineAlpha * 1.5})`\n            );\n            ctx.fillStyle = spineGrad;\n            ctx.fillRect(\n              drawX - drop.fontSize * 0.45,\n              Math.max(0, tailTopY),\n              drop.fontSize * 0.9,\n              Math.min(height, headY + stepY) - Math.max(0, tailTopY)\n            );\n          }\n        }\n\n        const baseLayerAlpha =\n          drop.layer === 0 ? 0.34 : drop.layer === 1 ? 0.92 : 0.52;\n\n        for (let j = 0; j < drop.length; j++) {\n          const charY = (headRow - j) * stepY;\n          if (charY < -drop.fontSize || charY > height + drop.fontSize) {\n            continue;\n          }\n\n          // Deterministic slot tied to grid row so characters stay stationary in space while the illumination wave falls through them\n          const charSlot =\n            ((headRow - j) % drop.chars.length + drop.chars.length) %\n            drop.chars.length;\n\n          if (Math.random() < drop.mutationRate * (j === 0 ? 2.5 : 0.5)) {\n            drop.chars[charSlot] = randomGlyph();\n            drop.glitchTimers[charSlot] = 1.0;\n          } else {\n            drop.glitchTimers[charSlot] = Math.max(\n              0,\n              drop.glitchTimers[charSlot] - 0.04\n            );\n          }\n\n          const ch = drop.chars[charSlot];\n\n          // Pointer proximity boost\n          let pointerBoost = 0;\n          if (ptr.active) {\n            const dist = Math.hypot(drawX - ptr.x, charY - ptr.y);\n            if (dist < 130) {\n              pointerBoost = (1 - dist / 130) * 0.55;\n            }\n          }\n\n          if (j === 0) {\n            // Incandescent white-hot lead character with bloom\n            ctx.save();\n            ctx.shadowColor = `rgba(${brightR}, ${brightG}, ${brightB}, 0.95)`;\n            ctx.shadowBlur = drop.layer === 0 ? 4 : drop.layer === 1 ? 14 : 20;\n            ctx.fillStyle =\n              drop.layer === 0\n                ? `rgba(${brightR}, ${brightG}, ${brightB}, 0.75)`\n                : \"#FFFFFF\";\n            ctx.fillText(ch, drawX, charY);\n            ctx.restore();\n          } else if (j <= 2) {\n            // Sub-head high-energy phosphor transition\n            const subAlpha = Math.min(1, baseLayerAlpha + pointerBoost);\n            ctx.fillStyle = `rgba(${brightR}, ${brightG}, ${brightB}, ${subAlpha.toFixed(\n              3\n            )})`;\n            ctx.fillText(ch, drawX, charY);\n          } else {\n            // Smooth exponential phosphor decay along tail + occasional bright glitch character\n            const progress = j / drop.length;\n            const decay = Math.pow(1 - progress, 1.65);\n            const glitchFlash = drop.glitchTimers[charSlot] * 0.35;\n            const finalAlpha = Math.min(\n              1,\n              (decay * baseLayerAlpha + glitchFlash + pointerBoost)\n            );\n\n            if (finalAlpha > 0.02) {\n              if (drop.glitchTimers[charSlot] > 0.65 && drop.layer === 1) {\n                ctx.fillStyle = `rgba(${brightR}, ${brightG}, ${brightB}, ${finalAlpha.toFixed(\n                  3\n                )})`;\n              } else {\n                ctx.fillStyle = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${finalAlpha.toFixed(\n                  3\n                )})`;\n              }\n              ctx.fillText(ch, drawX, charY);\n            }\n          }\n        }\n\n        // Respawn stream once tail clears bottom of screen\n        if ((headRow - drop.length) * stepY > height + drop.fontSize * 2) {\n          drop.y = -Math.random() * height * 0.45;\n        }\n      }\n\n      animationFrameId = requestAnimationFrame(render);\n    };\n\n    animationFrameId = requestAnimationFrame(render);\n\n    return () => {\n      cancelAnimationFrame(animationFrameId);\n      observer.disconnect();\n    };\n  }, [activeCharset, activeColor, activeSpeed, activeDensity, propFontSize]);\n\n  return (\n    <div\n      ref={containerRef}\n      data-fullscreen-scroll\n      onMouseMove={(e) => {\n        const rect = e.currentTarget.getBoundingClientRect();\n        pointerRef.current = {\n          x: e.clientX - rect.left,\n          y: e.clientY - rect.top,\n          active: true,\n        };\n      }}\n      onMouseLeave={() => {\n        pointerRef.current.active = false;\n      }}\n      className={cn(\n        \"relative w-full min-h-[520px] overflow-hidden rounded-2xl border border-emerald-500/20 bg-[#02060A] text-slate-100 shadow-2xl\",\n        className\n      )}\n    >\n      <canvas\n        ref={canvasRef}\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0 h-full w-full\"\n      />\n\n      {/* Authentic CRT Scanline & Vignette Overlay */}\n      <div\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0 bg-[linear-gradient(to_bottom,transparent_50%,rgba(0,0,0,0.28)_50%)] bg-[length:100%_4px] opacity-60\"\n      />\n      <div\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-0 bg-[radial-gradient(circle_at_center,transparent_40%,rgba(1,4,9,0.86)_100%)]\"\n      />\n\n      {children && (\n        <div className=\"relative z-10 flex min-h-[520px] w-full flex-col\">\n          {children}\n        </div>\n      )}\n    </div>\n  );\n}\n\nexport default MatrixRain;\n",
      "type": "registry:ui",
      "target": "components/ui/matrix-rain.tsx"
    },
    {
      "path": "lib/utils.ts",
      "content": "import { type ClassValue, clsx } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport async function copyToClipboard(text: string): Promise<boolean> {\n  if (typeof window === \"undefined\") return false;\n\n  try {\n    if (navigator.clipboard && window.isSecureContext) {\n      await navigator.clipboard.writeText(text);\n      return true;\n    }\n\n    const textArea = document.createElement(\"textarea\");\n    textArea.value = text;\n    textArea.style.position = \"fixed\";\n    textArea.style.left = \"-999999px\";\n    textArea.style.top = \"-999999px\";\n    document.body.appendChild(textArea);\n    textArea.focus();\n    textArea.select();\n    const succeeded = document.execCommand(\"copy\");\n    textArea.remove();\n    return succeeded;\n  } catch {\n    return false;\n  }\n}\n",
      "type": "registry:lib",
      "target": "lib/utils.ts"
    }
  ]
}
