matrix-terminal-posthog-cv/web/static/js/matrix-rain.js
Kyle Stevenson a2bd58e1b0 frontend
2026-08-11 14:20:56 +01:00

69 lines
2.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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,
};
}