const CHARACTERS = "アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズヅブプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン0123456789"; export function initMatrixRain(canvasId) { const canvas = document.getElementById(canvasId); if (!canvas) return null; const ctx = canvas.getContext("2d"); const fontSize = 16; let width, height, drops; let intervalId = null; function resize() { width = canvas.width = window.innerWidth; height = canvas.height = window.innerHeight; const columns = Math.floor(width / fontSize); const rows = Math.ceil(height / fontSize); drops = new Array(columns) .fill(0) .map(() => -Math.floor(Math.random() * rows)); } function draw() { ctx.fillStyle = "rgba(0, 0, 0, 0.05)"; ctx.fillRect(0, 0, width, height); ctx.fillStyle = "#00ff41"; ctx.font = `${fontSize}px monospace`; for (let i = 0; i < drops.length; i++) { const char = CHARACTERS[Math.floor(Math.random() * CHARACTERS.length)]; const x = i * fontSize; const y = drops[i] * fontSize; ctx.fillText(char, x, y); if (y > height && Math.random() > 0.975) { drops[i] = 0; } drops[i]++; } } function start() { if (intervalId) return; canvas.classList.remove("hidden"); resize(); draw(); intervalId = setInterval(draw, 90); } function stop() { if (!intervalId) return; clearInterval(intervalId); intervalId = null; ctx.clearRect(0, 0, width, height); canvas.classList.add("hidden"); } window.addEventListener("resize", () => { if (intervalId) resize(); }); start(); return { toggle() { if (intervalId) stop(); else start(); }, isRunning: () => intervalId !== null, }; }