-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
79 lines (62 loc) · 1.89 KB
/
script.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
// width and height of window
const WIDTH = window.innerWidth,
HEIGHT = window.innerHeight;
// array containing droplet positions
var raindrops;
// size of droplets in pixels
var dropSize = 8;
// chars to be displayed
var charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// run on window load
window.onload = function(e) {
// canvas setup
var canvas = document.getElementById("canvas");
var c = canvas.getContext("2d");
canvas.width = WIDTH;
canvas.height = HEIGHT;
// initial setup
init(c);
// main logic loop
var loop = function() {
update();
render(c);
window.requestAnimationFrame(loop, canvas);
}
window.requestAnimationFrame(loop, canvas);
}
function init(c) {
// initialize raindrops array
raindrops = new Array(Math.round(WIDTH / dropSize));
for (var i = 0; i < raindrops.length; i++) {
raindrops[i] = Math.random() * HEIGHT / dropSize;
}
// set font and font size
c.font = dropSize + "px Arial";
}
function update() {
// update raindrop positions
for (var i = 0; i < raindrops.length; i++) {
raindrops[i] += 1;
if (raindrops[i] > HEIGHT / dropSize) {
raindrops[i] = 0;
}
}
}
function render(c) {
// render raindrops
for (var i = 0; i < raindrops.length; i++) {
// generate and set random color
var r = Math.random() * 255,
g = Math.random() * 255,
b = Math.random() * 255;
c.fillStyle = "rgb(" + r + ", " + g + ", " + b + ")";
// choose and render char form char set
var randomChar = charSet.charAt(Math.round(Math.random() * charSet.length));
c.fillText(randomChar, i * (WIDTH / raindrops.length), raindrops[i] * dropSize);
}
// screen fade out effect
c.fillStyle = "black";
c.globalAlpha = 0.06;
c.fillRect(0, 0, WIDTH, HEIGHT)
c.globalAlpha = 1;
}