-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (148 loc) · 4.73 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
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
154
155
/* sets the char size */
Char_Size = 16;
/* create local width and height values */
var w = window.innerWidth;
var h = window.innerHeight;
addEventListener('load', function (e) {
/* creates all of the streams */
for (var i = 0; i < Math.floor(window.innerWidth / Char_Size); i++) {
/* Make new string */
s = new Stream();
/* Move the new strings x */
s.x = Char_Size * i;
/* console log the stream */
console.log("stream", s, i);
/* add to 'array' (list) of streams */
streams.push(s);
}
canvas = document.getElementById("MyCanvas");
/* update local with and height values */
w = window.innerWidth;
h = window.innerHeight;
/* change canvas size */
document.getElementById("MyCanvas").width = w;
document.getElementById("MyCanvas").height = h;
/* Makes the animation start right away */
/* Keep track if the code started the animation yet */
var started = false;
/* While animation is not started */
while (!started) {
/* update all the char's 1/60th */
streams.forEach(s => {
s.update(1 / 60);
/* if on the screen */
if (s.y >= 0)
/* let the loop know we are done */
started = true;
});
}
/* starts frame updater */
window.requestAnimationFrame(step);
});
streams = [];
var last = 0;
class Stream {
constructor() {
/* determine how long the stream is going to be */
this.size = Math.floor(Math.random() * 20) + 5;
/* make an empty array for char's to go */
this.Chars = [];
this.x = 0;
this.y = -600;
/* decide how fast or slow the stream will be going */
this.speed = Math.floor(Math.random() * 100) + 10;
/* decide if the first char will glow or not are the first or not */
var first = Math.floor(Math.random() * 5) == 0;
for (var i = 0; i < this.size; i++) {
/* makes a new char */
var c = new Char(first);
/* add it ti the array */
this.Chars.push(c);
/* make the rest of the chars be normal */
first = false;
}
}
update(now) {
/* moves the stream down the screen */
this.y += this.speed * now;
/* move to the top of the screen if off the screen */
this.y %= h + (this.size * Char_Size);
/* tell all of the char's to update */
this.Chars.forEach(c => {
c.update(now);
});
}
/* displays the stream with the canvas */
display(ctx) {
/* change font */
ctx.font = Char_Size + "px Arial";
for (var i = 0; i < this.size; i++) {
/* if first char */
if (this.Chars[i].First)
/* change the color to light green */
ctx.fillStyle = "#3f3";
/* otherwise */
else
/* change the color to light green */
ctx.fillStyle = "#080";
/* Keep the x the same */
var x = this.x;
/* Find the y value */
var y = this.y - Char_Size * i;
/* put the char on the canvas */
ctx.fillText(this.Chars[i].str, x, y);
}
}
}
/* char class */
class Char {
constructor(First) {
/* Save if this char is the first */
this.First = First;
/* set a random char*/
this.change();
}
update(now) {
/* if were lucky */
if (Math.floor(Math.random() * 60) == 0) {
/* change the char */
this.change();
}
}
change() {
/* pick a char between 0x30A0 and 0x30FF */
this.str = String.fromCharCode(0x30A0 + Math.floor(Math.random() * 96));
}
}
window.onresize = function () {
/* Change local With and height variables */
w = window.innerWidth;
h = window.innerHeight;
/* Change canvas With and height variables */
document.getElementById("MyCanvas").width = w;
document.getElementById("MyCanvas").height = h;
};
function step(now) {
/* requests a new frame */
window.requestAnimationFrame(step);
/* Creates a canvas that can be used to draw */
var ctx = canvas.getContext("2d");
/* Gets the current screen to make dimmer */
imgData = ctx.getImageData(0, 0, w, h);
/* for every pixel */
for (var i = 0; i < imgData.data.length; i += 4) {
/* dim the pixel's alpha channel by 2 */
imgData.data[i + 3] /= 2;
}
/* put the image back */
ctx.putImageData(imgData, 0, 0);
/* update every stream */
streams.forEach(s => {
s.update((now - last) / 1000);
});
/* Display every stream */
streams.forEach(s => {
s.display(ctx);
});
last = now;
}