-
Notifications
You must be signed in to change notification settings - Fork 0
/
elixor.js
350 lines (303 loc) · 9.77 KB
/
elixor.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
344
345
346
347
348
349
350
var multiverse = []; // made globally visible so we can poke at it
var ECA = {};
(function() {
'use strict';
var index = 0; // keeps track of all universes ever made
var fps = 30;
var paused = false;
var godmode = false;
var defaultSize = 249; // if no query is specified
var showECA = false; // whether to show an elementary CA for comparison
var settings = getInitialSettings();
makeUniverseGrid();
setupEvents();
$('#current').val(settings.size);
$('#fps').val(fps);
updateLink();
updateMultiverse();
if (showECA)
updateECA();
function makeUniverseGrid() {
if (settings.size === undefined && Array.isArray(settings.data))
settings.size = settings.data.length;
var uc = 0;
['1l', '1r', '2l', '2r'].forEach(function(variant) {
// Set checkboxes
$('#' + variant).prop('checked', settings[variant]);
// Create the universe(s)
if (settings[variant]) {
var universe = {};
universe.size = settings.size;
universe.data = settings.data;
uc++;
setVariant(universe, variant);
if (!godmode)
universe.linebreak = uc === 3 ? true : false;
makeUniverse(universe);
}
});
if(!godmode && showECA) {
ECA.size = settings.size;
ECA.data = new Array(settings.size);
ECA.data[0]=1;
$('#universes').append('<div class="container"><canvas id="ECA" width="' + settings.size + '" height="' + settings.size + '"></canvas><br></div>');
}
}
function setVariant(universe, type) {
switch (type) {
case '1l':
universe.moveLeft = false;
universe.compareRight = false;
break;
case '1r':
universe.moveLeft = true;
universe.compareRight = true;
break;
case '2l':
universe.moveLeft = true;
universe.compareRight = false;
break;
case '2r':
universe.moveLeft = false;
universe.compareRight = true;
break;
}
}
function updateLink() {
if ($('#universeLink').length)
$('#universeLabel').unwrap();
var uri = new Uri(window.location.href);
uri.setQuery('');
['1l', '1r', '2l', '2r'].forEach(function(variant) {
if (settings[variant])
uri.addQueryParam(variant, null);
});
uri.addQueryParam('size', settings.size);
$('#universeLabel').wrap('<a href="' + uri.toString() + '" id="universeLink">');
}
function setupEvents() {
$('#prev').click(function() {
var val = Number($('#current').val());
val--;
if (val > 0) {
$('#current').val(val);
$('#current').blur();
}
});
$('#next').click(function() {
var val = Number($('#current').val());
val++;
$('#current').val(val);
$('#current').blur();
});
$('#current').blur(function() {
settings.size = Number($(this).val());
settings.data = undefined;
if (!godmode) {
cleanUp();
updateLink();
}
makeUniverseGrid();
});
$('#current').keyup(function(e) {
if (e.which == 13) {
$('#current').blur();
}
});
$('#fps').keyup(function(e) {
if (e.which == 13) {
$('#fps').blur();
}
});
$('#fps').blur(function() {
fps = Number($(this).val());
});
$('#slower').click(function() {
var val = Number($('#fps').val());
var stepsize = Math.ceil(val / 3);
if (fps > 1)
val = val - stepsize;
$('#fps').val(val);
$('#fps').blur();
});
$('#faster').click(function() {
var val = Number($('#fps').val());
var stepsize = Math.ceil(val / 3);
val = val + stepsize;
if (val > 60)
val = 60;
$('#fps').val(val);
$('#fps').blur();
});
$('#pause').click(function() {
paused = !paused;
});
$('#randomize').click(function() {
multiverse.forEach(function(universe) {
universe.data.forEach(function(ele, ind, arr) {
arr[ind] = Math.round(Math.random());
});
});
if (showECA && ECA.data.length) {
ECA.data.forEach(function(ele, ind, arr) {
arr[ind] = Math.round(Math.random());
});
}
});
$('#reboot').click(function() {
multiverse.forEach(function(universe) {
universe.data.fill(1);
});
});
$('#godmode').click(function() {
godmode = $(this).is(':checked');
cleanUp();
if (godmode)
$('#universeLabel').unwrap();
$('#current').blur();
});
$('#1l,#1r,#2l,#2r').click(function() {
settings[$(this).attr('id')] = $(this).is(':checked');
});
}
function getInitialSettings() {
var rv = {};
var uri = new Uri(window.location.search);
var queryData = uri.getQueryParamValue('data');
var querySize = Number(uri.getQueryParamValue('size'));
if (queryData !== undefined && queryData.match(/[^0-1]/g) === null) {
rv.size = queryData.length;
rv.data = queryData.split('');
} else if (querySize > 0) {
rv.size = querySize;
} else {
rv.size = defaultSize;
}
var queryECA = uri.getQueryParamValue('ECA');
if (Number(queryECA) >= 0 && Number(queryECA) <= 255) {
showECA = true;
ECA.size = rv.size;
var rule = Number(queryECA).toString(2).split('').map(function(ele) { return Number(ele); } );
while (rule.length < 8)
rule.unshift(0);
ECA.rule = rule;
}
// Which universes to show, query string has format 1l&2l, etc., without
// assignment
var vq = [uri.getQueryParamValue('1l') === null, uri.getQueryParamValue('1r') === null,
uri.getQueryParamValue('2l') === null, uri.getQueryParamValue('2r') === null
];
// By default we sow all of them
if (vq.indexOf(true) === -1)
vq = [true, true, true, true];
rv['1l'] = vq[0];
rv['1r'] = vq[1];
rv['2l'] = vq[2];
rv['2r'] = vq[3];
return rv;
}
function makeUniverse(universe) {
index++;
universe.index = index;
var size = universe.size;
var linebreak = universe.linebreak ? ' linebreak' : '';
var title = '';
if (godmode) {
var type = '';
if (!universe.moveLeft && !universe.compareRight)
type = '1L';
else if (universe.moveLeft && universe.compareRight)
type = '1R';
else if (universe.moveLeft && !universe.compareRight)
type = '2L';
else if (!universe.moveLeft && universe.compareRight)
type = '2R';
title = 'title=" Size: ' + size + ' Type: ' + type + '"';
}
$('#universes').append('<div class="container' + linebreak + '" id="container' + index + '"' + title + '><canvas id="universe' + index + '" width="' + size + '" height="' + size + '"></canvas><br></div>');
if (universe.data === undefined) {
universe.data = [];
}
multiverse.push(universe);
}
function cleanUp() {
multiverse = [];
$('.container').remove();
}
function updateECA() {
setTimeout(function() {
requestAnimationFrame(updateECA);
}, 1000 / fps);
if (paused) return;
var ctx = $('#ECA')[0].getContext('2d');
ctx.clearRect(0, 0, ECA.size, ECA.size);
var y = 0;
for (var a = 0; a < ECA.size; a++) {
ECA.prevData = ECA.data.slice(0);
for (var i = 0; i < ECA.size; i++) {
var leftNeighbor = i - 1 < 0 ? ECA.prevData[ECA.size - 1] : ECA.prevData[i - 1];
var center = ECA.prevData[i];
var rightNeighbor = i + 1 >= ECA.size ? ECA.prevData[0] : ECA.prevData[i + 1];
if (leftNeighbor === undefined)
leftNeighbor = 0;
if (center === undefined)
center = 0;
if (rightNeighbor === undefined)
rightNeighbor = 0;
if (leftNeighbor === 1 && center === 1 && rightNeighbor === 1)
ECA.data[i] = ECA.rule[0];
else if (leftNeighbor === 1 && center === 1 && rightNeighbor === 0)
ECA.data[i] = ECA.rule[1];
else if (leftNeighbor === 1 && center === 0 && rightNeighbor === 1)
ECA.data[i] = ECA.rule[2];
else if (leftNeighbor === 1 && center === 0 && rightNeighbor === 0)
ECA.data[i] = ECA.rule[3];
else if (leftNeighbor === 0 && center === 1 && rightNeighbor === 1)
ECA.data[i] = ECA.rule[4];
else if (leftNeighbor === 0 && center === 1 && rightNeighbor === 0)
ECA.data[i] = ECA.rule[5];
else if (leftNeighbor === 0 && center === 0 && rightNeighbor === 1)
ECA.data[i] = ECA.rule[6];
else if (leftNeighbor === 0 && center === 0 && rightNeighbor === 0)
ECA.data[i] = ECA.rule[7];
if (ECA.data[i])
ctx.fillRect(i, y, 1, 1);
}
y++;
}
}
function updateMultiverse() {
setTimeout(function() {
requestAnimationFrame(updateMultiverse);
}, 1000 / fps);
if (paused)
return;
multiverse.forEach(function(universe, index) {
var ctx = $('#universe' + universe.index)[0].getContext('2d');
ctx.clearRect(0, 0, universe.size, universe.size);
var y = 0;
for (var i = 0; i < universe.size; i++) {
var j = universe.moveLeft ? universe.size : 0;
var breakCondition = universe.moveLeft ? -1 : universe.size;
while (1) {
var val;
var leftNeighbor, rightNeighbor, chosenNeighbor;
leftNeighbor = j === 0 ? universe.size - 1 : j - 1;
rightNeighbor = j === universe.size - 1 ? 0 : j + 1;
chosenNeighbor = universe.compareRight ? rightNeighbor : leftNeighbor;
if (universe.data[j] === undefined)
val = 1;
else
val = universe.data[j] ^ universe.data[chosenNeighbor];
universe.data[j] = val;
if (universe.data[j])
ctx.fillRect(j, y, 1, 1);
j = universe.moveLeft ? j - 1 : j + 1;
if (j === breakCondition)
break;
}
y++;
}
});
}
}());