@@ -288,66 +288,77 @@ window.onload = () => {
288
288
} )
289
289
}
290
290
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 ( )
294
295
}
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 ) {
299
301
if ( s . binding ) settings [ s . binding . key ] = s . binding . value
300
302
}
303
+ return settings as Settings
304
+ }
301
305
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 => {
307
312
const resolution : number = settings . resolution || 5
308
313
309
- // Create the context
310
314
switch ( settings . algo ) {
311
315
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 )
319
317
case "cca-2D" :
320
- automaton = new CCA2D (
318
+ return new CCA2D (
321
319
settings . cca2dThreshold ,
322
320
canvasEl ,
323
321
width ,
324
322
height ,
325
323
resolution ,
326
324
settings . cca2dColorsCount ,
327
325
)
328
- break
329
326
case "conway" :
330
- automaton = new ConwayAutomaton ( canvasEl , width , height , resolution )
331
- break
327
+ return new ConwayAutomaton ( canvasEl , width , height , resolution )
332
328
case "immigration" :
333
- automaton = new ImmigrationAutomaton ( canvasEl , width , height , resolution )
334
- break
329
+ return new ImmigrationAutomaton ( canvasEl , width , height , resolution )
335
330
case "quadlife" :
336
- automaton = new QuadLifeAutomaton ( canvasEl , width , height , resolution )
337
- break
331
+ return new QuadLifeAutomaton ( canvasEl , width , height , resolution )
338
332
case "langton" :
339
- automaton = new LangtonAutomaton ( canvasEl , width , height , resolution )
340
- break
333
+ return new LangtonAutomaton ( canvasEl , width , height , resolution )
341
334
case "entropy" :
342
- automaton = new EntropyAutomaton (
335
+ return new EntropyAutomaton (
343
336
canvasEl ,
344
337
width ,
345
338
height ,
346
339
resolution ,
347
340
settings . entropyColorsCount ,
348
341
)
349
- break
342
+ default :
343
+ throw new Error ( `Unknown algorithm: ${ settings . algo } ` )
350
344
}
351
345
}
352
346
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
+
353
364
window . onresize = ( ) : void => reset ( )
0 commit comments