|
| 1 | +import * as React from 'react' |
| 2 | + |
| 3 | +type AnimationHandle = { |
| 4 | + rafId: number | null |
| 5 | +} |
| 6 | + |
| 7 | +export function MatrixBackground(): JSX.Element { |
| 8 | + const canvasRef = React.useRef<HTMLCanvasElement | null>(null) |
| 9 | + const handleRef = React.useRef<AnimationHandle>({ rafId: null }) |
| 10 | + |
| 11 | + React.useEffect(() => { |
| 12 | + const canvas = canvasRef.current |
| 13 | + if (!canvas) return |
| 14 | + |
| 15 | + const ctx = canvas.getContext('2d', { alpha: true }) |
| 16 | + if (!ctx) return |
| 17 | + |
| 18 | + const dpr = Math.max(1, Math.min(2, window.devicePixelRatio || 1)) |
| 19 | + let width = 0 |
| 20 | + let height = 0 |
| 21 | + let columns = 0 |
| 22 | + let drops: Array<number> = [] |
| 23 | + |
| 24 | + const glyphs = ['0', '1'] |
| 25 | + const baseFont = 14 // logical pixels |
| 26 | + const speed = 0.2 // fall speed factor |
| 27 | + |
| 28 | + function resize() { |
| 29 | + const { innerWidth, innerHeight } = window |
| 30 | + width = innerWidth |
| 31 | + height = innerHeight |
| 32 | + canvas.width = Math.floor(width * dpr) |
| 33 | + canvas.height = Math.floor(height * dpr) |
| 34 | + canvas.style.width = `${width}px` |
| 35 | + canvas.style.height = `${height}px` |
| 36 | + ctx.setTransform(dpr, 0, 0, dpr, 0, 0) |
| 37 | + |
| 38 | + const fontSize = baseFont |
| 39 | + ctx.font = `${fontSize}px ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace` |
| 40 | + columns = Math.max(1, Math.floor(width / fontSize)) |
| 41 | + drops = new Array(columns).fill(0).map(() => Math.floor(Math.random() * 20)) |
| 42 | + } |
| 43 | + |
| 44 | + function step() { |
| 45 | + if (document.hidden) { |
| 46 | + handleRef.current.rafId = requestAnimationFrame(step) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + // Trail fade |
| 51 | + ctx.fillStyle = 'rgba(0,0,0,0.08)' |
| 52 | + ctx.fillRect(0, 0, width, height) |
| 53 | + |
| 54 | + // Draw glyphs |
| 55 | + ctx.fillStyle = 'rgba(234,179,8,0.85)' // yellow-500 with alpha |
| 56 | + ctx.shadowColor = 'rgba(234,179,8,0.25)' |
| 57 | + ctx.shadowBlur = 0 |
| 58 | + |
| 59 | + const fontSize = baseFont |
| 60 | + for (let i = 0; i < columns; i++) { |
| 61 | + const text = glyphs[(Math.random() * glyphs.length) | 0] |
| 62 | + const x = i * fontSize |
| 63 | + const y = drops[i] * fontSize |
| 64 | + ctx.fillText(text, x, y) |
| 65 | + |
| 66 | + // Reset drop randomly after it exits the screen to vary stream lengths |
| 67 | + if (y > height && Math.random() > 0.975) { |
| 68 | + drops[i] = 0 |
| 69 | + } |
| 70 | + |
| 71 | + drops[i] += speed + Math.random() * 0.5 |
| 72 | + } |
| 73 | + |
| 74 | + handleRef.current.rafId = requestAnimationFrame(step) |
| 75 | + } |
| 76 | + |
| 77 | + const onResize = () => resize() |
| 78 | + resize() |
| 79 | + handleRef.current.rafId = requestAnimationFrame(step) |
| 80 | + window.addEventListener('resize', onResize, { passive: true }) |
| 81 | + |
| 82 | + return () => { |
| 83 | + window.removeEventListener('resize', onResize) |
| 84 | + if (handleRef.current.rafId != null) cancelAnimationFrame(handleRef.current.rafId) |
| 85 | + } |
| 86 | + }, []) |
| 87 | + |
| 88 | + return ( |
| 89 | + <canvas |
| 90 | + ref={canvasRef} |
| 91 | + className="fixed inset-0 z-0 opacity-20 pointer-events-none bg-transparent" |
| 92 | + aria-hidden="true" |
| 93 | + /> |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | + |
0 commit comments