Matrix Code Rain
nyxui/matrix-code-rainFreeComponent
A matrix code rain that that provide several effects.
Live preview
live · rendered by the registry
Rendered by shadcn/ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://nyxui.com/r/matrix-code-rain.jsonSource
1"use client";2import { cn } from "@/lib/utils";3import React, { useEffect, useRef } from "react";45type MatrixCodeRainProps = {6 color?: string;7 charset?: string;8 fontSize?: number;9 fps?: number;10 opacity?: number;11 fullScreen?: boolean;12 width?: string;13 height?: string;14};1516export const MatrixCodeRain = ({17 color = "#00ff00",18 charset = "0123#!$^&456789ABCDEFRLY",19 fontSize = 16,20 fps = 20,21 opacity = 0.05,22 fullScreen = false,23 width = "100%",24 height = "400px",25}: MatrixCodeRainProps) => {26 const canvasRef = useRef<HTMLCanvasElement>(null);27 const animationRef = useRef<number | null>(null);28 const particlesRef = useRef<number[]>([]);2930 useEffect(() => {31 const canvas = canvasRef.current;32 if (!canvas) return;3334 const ctx = canvas.getContext("2d");35 if (!ctx) return;3637 let w = 0,38 h = 0;39 let lastFrameTime = 0;40 const frameInterval = 1000 / fps;4142 const resize = () => {43 const container = canvas.parentElement;44 if (!container) return;4546 if (fullScreen) {47 w = canvas.width = window.innerWidth;48 h = canvas.height = window.innerHeight;49 } else {50 w = canvas.width = container.clientWidth;51 h = canvas.height = container.clientHeight;52 }5354 particlesRef.current = new Array(Math.ceil(w / fontSize)).fill(0);55 };5657 const random = (items: string) =>58 items[Math.floor(Math.random() * items.length)];5960 const draw = (timestamp: number) => {61 if (timestamp - lastFrameTime >= frameInterval) {62 ctx.fillStyle = `rgba(0,0,0,${opacity})`;63 ctx.fillRect(0, 0, w, h);64 ctx.fillStyle = color;65 ctx.font = `${fontSize}px monospace`;6667 for (let i = 0; i < particlesRef.current.length; i++) {68 const v = particlesRef.current[i];69 ctx.fillText(random(charset), i * fontSize, v);70 particlesRef.current[i] =71 v >= h || v >= 10000 * Math.random() ? 0 : v + fontSize;72 }7374 lastFrameTime = timestamp;75 }7677 animationRef.current = requestAnimationFrame(draw);78 };7980 const handleResize = () => resize();8182 window.addEventListener("resize", handleResize);83 resize();84 animationRef.current = requestAnimationFrame(draw);8586 return () => {87 window.removeEventListener("resize", handleResize);88 if (animationRef.current) {89 cancelAnimationFrame(animationRef.current);90 }91 };92 }, [color, charset, fontSize, fps, opacity, fullScreen]);9394// … truncated
Files it writes
More from shadcn/ui
All 68 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| 3D Layered Card3d-layered-card | shadcn/ui | Components | Free | 1 dep | |
| Animated Code Blockanimated-code-block | shadcn/ui | Components | Free | 3 deps | |
| Animated Grainy Backgroundanimated-grainy-bg | shadcn/ui | Components | Free | 1 dep | |
| Animated Textanimated-text | shadcn/ui | Components | Free | 1 dep | |
| Apple Glass Effectapple-glass-effect | shadcn/ui | Components | Free | 1 dep | |
| Bubble Backgroundbubble-background | shadcn/ui | Components | Free | no deps | |
| Custom Cursorcustom-cursor | shadcn/ui | Components | Free | 1 dep | |
| Cyberpunk Cardcyberpunk-card | shadcn/ui | Components | Free | no deps |
