-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
221 lines (203 loc) · 8.1 KB
/
index.html
File metadata and controls
221 lines (203 loc) · 8.1 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Matrix Code</title>
<script type="text/javascript">
var looptime = 30;
var fontsize = 15;
var changingsymbols = 1;
var changingcolor = 1;
var differentspeed = 1;
var hue = 120; // startvalue: green
var color = 'hsl(120, 100%, 45%)'; // default: green
var maxwidth = window.innerWidth;
var maxheight = window.innerHeight;
var isrunning = false;
var intval = null;
window.onload = main;
window.onresize = main;
window.onkeydown = function (e) {
if (e.keyCode == 32) // spacebar
toggle_loop();
if (e.keyCode == 'C'.charCodeAt(0))
changingcolor = !changingcolor;
if (e.keyCode == 'D'.charCodeAt(0))
differentspeed = !differentspeed;
if (e.keyCode == 'S'.charCodeAt(0))
changingsymbols = !changingsymbols;
if (e.keyCode == 'W'.charCodeAt(0)) {
looptime -= 3;
if (looptime < 3)
looptime = 3;
}
if (e.keyCode == 'Q'.charCodeAt(0))
looptime += 3;
if (isrunning && (e.keyCode == 'Q'.charCodeAt(0) || e.keyCode == 'W'.charCodeAt(0))) {
clearInterval(intval);
intval = setInterval(draw_matrix, looptime);
}
}
var codes = [];
var ypos = [];
var charcount = 0;
var columncount = 0;
var frontcanvas = null;
var backcanvas = null;
var frontcontent = null;
var backcontent = null;
function resize_canvases() {
frontcanvas.width = Math.min(window.innerWidth, maxwidth);
frontcanvas.height = Math.min(window.innerHeight, maxheight);
backcanvas.width = frontcanvas.width;
backcanvas.height = frontcanvas.height;
charcount = parseInt(frontcanvas.height / fontsize * 1.0);
columncount = parseInt(frontcanvas.width / fontsize);
}
function get_randomchar() {
// http://www.kirupa.com/forum/showthread.php?340198-The-Matrix-Movie-Fonts
// http://de.wikipedia.org/wiki/Unicodeblock_Katakana
return parseInt(Math.random() * 95 + 12448);
}
function get_randompos() {
var charcount = parseInt(frontcanvas.height / fontsize);
var startcell = parseInt(Math.random() * charcount * 2)
return parseInt(-1 * startcell * fontsize);
}
function fill_array(arr, func) {
for (i = 0; i < arr.length; i++)
arr[i] = parseInt(func());
}
function set_symbol(symbolindex, x, y) {
var symbol = String.fromCharCode(codes[symbolindex]);
frontcontent.fillText(symbol, x, y - 1);
}
function change_symbols() {
var randomcolumn = 0;
var randomrow = 0;
var randomindex = 0;
var x = 0;
var y = 0;
for (k = 0; k < charcount * 1.0; k++) {
randomcolumn = parseInt(Math.random() * columncount);
randomrow = parseInt(Math.random() * charcount);
x = randomcolumn * fontsize;
y = randomrow * fontsize;
randomindex = randomcolumn * randomrow;
codes[randomindex] = get_randomchar();
frontcontent.fillStyle = 'black';
frontcontent.globalCompositeOperation = "source-over";
frontcontent.fillRect(x, y, fontsize, fontsize);
frontcontent.fillStyle = 'white';
frontcontent.globalCompositeOperation = "destination-out";
set_symbol(randomindex, x, y + fontsize);
}
}
function draw_matrix() {
// video example: https://www.youtube.com/watch?v=8ZdpA3p9ZMY
var x = 0;
var y = 0;
var grad = null;
backcontent.clearRect(0, 0, backcanvas.width, backcanvas.height);
for (i = 0; i < columncount; i++) {
ypos[i] += fontsize * 0.5;
if (differentspeed)
if (fastcolumns[i] == 1)
ypos[i] += ((i + 1) % fontsize) * 1.5;
y = ypos[i];
if (y < 0)
continue;
x = parseInt(i * fontsize);
grad = backcontent.createLinearGradient(x, y - charcount * fontsize, x, y);
grad.addColorStop(0.0, 'hsla(0, 0%, 0%, 0)');
grad.addColorStop(0.4, color);
grad.addColorStop(1 - 4 / charcount, color);
grad.addColorStop(1 - 1 / charcount, 'white');
backcontent.fillStyle = grad;
backcontent.fillRect(x, parseInt(y - charcount * fontsize), fontsize, charcount * fontsize);
if (y > frontcanvas.height + charcount * fontsize)
if (Math.random() > 0.5)
{
y = 0;
if (fastcolumns[i] == 1)
fastcolumns[i] = 0;
var isfastcolumn = Math.random() > 0.9;
if (isfastcolumn)
fastcolumns[i] = 1;
}
ypos[i] = y;
}
if (changingsymbols)
change_symbols();
if (changingcolor) {
hue += 0.3;
hue = (hue > 360) ? 0 : hue;
color = 'hsl(' + hue + ', 100%, 40%)';
}
}
function main() {
frontcanvas = document.getElementById('frontlayer');
backcanvas = document.getElementById('backlayer');
if (frontcanvas.getContext && backcanvas.getContext) {
resize_canvases();
if (isrunning)
toggle_loop();
codes = new Array(parseInt((parseInt(4096 / fontsize) + 1) * (parseInt(2160 / fontsize) + 1)));
fill_array(codes, get_randomchar);
frontcontent = frontcanvas.getContext('2d');
frontcontent.fillStyle = 'black';
frontcontent.fillRect(0, 0, frontcanvas.width, frontcanvas.height);
frontcontent.fillStyle = 'white';
// https://developer.mozilla.org/samples/canvas-tutorial/6_1_canvas_composite.html
frontcontent.globalCompositeOperation = "destination-out";
frontcontent.font = fontsize + 'px monospace';
for (y = 0; y < frontcanvas.height; y += fontsize)
for (x = 0; x < frontcanvas.width; x += fontsize) {
index = parseInt(x / fontsize + 1) * parseInt(y / fontsize + 1);
set_symbol(index, x, y);
}
ypos = new Array(parseInt(4096 / fontsize) + 1);
fastcolumns = new Array(columncount);
fastcolumns.fill(0);
fill_array(ypos, get_randompos);
backcontent = backcanvas.getContext('2d');
toggle_loop();
}
}
function toggle_loop() {
if (isrunning)
clearInterval(intval);
else
intval = setInterval(draw_matrix, looptime);
isrunning = !isrunning;
}
</script>
<style type="text/css">
body {
overflow: hidden;
margin: 0px;
background-color: black;
}
#frontlayer,
#backlayer {
position: absolute;
top: 0;
left: 0;
}
#frontlayer {
z-index: 2;
}
#backlayer {
z-index: 1;
}
</style>
</head>
<body>
<canvas id="frontlayer">
Your browser does not support HTML5 canvas. Please update your browser or try another one.
</canvas>
<canvas id="backlayer">
Your browser does not support HTML5 canvas. Please update your browser or try another one.
</canvas>
</body>
</html>