-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
153 lines (119 loc) · 3.51 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import './style.css';
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const letterSize = 20;
const numRows = Math.ceil(canvas.width / letterSize);
const text =
'我你不abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const textArray = text.split('');
let lanes = [];
let voidLanes = [];
const numberInRange = (min, max) => {
return Math.ceil(Math.random() * (max - min) + min);
};
const clearCanvas = () => {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
};
const drawText = (text, x, y, a) => {
ctx.fillStyle = `rgba(0,255,65,${a})`;
ctx.font = letterSize + 'px monospace';
ctx.fillText(text, x, y);
};
const updateRandomLetters = () => {
let randLength = numberInRange(12, 20);
let randLetters = [];
for (let k = 0; k < randLength; k++) {
let l = textArray[Math.floor(Math.random() * (textArray.length - 1))];
randLetters.push(l);
}
return randLetters;
};
const updateRandomAlphas = (letters) => {
let alphas = [];
let alphaStart = Math.random();
for (let i = 0; i < letters.length; i++) {
if (alphaStart > 1.5) {
alphaStart = 0;
}
alphas.push(alphaStart);
alphaStart += 0.15;
}
return alphas;
};
// A lane is a column of strings stacked on top of each other
// with the alpha values increasing as the you get the the letter
// closest to the bottom of the screen
const lane = (x, y, ls) => {
// Create alpha values based on the position of the letter
let alphas = updateRandomAlphas(ls);
return {
x: x,
y: y,
letters: ls,
speed: numberInRange(3, 8),
update: function () {
// Start all the lanes off screen
let startY = this.y - this.letters.length * letterSize;
for (let i = 0; i < this.letters.length; i++) {
let l = this.letters[i];
let y = startY + letterSize * i;
let a = alphas[i];
// Change to a random letter
if (Math.random() > 0.95) {
l = textArray[Math.floor(Math.random() * (textArray.length - 1))];
this.letters[i] = l;
}
drawText(l, this.x, y, a);
}
// If the entire string of letters goes off canvas remove it
if (
this.y - this.letters.length * letterSize >
canvas.height + letterSize
) {
this.y = -letterSize;
// Update the letters in the lane
this.letters = updateRandomLetters();
// Update their alphas
alphas = updateRandomAlphas(this.letters);
}
this.y += this.speed;
},
};
};
// Create columns of blank space so lanes aren't so close together
const updateVoidLanes = () => {
voidLanes = [];
const randVoidLanes = numberInRange(5, 9);
for (let i = 0; i < randVoidLanes; i++) {
const voidLane = Math.ceil(Math.random() * lanes.length);
voidLanes.push(voidLane);
}
};
const init = () => {
for (let i = 0; i < numRows; i++) {
let x = i * letterSize;
let y = Math.ceil(Math.random() * 10 * letterSize);
let randLetters = updateRandomLetters();
lanes.push(lane(x, y, randLetters));
}
updateVoidLanes();
};
const draw = () => {
clearCanvas();
ctx.save();
ctx.fillStyle = '#0D0208';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < lanes.length; i++) {
if (voidLanes.indexOf(i) !== -1) {
continue;
}
let lane = lanes[i];
lane.update();
}
ctx.restore();
window.requestAnimationFrame(draw);
};
// Call the init function to start everything off
init();
window.requestAnimationFrame(draw);