-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
343 lines (326 loc) · 7.84 KB
/
main.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { Pane } from "tweakpane"
import { CCA1D } from "./1d/cca_1d"
import { CCA2D } from "./2d/cca_2d/cca_2d"
import { ConwayAutomaton } from "./2d/conway/conway"
import { gosperGliderGunPattern } from "./2d/conway/patterns/guns"
import {
beaconPattern,
blinkerPattern,
pentadecathlonPattern,
pulsarPattern,
} from "./2d/conway/patterns/oscillators"
import {
HWSSPattern,
LWSSPattern,
MWSSPattern,
gliderPattern,
} from "./2d/conway/patterns/spaceships"
import { EntropyAutomaton } from "./2d/entropy/entropy"
import { ImmigrationAutomaton } from "./2d/immigration/immigration"
import { LangtonAutomaton } from "./2d/langton/langton"
import { QuadLifeAutomaton } from "./2d/quadlife/quadlife"
let pane
let settings
let automaton
window.onload = () => {
pane = new Pane({
title: "Parameters",
expanded: true,
})
const algoSelector = pane.addBinding({ algo: "cca-2D" }, "algo", {
index: 1,
label: "Algorithm",
options: {
"1 dimension Cyclic Cellular Automaton": "cca-1D",
"2 dimensions Cyclic Cellular Automaton": "cca-2D",
"Conway's game of Life": "conway",
"Immigration game": "immigration",
"Quad-Life": "quadlife",
"Langton's ant": "langton",
"2 dimensions Entropy Automaton": "entropy",
},
})
const cca1dColorsCountBlade = pane.addBinding(
{ cca1dColorsCount: 4 },
"cca1dColorsCount",
{ label: "Number of colors", min: 3, max: 5, step: 1 },
)
const cca2dColorsCountBlade = pane.addBinding(
{ cca2dColorsCount: 8 },
"cca2dColorsCount",
{ label: "Number of colors", min: 2, max: 20, step: 1 },
)
const cca2dThresholdBlade = pane.addBinding(
{ cca2dThreshold: 2 },
"cca2dThreshold",
{ label: "Threshold", min: 1, max: 3, step: 1 },
)
const entropyColorsCountBlade = pane.addBinding(
{ entropyColorsCount: 4 },
"entropyColorsCount",
{ label: "Number of colors", min: 2, max: 20, step: 1 },
)
const resolutionBlade = pane.addBinding({ resolution: 5 }, "resolution", {
label: "Resolution",
min: 1,
max: 20,
step: 1,
})
const conwayPatterns = pane.addFolder({
title: "Add patterns",
expanded: true,
})
const oscillators = conwayPatterns.addFolder({
title: "Oscillators",
})
const addBlinkerBtn = oscillators.addButton({
title: "Add a blinker",
})
const addBeaconBtn = oscillators.addButton({
title: "Add a beacon",
})
const addPulsarBtn = oscillators.addButton({
title: "Add a pulsar",
})
const addPentadecathlonBtn = oscillators.addButton({
title: "Add a pentadecathlon",
})
const spaceships = conwayPatterns.addFolder({
title: "Spaceships",
})
const addGliderBtn = spaceships.addButton({
title: "Add a glider",
})
const addLWSSBtn = spaceships.addButton({
title: "Add a light-weight spaceship",
})
const addMWSSBtn = spaceships.addButton({
title: "Add a middle-weight spaceship",
})
const addHWSSBtn = spaceships.addButton({
title: "Add a heavy-weight spaceship",
})
const guns = conwayPatterns.addFolder({
title: "Guns",
})
const addGosperGliderGunBtn = guns.addButton({
title: "Add a Gosper Glider Gun",
})
const clearBtn = pane.addButton({
title: "Clear",
})
const resetBtn = pane.addButton({
title: "Reset",
})
const startBtn = pane.addButton({
index: 10,
title: "Start",
})
pane.addBlade({
view: "text",
label: "Version",
value: `${APP_VERSION}`,
parse: () => {},
disabled: true,
})
const blades = [
cca1dColorsCountBlade,
cca2dColorsCountBlade,
cca2dThresholdBlade,
conwayPatterns,
entropyColorsCountBlade,
resolutionBlade,
clearBtn,
]
const setCca1dBlades = () => {
for (const blade of blades) {
blade.hidden = true
}
cca1dColorsCountBlade.hidden = false
}
const setCca2dBlades = () => {
for (const blade of blades) {
blade.hidden = true
}
cca2dColorsCountBlade.hidden = false
cca2dThresholdBlade.hidden = false
resolutionBlade.hidden = false
}
const setConwayBlades = () => {
for (const blade of blades) {
blade.hidden = true
}
resolutionBlade.hidden = false
conwayPatterns.hidden = false
clearBtn.hidden = false
}
const setImmigrationBlades = () => {
for (const blade of blades) {
blade.hidden = true
}
resolutionBlade.hidden = false
}
const setQuadLifeBlades = () => {
for (const blade of blades) {
blade.hidden = true
}
resolutionBlade.hidden = false
}
const setLangtonBlades = () => {
for (const blade of blades) {
blade.hidden = true
}
resolutionBlade.hidden = false
}
const setEntropyBlades = () => {
for (const blade of blades) {
blade.hidden = true
}
entropyColorsCountBlade.hidden = false
resolutionBlade.hidden = false
}
setCca2dBlades()
reset()
algoSelector.on("change", (event) => {
switch (event.value) {
case "cca-1D":
setCca1dBlades()
break
case "cca-2D":
setCca2dBlades()
break
case "conway":
setConwayBlades()
break
case "immigration":
setImmigrationBlades()
break
case "quadlife":
setQuadLifeBlades()
break
case "langton":
setLangtonBlades()
break
case "entropy":
setEntropyBlades()
break
}
reset()
})
addBlinkerBtn.on("click", () => {
automaton.placePatternRandomly(blinkerPattern())
})
addBeaconBtn.on("click", () => {
automaton.placePatternRandomly(beaconPattern())
})
addPulsarBtn.on("click", () => {
automaton.placePatternRandomly(pulsarPattern())
})
addPentadecathlonBtn.on("click", () => {
automaton.placePatternRandomly(pentadecathlonPattern())
})
addGliderBtn.on("click", () => {
automaton.placePatternRandomly(gliderPattern())
})
addLWSSBtn.on("click", () => {
automaton.placePatternRandomly(LWSSPattern())
})
addMWSSBtn.on("click", () => {
automaton.placePatternRandomly(MWSSPattern())
})
addHWSSBtn.on("click", () => {
automaton.placePatternRandomly(HWSSPattern())
})
addGosperGliderGunBtn.on("click", () => {
automaton.placePatternRandomly(gosperGliderGunPattern())
})
clearBtn.on("click", () => {
if (automaton) {
clearInterval(automaton.renderInterval)
automaton.clear()
}
})
resetBtn.on("click", () => {
reset()
})
startBtn.on("click", () => {
clearInterval(automaton.renderInterval)
switch (settings.algo) {
case "cca-1D":
automaton.start(10)
break
case "cca-2D":
automaton.start(25, 2500)
break
case "conway":
automaton.start(25, 12000)
break
case "immigration":
automaton.start(25, 12000)
break
case "quadlife":
automaton.start(25, 12000)
break
case "langton":
automaton.start(3, 12000)
break
case "entropy":
automaton.start(25, 2500)
break
}
})
}
const reset = () => {
if (automaton) {
clearInterval(automaton.renderInterval)
}
const paneState = pane.exportState()
// Convert Tweakpane state to a clean "settings" object
settings = {}
for (const s of paneState.children) {
if (s.binding) settings[s.binding.key] = s.binding.value
}
// Add more keys/values to the "settings" object
const canvasEl = document.getElementById("canvas")
const width = window.innerWidth
const height = window.innerHeight
const resolution = settings.resolution
// Create the context
switch (settings.algo) {
case "cca-1D":
automaton = new CCA1D(canvasEl, width, height, settings.cca1dColorsCount)
break
case "cca-2D":
automaton = new CCA2D(
settings.cca2dThreshold,
canvasEl,
width,
height,
resolution,
settings.cca2dColorsCount,
)
break
case "conway":
automaton = new ConwayAutomaton(canvasEl, width, height, resolution)
break
case "immigration":
automaton = new ImmigrationAutomaton(canvasEl, width, height, resolution)
break
case "quadlife":
automaton = new QuadLifeAutomaton(canvasEl, width, height, resolution)
break
case "langton":
automaton = new LangtonAutomaton(canvasEl, width, height, resolution)
break
case "entropy":
automaton = new EntropyAutomaton(
canvasEl,
width,
height,
resolution,
settings.entropyColorsCount,
)
break
}
}
window.onresize = () => reset()