-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.js
43 lines (35 loc) · 1.04 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
const canvas = document.getElementById("matrix");
const ctx = canvas.getContext("2d");
function setupCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
setupCanvas(); // Call the function initially to set up the canvas dimensions
const columns = Math.floor(canvas.width / 20);
const matrix = [];
for (let i = 0; i < columns; i++) {
matrix[i] = Math.floor(Math.random() * canvas.height);
}
function draw() {
ctx.fillStyle = "rgba(0, 0, 0, 0.05)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "blue";
ctx.font = "15px Courier";
for (let i = 0; i < columns; i++) {
ctx.fillText(
String.fromCharCode(Math.floor(Math.random() * 128)),
i * 20,
matrix[i] * 20
);
if (matrix[i] * 20 > canvas.height && Math.random() > 0.975) {
matrix[i] = 0;
}
matrix[i]++;
}
}
function update() {
draw();
// Adjust the value (e.g., 60) to change the speed. Lower values make it faster.
setTimeout(() => requestAnimationFrame(update), 1000 / 10);
}
update();