-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
43 lines (34 loc) · 1.38 KB
/
main.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
// Get the canvas node and the drawing context
const canvas = document.getElementById('canvasMatrix');
const ctx = canvas.getContext('2d');
// set the width and height of the canvas
const w = canvas.width = document.body.offsetWidth;
const h = canvas.height = document.body.offsetHeight;
// draw a black rectangle of width and height same as that of the canvas
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, w, h);
const cols = Math.floor(w / 20) + 1;
const ypos = Array(cols).fill(0);
function matrix () {
// Draw a semitransparent black rectangle on top of previous drawing
ctx.fillStyle = '#0001';
ctx.fillRect(0, 0, w, h);
// Set color to green and font to 15pt monospace in the drawing context
ctx.fillStyle = '#0f0';
ctx.font = '20pt monospace';
// for each column put a random character at the end
ypos.forEach((y, ind) => {
// generate a random character
const text = String.fromCharCode(Math.random() * 122);
// x coordinate of the column, y coordinate is already given
const x = ind * 20;
// render the character at (x, y)
ctx.fillText(text, x, y);
// randomly reset the end of the column if it's at least 100px high
if (y > 100 + Math.random() * 10000) ypos[ind] = 0;
// otherwise just move the y coordinate for the column 20px down,
else ypos[ind] = y + 20;
});
}
// render the animation at 20 FPS.
setInterval(matrix, 50);