-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
115 lines (94 loc) · 3.54 KB
/
app.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
import {Perf} from './perf.js';
import {PatternLoader} from './pattern-loader.js';
import {PatternSelector} from './pattern-selector.js';
import {Graphics} from './graphics.js';
import {CellManager} from './cell-manager.js';
// TODO
// * step thru files and fix errors on loading (rle parser updates?)
// * setup public and private remotes
// * web component-ize
// ** src
// ** height/width
// ** bbox padding
// ** state colors
// ** media style contorls: play, pause, speed select, step
class App {
collectionsUrl;
collectionName;
stageWidth = 800;
stageHeight = 800;
cellCountX = 400;
cellCountY = 400;
// vgun sized:
// stageWidth = 2500;
// stageHeight = 1500;
// cellCountX = 2500;
// cellCountY = 1500;
cellWidth = this.stageWidth / this.cellCountX;
cellHeight = this.stageHeight / this.cellCountY;
frameId;
constructor(collectionsUrl, collectionsName) {
this.collectionsUrl = collectionsUrl;
this.collectionName = collectionsName;
this.perf = new Perf({logEvery: 100});
this.patternLoader = new PatternLoader(this.collectionsUrl);
this.graphics = new Graphics(this.cellWidth, this.cellHeight, this.stageWidth, this.stageHeight);
this.cellManager = new CellManager(this.cellCountX, this.cellCountY, this.graphics);
}
async init() {
document.getElementById('container').appendChild(this.graphics.view);
const index = await this.patternLoader.getPatternIndex();
const collection = index.find(obj => obj.hasOwnProperty(this.collectionName));
const patterns = collection[this.collectionName];
if (patterns) {
const patternSelector = new PatternSelector(patterns);
patternSelector.render(document.getElementById('selector'));
patternSelector.addEventListener('patternselected', async function(e) {
e.detail ?
await this.loadPattern(e.detail) :
this.clear();
}.bind(this));
}
}
async loadPattern(patternFilename) {
try {
const [, patternName] = patternFilename.match(/^(.+)\.\w+$/);
const data = await this.patternLoader.getRleData(this.collectionName, patternName);
console.log(JSON.stringify(data));
this.cellManager.init(data);
this.clear();
this.graphics.render();
// console.log(cellManager.toCellsText());
this.frameId = requestAnimationFrame(this.animate.bind(this));
// setTimeout(() => requestAnimationFrame(animate), 256);
}
catch (err) {
console.error(err);
}
}
clear() {
if (this.frameId) {
cancelAnimationFrame(this.frameId);
}
this.graphics.clear();
}
animate() {
this.cellManager.nextGeneration();
this.graphics.render();
this.perf.tick();
if (this.perf.ticks === 1000000) {
this.perf.end();
return;
}
this.frameId = requestAnimationFrame(this.animate.bind(this));
// setTimeout(() => requestAnimationFrame(animate), 256);
}
}
(async() => {
// if on development port for local development, load patterns locally
const url = document.location.port === '' ?
'https://raw.githubusercontent.com/thomasdunn/cellular-automata-patterns/master' :
'node_modules/cellular-automata-patterns';
const app = new App(url, 'conwaylife');
app.init();
})();