1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
// matrix-rain.js
(function () {
const canvas = document.getElementById('matrix-canvas');
if (!canvas) return;
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
const ctx = canvas.getContext('2d');
const CHARS = 'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン0123456789ABCDEF<>/\\|{}[]$#@!';
const FS = 14; // font size / column width in px
const mode = canvas.getAttribute('data-mode') || 'background';
let cols, drops, raf;
function init() {
// Use offsetWidth/offsetHeight which works for both fixed and positioned elements
canvas.width = canvas.offsetWidth || window.innerWidth;
canvas.height = canvas.offsetHeight || window.innerHeight;
cols = Math.floor(canvas.width / FS) + 1;
drops = Array.from({ length: cols }, () => Math.random() * -(canvas.height / FS));
}
function getThemeColors() {
const isDark = !document.documentElement.classList.contains('theme-light');
return {
bgFill: isDark ? 'rgba(6, 11, 16, 0.07)' : 'rgba(240, 244, 248, 0.07)',
};
}
function tick() {
const colors = getThemeColors();
ctx.fillStyle = colors.bgFill;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = `${FS}px "JetBrains Mono", monospace`;
for (let i = 0; i < cols; i++) {
const char = CHARS[Math.floor(Math.random() * CHARS.length)];
// Occasional bright green "head", otherwise purple
ctx.fillStyle = Math.random() > 0.96 ? '#00ff88' : '#a855f7';
ctx.fillText(char, i * FS, drops[i] * FS);
if (drops[i] * FS > canvas.height && Math.random() > 0.975) {
drops[i] = Math.random() * -20;
}
drops[i] += 0.5;
}
raf = requestAnimationFrame(tick);
}
init();
window.addEventListener('resize', () => { cancelAnimationFrame(raf); init(); tick(); }, { passive: true });
document.addEventListener('visibilitychange', () => {
if (document.hidden) cancelAnimationFrame(raf); else tick();
});
tick();
window.MatrixRain = { init, tick };
})();
|