-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.js
53 lines (44 loc) · 1.44 KB
/
matrix.js
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
// Matrix Rain Effect
const canvas = document.getElementById('matrixCanvas');
const ctx = canvas.getContext('2d');
// Set canvas size
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
// Matrix rain characters
const chars = "@drbaph".split('');
const fontSize = 14;
let columns = Math.floor(window.innerWidth / fontSize);
let drops = new Array(columns).fill(0);
// Matrix rain animation
function drawMatrixRain() {
ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#ffffff';
ctx.font = fontSize + 'px monospace';
for (let i = 0; i < drops.length; i++) {
const char = chars[Math.floor(Math.random() * chars.length)];
ctx.fillText(char, i * fontSize, drops[i] * fontSize);
if (drops[i] * fontSize > canvas.height && Math.random() > 0.975) {
drops[i] = 0;
}
drops[i]++;
}
}
// Handle window resize
window.addEventListener('resize', () => {
resizeCanvas();
columns = Math.floor(window.innerWidth / fontSize);
drops = new Array(columns).fill(0);
});
// Start Matrix rain
let matrixInterval = setInterval(drawMatrixRain, 50);
// Stop matrix rain when loading is done
window.addEventListener('load', () => {
setTimeout(() => {
clearInterval(matrixInterval);
}, 2500);
});