|
| 1 | +import React, { useRef, useEffect } from 'react'; |
| 2 | + |
| 3 | +const DigitalRain = () => { |
| 4 | + const canvasRef = useRef(null); |
| 5 | + |
| 6 | + useEffect(() => { |
| 7 | + const canvas = canvasRef.current; |
| 8 | + const ctx = canvas.getContext('2d'); |
| 9 | + |
| 10 | + // Set canvas to full width and height of its container |
| 11 | + canvas.width = canvas.offsetWidth; |
| 12 | + canvas.height = canvas.offsetHeight; |
| 13 | + |
| 14 | + const katakana = 'アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズブヅプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン'; |
| 15 | + const latin = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 16 | + const nums = '0123456789'; |
| 17 | + const alphabet = katakana + latin + nums; |
| 18 | + |
| 19 | + const fontSize = 16; |
| 20 | + const columns = canvas.width / fontSize; |
| 21 | + const rainDrops = Array.from({ length: columns }).map(() => 1); |
| 22 | + |
| 23 | + let animationFrameId; |
| 24 | + let frameCount = 0; |
| 25 | + const slowdownFactor = 3; // Higher number = slower rain |
| 26 | + |
| 27 | + const draw = () => { |
| 28 | + ctx.fillStyle = 'rgba(21, 21, 21, 0.05)'; // Semi-transparent black for fading effect |
| 29 | + ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 30 | + |
| 31 | + ctx.fillStyle = '#0F0'; // Green text |
| 32 | + ctx.font = `${fontSize}px monospace`; |
| 33 | + |
| 34 | + frameCount++; |
| 35 | + |
| 36 | + for (let i = 0; i < rainDrops.length; i++) { |
| 37 | + const text = alphabet.charAt(Math.floor(Math.random() * alphabet.length)); |
| 38 | + ctx.fillText(text, i * fontSize, rainDrops[i] * fontSize); |
| 39 | + |
| 40 | + if (frameCount % slowdownFactor === 0) { |
| 41 | + if (rainDrops[i] * fontSize > canvas.height && Math.random() > 0.975) { |
| 42 | + rainDrops[i] = 0; |
| 43 | + } |
| 44 | + rainDrops[i]++; |
| 45 | + } |
| 46 | + } |
| 47 | + animationFrameId = requestAnimationFrame(draw); |
| 48 | + }; |
| 49 | + draw(); |
| 50 | + return () => { |
| 51 | + cancelAnimationFrame(animationFrameId); |
| 52 | + }; |
| 53 | + }, []); |
| 54 | + |
| 55 | + // Style for the canvas to fill the modal body |
| 56 | + const canvasStyle = { |
| 57 | + display: 'block', |
| 58 | + width: '100%', |
| 59 | + height: '60vh', // Make it tall inside the modal |
| 60 | + }; |
| 61 | + |
| 62 | + return ( |
| 63 | + <canvas ref={canvasRef} style={canvasStyle}></canvas> |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +export default DigitalRain; |
0 commit comments