-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
296 lines (255 loc) · 8.55 KB
/
index.html
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
<title>Acan Demo Page</title>
<script src="js/HackTimer.min.js"></script>
<script src="js/utils.js"></script>
<script src="js/grid.js"></script>
<script src="js/world.js"></script>
<script src="js/simulation-interface.js"></script>
<link rel="stylesheet" href="main.css"/>
<!-- codemirror -->
<link rel="stylesheet" href="js/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="js/codemirror/addon/hint/show-hint.css">
<script src="js/codemirror/lib/codemirror.js"></script>
<script src="js/codemirror/addon/hint/show-hint.js"></script>
<script src="js/codemirror/addon/hint/javascript-hint.js"></script>
<script src="js/codemirror/addon/comment/comment.js"></script>
<script src="js/codemirror/mode/javascript/javascript.js"></script>
<!-- /codemirror -->
<div id="overlay">
<div id="overlayPopup">
Sharable link: <br />
<input onclick="this.select()" id="urlBox" class="cody" readonly />
</div>
</div>
<canvas id="test"></canvas>
<div id="controls">
<select id="simulation"></select>
<a id="run">Run</a>
<a id="pause">Pause</a>
<a id="step">Step</a>
<a id="reset">Reset</a>
<a id="save">Save</a>
<a id="load"><< Load</a>
<label for="stepDelay">Step delay (ms)</label>
<input type="number" id="stepDelay" value="100">
<label for="iterationCount">Iteration #</label>
<input type="number" id="iterationCount" value="0" disabled>
<div id="stats"></div>
<div id="legend"></div>
</div>
<textarea id="code" class="cody"></textarea>
<script>
// init
var sims = {
'Game of Life': 'game-of-life',
'Langton\'s Ant': 'langtons-ant',
'Racist People': 'racist-people',
'Forest Fire': 'forest-fire',
'BZ Reaction': 'bz-reaction'
};
var width = 75;
var height = 75;
var world = new World(width, height);
var grid = new Grid('test', width, height);
// helpers
function getColorMapHTML() {
var rowTemplate = '<tr><td><div class="legendColor" style="background: {color}"></div></td><td>{name}</td></tr>';
var html = '<table>';
if(world.sim.legend) {
var legendData = world.sim.legend();
for(var color in legendData) {
if(!legendData.hasOwnProperty(color)) continue;
var name = legendData[color];
if(color.charAt(0) != '#') {
color = grid.colors[color];
}
html += rowTemplate.replace('{color}', color).replace('{name}', name);
}
} else {
for(var entry of grid.colorMap.entries()) {
html += rowTemplate.replace('{color}', entry[1]).replace('{name}', entry[0]);
}
}
html += '</table>';
return html;
}
function redraw() {
grid.updateData(world.getData());
document.getElementById('legend').innerHTML = getColorMapHTML();
renderStats();
}
function step() {
var res = world.step();
generationDisplay.value = world.generation;
redraw();
return res;
}
var paused = true;
var timeoutRef = null;
function startLoop() {
if(step()) { // finished
pause();
}
if(!paused) {
timeoutRef = setTimeout(startLoop, Number(stepDelayInput.value));
}
}
function getCode() {
if(editor) {
return editor.getValue();
} else {
return codeArea.value;
}
}
function loadFromCode() {
pause();
try {
var sim = eval('(' + getCode() + ')');
} catch(e) {
alert('There is an error occured while loading the code.');
}
world.setSimulation(sim);
world.reset();
grid.reset();
if(sim.colorPicker) {
grid.colorPicker = sim.colorPicker;
}
run();
}
function saveCode() {
var newUrl = window.location.origin + window.location.pathname + '#' + btoa(getCode());
window.location = newUrl;
var urlBox = document.getElementById('urlBox');
urlBox.value = 'Creating link...';
fetch('https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyC1CXR3sIpbTtiz_kDoP3TrIpOMPDGV9Bo',
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify({longUrl: newUrl})
}).then(function(res) {
return res.json();
}).then(function(data) {
urlBox.value = data.id;
});
overlay.style.display = 'block';
urlBox.select();
}
function changeSim(sim) {
fetch('./js/examples/' + sim + '.js', {
method: 'get'
}).then(function(response) {
return response.text();
}).then(function(text) {
codeArea.value = text.replace(/\t/g, ' ');
editor.setValue(codeArea.value);
loadFromCode();
});
}
function renderStats() {
var html = '<table>';
var stats = world.getStats();
for(var stat in stats) {
if(!stats.hasOwnProperty(stat)) continue;
html += '<tr><td>' + stat + '</td><td>' + stats[stat] + '</td></tr>';
}
html += '</table>';
document.getElementById('stats').innerHTML = html;
}
function hideOverlay() {
overlay.style.display = 'none';
}
// ui stuff
var stepDelayInput = document.getElementById('stepDelay');
var generationDisplay = document.getElementById('iterationCount');
var runButton = document.getElementById('run');
var pauseButton = document.getElementById('pause');
var stepButton = document.getElementById('step');
var resetButton = document.getElementById('reset');
var loadButton = document.getElementById('load');
var saveButton = document.getElementById('save');
var simSelect = document.getElementById('simulation');
var codeArea = document.getElementById('code');
var overlay = document.getElementById('overlay');
// add simulations
for(var sim in sims) {
if(!sims.hasOwnProperty(sim)) continue;
var option = document.createElement('option');
option.value = sims[sim];
option.innerText = sim;
simSelect.appendChild(option);
}
function run() {
paused = false;
runButton.style.display = 'none';
pauseButton.style.display = 'block';
startLoop();
}
function pause() {
paused = true;
runButton.style.display = 'block';
pauseButton.style.display = 'none';
clearTimeout(timeoutRef);
}
runButton.addEventListener('click', run);
pauseButton.addEventListener('click', pause);
stepButton.addEventListener('click', step);
resetButton.addEventListener('click', function() {
world.reset();
redraw();
});
loadButton.addEventListener('click', loadFromCode);
saveButton.addEventListener('click', saveCode);
overlay.addEventListener('click', function(e) {
if(e.target.id == 'overlay') {
hideOverlay();
}
});
simSelect.addEventListener('change', function(e) {
changeSim(e.target.value);
});
// initial
var editor = null;
function initEditor() {
// code mirror editor
editor = CodeMirror.fromTextArea(codeArea, {
lineNumbers: true,
dragDrop: false,
lineWrapping: true,
indentWithTabs: true,
mode: {name: "javascript", globalVars: true},
extraKeys: {
"Alt-Enter": "autocomplete",
"Alt-Space": "toggleComment"
}
});
}
window.onload = function() {
var sim = 'game-of-life';
if(window.location.hash) {
sim = decodeURIComponent(window.location.hash.substring(1));
}
var found = false;
for(var simName in sims) {
if(!sims.hasOwnProperty(simName)) continue;
if(sim == sims[simName]) {
simSelect.value = sim;
changeSim(sim);
found = true;
}
}
if(!found) {
codeArea.value = atob(sim);
loadFromCode();
}
// escape key handle
document.onkeydown = function(evt) {
evt = evt || window.event;
if(evt.keyCode == 27) {
overlay.style.display = 'none';
}
};
initEditor();
};
</script>