-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix.html
165 lines (146 loc) · 3.95 KB
/
matrix.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pretty pretty Matrix</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#target {
background: black;
height: 100vh;
width: 100vw;
z-index: -1;
position: fixed;
}
button {
background: transparent;
border: 1px solid transparent;
border-radius: 5px;
box-shadow: 0 0 5px 2px #0f0;
color: #0f0;
margin: 1em;
padding: 0.3em;
cursor: pointer;
}
#counter {
cursor: all-scroll;
position: absolute;
top: 0;
right: 0;
}
</style>
</head>
<body>
<canvas id="target"></canvas>
<div id="wrapper">
<button id="stahp">That one button</button>
<button id="counter"></button>
</div>
<script type="text/javascript">
// Are you KIDDING me with this type casting?
/** @type {HTMLCanvasElement} */
const canvas = document.querySelector('#target');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/** @type {CanvasRenderingContext2d} */
const ctx = canvas.getContext('2d');
const set = "Potatotomato";
let pointer = -1;
const getChar = () => {
pointer++;
if (pointer >= set.length) {
pointer = 0;
}
return set[pointer];
}
const drawChar = (color, x, y, char, maxWidth) => {
ctx.fillStyle = color;
ctx.fillText(char, x, y, maxWidth);
};
class Worm {
constructor(x) {
this.x = x;
this.init(Math.random());
}
// This reuses random just as a micro-optimization
init(random) {
this.y = Math.ceil(random * canvas.height) - 100;
this.fontSize = 3 + Math.ceil(random * 45);
this.chars = [];
const charCount = 2+Math.ceil(random*10);
for (let i = 0; i < charCount; i++) {
this.chars.push(getChar());
}
}
drawChars() {
ctx.font = this.fontSize + 'px monospace'; // default 10px sans-serif
ctx.beginPath();
const random = Math.random();
let y = this.y += this.fontSize;
if (y > window.innerHeight+500) {
this.init(random);
this.y = -400;
y = this.y;
}
this.chars.forEach((char, i) => {
let localY = y + this.fontSize*i;
if (i === 0) {
drawChar('#002200', this.x, localY, char, this.fontSize);
}
else if (i === 1) {
drawChar('#005500', this.x, localY, char, this.fontSize);
}
else if (i === this.chars.length-1) {
drawChar('#99FF99', this.x, localY, char, this.fontSize);
}
else {
drawChar('#00CC00', this.x, localY, char, this.fontSize);
}
});
this.chars = this.chars.slice(1);
this.chars.push(getChar());
}
};
// setup lanes
const laneSpacing = canvas.width / 10;
const halfLaneSpacing = laneSpacing / 2
const lanes = [];
for (let i = 0; i < 10; i++) {
const x = (laneSpacing * i) + halfLaneSpacing;
lanes.push({ worm: new Worm(x) });
}
let killer = 300; // I dont like true infinite loops
const animate = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
lanes.forEach((lane) => {
lane.worm.drawChars();
});
if (killer > 0) {
setTimeout(() => {
requestAnimationFrame(animate);
}, 50);
}
killer--;
document.querySelector('#counter').innerHTML = killer;
};
animate();
const btn = document.querySelector('#stahp');
let handler;
handler = () => {
if (killer > 0) {
killer = 0;
} else {
killer = 300;
animate();
}
};
btn.addEventListener('click', handler);
</script>
</body>
</html>