-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.ts
271 lines (255 loc) · 6.44 KB
/
main.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import p5 from "p5";
import * as gcc from "gif-capture-canvas";
import * as formula from "./formula";
import { Random } from "./random";
import { range, wrap } from "./math";
import { Vector } from "./vector";
export let p: p5;
const isCapturing = false;
const seedRandom = new Random();
const random = new Random();
const variables: any = {};
let renderer;
let formulaDiv: HTMLDivElement;
let isShowingFormula = false;
function setup() {
renderer = p.createCanvas(500, 250);
renderer.canvas.style.cssText = `
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
`;
p.colorMode(p.HSB);
p.background(0);
formulaDiv = document.createElement("div");
formulaDiv.style.cssText = `
color: white;
font-family: monospace;
font-size: 12px;
width: 500px;
position: absolute;
left: 50%;
transform: translate(-50%, 0%);
`;
document.body.appendChild(formulaDiv);
const seed = loadFromUrl();
if (seed == null) {
nextFormulas();
} else {
random.setSeed(seed);
generateFormulas();
}
p.touchStarted = nextFormulas;
p.keyPressed = () => {
if (p.key !== "f") {
return;
}
isShowingFormula = !isShowingFormula;
isShowingFormula ? showFormula() : hideFormula();
};
if (isCapturing) {
gcc.setOptions({ capturingFps: 60, durationSec: 2 });
}
}
type FormulaRange = {
min: number;
max: number;
targetMin: number;
targetMax: number;
frameMin: number;
frameMax: number;
};
type Shape = {
pos: Vector;
size: Vector;
color: number[];
};
let formulas: formula.Formula[];
let formulaRanges: FormulaRange[];
let shapes: Shape[];
let t = 0;
function nextFormulas() {
const seed = seedRandom.getInt(0, 999999999);
saveAsUrl(seed);
random.setSeed(seed);
generateFormulas();
return false;
}
function generateFormulas() {
formulas = range(7).map(() => formula.generate(random));
if (random.get() < 0.5) {
formulas[1] = formula.swapSinCos(formulas[0]);
}
if (random.get() < 0.5) {
formulas[3] = formula.swapSinCos(formulas[2]);
}
formulaRanges = formulas.map(() => {
return {
min: -1,
max: 1,
targetMin: -1,
targetMax: 1,
frameMin: 1,
frameMax: -1
};
});
formulaRanges[0].targetMin = -p.width / 2;
formulaRanges[0].targetMax = p.width / 2;
formulaRanges[1].targetMin = -p.height / 2;
formulaRanges[1].targetMax = p.height / 2;
formulaRanges[2].targetMin = 1;
formulaRanges[2].targetMax = p.width / 8;
formulaRanges[3].targetMin = 1;
formulaRanges[3].targetMax = p.height / 8;
formulaRanges[4].targetMin = 0;
formulaRanges[4].targetMax = 360;
formulaRanges[5].targetMin = 50;
formulaRanges[5].targetMax = 100;
formulaRanges[6].targetMin = 50;
formulaRanges[6].targetMax = 100;
shapes = range(random.getInt(10, 100)).map(() => {
return { pos: new Vector(), size: new Vector(), color: [0, 0, 0] };
});
variables["a"] = random.getInt(2, 10);
variables["b"] = random.getInt(2, 10);
t = 0;
if (isShowingFormula) {
showFormula();
}
}
function draw() {
p.background(0);
variables["t"] = t;
shapes.forEach((s, i) => {
variables["i"] = i;
s.pos.set(
adjustFormulaValue(
formulaRanges[0],
formula.calc(formulas[0], variables)
),
adjustFormulaValue(formulaRanges[1], formula.calc(formulas[1], variables))
);
s.size.set(
adjustFormulaValue(
formulaRanges[2],
formula.calc(formulas[2], variables)
),
adjustFormulaValue(formulaRanges[3], formula.calc(formulas[3], variables))
);
s.color[0] +=
wrap(
adjustFormulaValue(
formulaRanges[4],
formula.calc(formulas[4], variables)
) - s.color[0],
-180,
180
) * 0.1;
s.color[1] +=
(adjustFormulaValue(
formulaRanges[5],
formula.calc(formulas[5], variables)
) -
s.color[1]) *
0.1;
s.color[2] +=
(adjustFormulaValue(
formulaRanges[6],
formula.calc(formulas[6], variables)
) -
s.color[2]) *
0.1;
p.stroke(wrap(s.color[0], 0, 360), s.color[1], s.color[2], 0.5);
p.strokeWeight(p.max((s.size.x + s.size.y) / 2, 1));
if (i > 0) {
const ps = shapes[i - 1];
p.line(
ps.pos.x + p.width / 2,
ps.pos.y + p.height / 2,
s.pos.x + p.width / 2,
s.pos.y + p.height / 2
);
}
});
formulaRanges.forEach(fr => {
adjustFormulaRange(fr);
});
t += 1 / 60;
if (isCapturing) {
gcc.capture(renderer.canvas);
}
}
function adjustFormulaValue(fr: FormulaRange, v: number) {
if (isNaN(v)) {
return 0;
}
if (v < fr.frameMin) {
fr.frameMin = v;
}
if (v > fr.frameMax) {
fr.frameMax = v;
}
return (
((v - fr.min) / (fr.max - fr.min)) * (fr.targetMax - fr.targetMin) +
fr.targetMin
);
}
function adjustFormulaRange(fr: FormulaRange) {
fr.min += (fr.frameMin - fr.min) * (fr.frameMin < fr.min ? 0.1 : 0.01);
fr.max += (fr.frameMax - fr.max) * (fr.frameMax > fr.max ? 0.1 : 0.01);
if (fr.max <= fr.min) {
fr.max = fr.min + 0.01;
}
fr.frameMin = fr.max;
fr.frameMax = fr.min;
}
function showFormula() {
formulaDiv.innerText = `
x = ${formula.toString(formulas[0])}
y = ${formula.toString(formulas[1])}
w = ${formula.toString(formulas[2])}
h = ${formula.toString(formulas[3])}
H = ${formula.toString(formulas[4])}
S = ${formula.toString(formulas[5])}
B = ${formula.toString(formulas[6])}
a = ${variables["a"]}
b = ${variables["b"]}
`;
}
function hideFormula() {
formulaDiv.innerText = "";
}
function saveAsUrl(seed: number) {
const baseUrl = window.location.href.split("?")[0];
let url = `${baseUrl}?s=${seed}`;
try {
window.history.replaceState({}, "", url);
} catch (e) {
console.log(e);
}
}
function loadFromUrl() {
const query = window.location.search.substring(1);
if (query == null) {
return undefined;
}
let params = query.split("&");
let seedStr: string;
for (let i = 0; i < params.length; i++) {
const param = params[i];
const pair = param.split("=");
if (pair[0] === "s") {
seedStr = pair[1];
}
}
if (seedStr == null) {
return undefined;
}
return Math.floor(Number(seedStr));
}
new p5((_p: p5) => {
p = _p;
p.setup = setup;
p.draw = draw;
});