Skip to content

Commit 522c7d0

Browse files
refactor: factorize main.ts
1 parent 4fa67e3 commit 522c7d0

File tree

6 files changed

+69
-38
lines changed

6 files changed

+69
-38
lines changed

1d/cca_1d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ export class CCA1D {
1010
private colors: ColorObject[]
1111
private state: ColorObject[]
1212
private ctx: CanvasRenderingContext2D
13-
renderInterval: number
13+
renderInterval: NodeJS.Timer
1414

1515
constructor(
1616
canvasEl: HTMLCanvasElement,
1717
width: number,
1818
height: number,
1919
colorsCount: number,
2020
) {
21+
this.clear()
2122
this.canvasEl = canvasEl
2223
this.width = width
2324
this.height = height
@@ -37,6 +38,16 @@ export class CCA1D {
3738
}
3839
}
3940

41+
clear = (): void => {
42+
if (this.renderInterval) {
43+
clearInterval(this.renderInterval)
44+
this.renderInterval = undefined
45+
}
46+
if (this.ctx) {
47+
this.ctx.clearRect(0, 0, this.width, this.height)
48+
}
49+
}
50+
4051
start = (intervalMs: number): void => {
4152
let line = 0
4253
this.renderInterval = setInterval(() => {

2d/automaton2d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class Automaton2D {
1414
protected colors: ColorObject[]
1515
protected state: ColorObject[][]
1616
protected ctx: CanvasRenderingContext2D
17-
public renderInterval: number
17+
renderInterval: NodeJS.Timer
1818

1919
constructor(
2020
canvasEl: HTMLCanvasElement,
@@ -36,6 +36,13 @@ export class Automaton2D {
3636
}
3737

3838
clear(): void {
39+
if (this.renderInterval) {
40+
clearInterval(this.renderInterval)
41+
this.renderInterval = undefined
42+
}
43+
if (this.ctx) {
44+
this.ctx.clearRect(0, 0, this.width, this.height)
45+
}
3946
this.setUniformStateAndRender()
4047
}
4148

2d/cca_2d/cca_2d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { Automaton2D } from "../automaton2d"
55
export class CCA2D extends Automaton2D {
66
private threshold: number
77

8-
constructor(threshold: number, ...args: ConstructorParameters<typeof Automaton2D>) {
8+
constructor(
9+
threshold: number,
10+
...args: ConstructorParameters<typeof Automaton2D>
11+
) {
912
super(...args)
1013
this.threshold = threshold
1114

2d/langton/langton.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export class LangtonAutomaton extends Automaton2D {
88
private orientationY: number
99

1010
constructor(...args: ConstructorParameters<typeof Automaton2D>) {
11-
1211
super(...args)
1312
this.colorsCount = 2
1413
this.colors = pickColors(this.colorsCount)

main.ts

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -288,66 +288,77 @@ window.onload = () => {
288288
})
289289
}
290290

291-
const reset = (): void => {
292-
if (automaton) {
293-
clearInterval(automaton.renderInterval)
291+
const cleanupAutomaton = (automaton: AutomatonBase): void => {
292+
if (!automaton) return
293+
if ("clear" in automaton) {
294+
automaton.clear()
294295
}
295-
const paneState = pane.exportState()
296-
// Convert Tweakpane state to a clean "settings" object
297-
settings = {}
298-
for (const s of paneState.children) {
296+
}
297+
298+
const getSettings = (pane: Pane): Settings => {
299+
const settings = {}
300+
for (const s of pane.exportState().children) {
299301
if (s.binding) settings[s.binding.key] = s.binding.value
300302
}
303+
return settings as Settings
304+
}
301305

302-
const canvasEl: HTMLCanvasElement | null = document.getElementById(
303-
"canvas",
304-
) as HTMLCanvasElement
305-
const width: number = window.innerWidth
306-
const height: number = window.innerHeight
306+
const createAutomaton = (
307+
settings: Settings,
308+
canvasEl: HTMLCanvasElement,
309+
width: number,
310+
height: number,
311+
): AutomatonBase => {
307312
const resolution: number = settings.resolution || 5
308313

309-
// Create the context
310314
switch (settings.algo) {
311315
case "cca-1D":
312-
automaton = new CCA1D(
313-
canvasEl,
314-
width,
315-
height,
316-
settings.cca1dColorsCount || 4,
317-
)
318-
break
316+
return new CCA1D(canvasEl, width, height, settings.cca1dColorsCount || 4)
319317
case "cca-2D":
320-
automaton = new CCA2D(
318+
return new CCA2D(
321319
settings.cca2dThreshold,
322320
canvasEl,
323321
width,
324322
height,
325323
resolution,
326324
settings.cca2dColorsCount,
327325
)
328-
break
329326
case "conway":
330-
automaton = new ConwayAutomaton(canvasEl, width, height, resolution)
331-
break
327+
return new ConwayAutomaton(canvasEl, width, height, resolution)
332328
case "immigration":
333-
automaton = new ImmigrationAutomaton(canvasEl, width, height, resolution)
334-
break
329+
return new ImmigrationAutomaton(canvasEl, width, height, resolution)
335330
case "quadlife":
336-
automaton = new QuadLifeAutomaton(canvasEl, width, height, resolution)
337-
break
331+
return new QuadLifeAutomaton(canvasEl, width, height, resolution)
338332
case "langton":
339-
automaton = new LangtonAutomaton(canvasEl, width, height, resolution)
340-
break
333+
return new LangtonAutomaton(canvasEl, width, height, resolution)
341334
case "entropy":
342-
automaton = new EntropyAutomaton(
335+
return new EntropyAutomaton(
343336
canvasEl,
344337
width,
345338
height,
346339
resolution,
347340
settings.entropyColorsCount,
348341
)
349-
break
342+
default:
343+
throw new Error(`Unknown algorithm: ${settings.algo}`)
350344
}
351345
}
352346

347+
const reset = (): void => {
348+
// Cleanup existing automaton
349+
cleanupAutomaton(automaton)
350+
automaton = undefined
351+
352+
// Get new settings
353+
settings = getSettings(pane)
354+
355+
// Get canvas and dimensions
356+
const canvasEl = document.getElementById("canvas") as HTMLCanvasElement
357+
const width = window.innerWidth
358+
const height = window.innerHeight
359+
360+
// Create new automaton
361+
automaton = createAutomaton(settings, canvasEl, width, height)
362+
}
363+
353364
window.onresize = (): void => reset()

types/Automaton.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface AutomatonBase {
2-
renderInterval: number
2+
renderInterval: NodeJS.Timer
33
start: (fps: number, maxIterations?: number) => void
4-
clear: () => void
4+
clear?: () => void
55
placePatternRandomly?: (pattern: number[][]) => void
66
}

0 commit comments

Comments
 (0)