-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutator.ts
221 lines (182 loc) · 5.8 KB
/
mutator.ts
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
import { tileForAddress } from "./generator";
const chars = [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"a",
"b",
"c",
"d",
"e",
"f",
] as const;
let showAddress = true;
let addresses: string[] = [];
let bpm = 60;
let rate = 1;
let size = 300;
let paused = false;
let tileCount = 1;
let inputsHidden = false;
const randChar = () => chars[Math.floor(Math.random() * chars.length)];
function mutate() {
if (tileCount !== addresses.length) {
addresses = [];
for (let i = 0; i < 40; i++) {
const char = randChar();
for (let i = 0; i < tileCount; i++) {
if (!addresses[i]) addresses.push("");
addresses[i] = addresses[i] += char;
}
}
}
const tilesContainer = document.getElementById("tiles-container");
if (rate > 0 && !paused && tilesContainer) {
tilesContainer.innerHTML = "";
const oldAddresses = addresses;
addresses = [];
for (let i = 0; i < tileCount; i++) {
const canvas = document.createElement("div");
canvas.className = "canvas";
const address = document.createElement("div");
address.className = "address";
const tileContainer = document.createElement("div");
tileContainer.className = "tile-container";
tileContainer.appendChild(canvas);
tileContainer.appendChild(address);
tilesContainer.appendChild(tileContainer);
tilesContainer.style.width = tileCount * 33 + "vw";
tilesContainer.style.justifyContent =
tileCount > 1 ? "space-between" : "center";
const newAddress = oldAddresses[i] ? oldAddresses[i].split("") : [];
for (let i = 0; i < rate; i++) {
newAddress[Math.floor(Math.random() * 40)] = randChar();
}
addresses.push(newAddress.join(""));
}
}
const inputsElem = document.getElementById("inputs");
const showInputsElem = document.getElementById("show-inputs");
const canvases = document.getElementsByClassName("canvas");
const addressElems = document.getElementsByClassName("address");
const bpmInput = document.getElementById("bpm") as HTMLInputElement;
const bpmRange = document.getElementById("bpm-range") as HTMLInputElement;
const rateInput = document.getElementById("rate") as HTMLInputElement;
if (showInputsElem) {
showInputsElem.style.visibility = inputsHidden ? "visible" : "hidden";
}
if (inputsElem) {
inputsElem.style.visibility = inputsHidden ? "hidden" : "visible";
}
if (canvases?.length) {
for (let i = 0; i < canvases.length; i++) {
if (canvases[i]) {
canvases[i].innerHTML = tileForAddress(addresses[i])
.viewbox("0 0 300 300")
.addClass("tile")
.svg();
}
}
}
if (addressElems?.length) {
for (let i = 0; i < addressElems.length; i++) {
addressElems[i].innerHTML = showAddress ? "0x" + addresses[i] : "";
(addressElems[i] as HTMLElement).style.fontSize = size / 25 + "px";
}
}
if (bpmInput) bpmInput.value = bpm.toString();
if (bpmRange) bpmRange.value = bpm.toString();
if (rateInput) rateInput.value = rate.toString();
const svgElems = document.getElementsByClassName("tile");
if (svgElems) {
for (let i = 0; i < svgElems.length; i++) {
svgElems[i].setAttribute("width", size.toString() + "px");
svgElems[i].setAttribute("height", size.toString() + "px");
}
}
const pausePlayElem = document.getElementById("pause-play");
if (pausePlayElem) {
pausePlayElem.innerText = paused ? ">>" : "||";
}
setTimeout(function () {
mutate();
}, (60 / bpm) * 1000);
}
mutate();
function docReady(fn: VoidFunction) {
// see if DOM is already available
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
// call on next available tick
setTimeout(fn, 1);
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
docReady(() => {
const canvases = document.getElementsByClassName("canvas");
for (let i = 0; i < canvases.length; i++) {
canvases[i].addEventListener("click", () => {
showAddress = !showAddress;
});
}
document.getElementById("bpm").addEventListener("input", (e: InputEvent) => {
const val = parseInt((e.target as HTMLInputElement).value);
if (val < 12 || val > 800) return;
bpm = val;
});
document
.getElementById("bpm-range")
.addEventListener("input", (e: InputEvent) => {
const val = parseInt((e.target as HTMLInputElement).value);
if (val < 12) return;
bpm = val;
});
document.getElementById("rate").addEventListener("input", (e: InputEvent) => {
const val = parseInt((e.target as HTMLInputElement).value);
if (val < 0 || val > 40) return;
rate = val;
});
// Set all addresses to input value
document
.getElementById("address-input")
.addEventListener("input", (e: InputEvent) => {
let val = (e.target as HTMLInputElement).value;
if (val.startsWith("0x")) val = val.split("0x")[1];
if (val.length !== 40) return;
addresses = addresses.map(() => val.toLowerCase());
});
document.getElementById("decrease-size").addEventListener("click", () => {
size = size - 25;
});
document.getElementById("increase-size").addEventListener("click", () => {
size = size + 25;
});
document.getElementById("pause-play").addEventListener("click", () => {
paused = !paused;
});
document.getElementById("tiles-1").addEventListener("click", () => {
tileCount = 1;
});
document.getElementById("tiles-2").addEventListener("click", () => {
tileCount = 2;
});
document.getElementById("tiles-3").addEventListener("click", () => {
tileCount = 3;
});
document.getElementById("show-inputs").addEventListener("click", () => {
inputsHidden = false;
});
document.getElementById("hide-inputs").addEventListener("click", () => {
inputsHidden = true;
});
});