-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
188 lines (164 loc) · 4.96 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
<html>
<head>
<script src="js/lib/dat.gui.min.js"></script>
<script src="js/core.js"></script>
<script src="js/rc4random.js"></script>
<script src="js/lib/Stats.js"></script>
<style>
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
#container, canvas{
margin: 10px;
border: 1px solid #D0D0D0;
-webkit-box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<div id="container">
<h1>Biham–Middleton–Levine traffic model</h1>
<canvas width="512" height="512" id="my_canvas"></canvas>
</div>
<script>
var stats = new Stats();
stats.setMode(0);
// Align top-left
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
document.body.appendChild( stats.domElement );
function loadVisualisation(settings) {
cancelAnimationFrame(window.visRequest);
var my_canvas = document.getElementById("my_canvas");
var ctx = my_canvas.getContext("2d");
var width = settings.width;
var height = settings.height;
my_canvas.width = width;
my_canvas.height = height;
my_canvas.style.backgroundColor = settings.backgroundColor;
main = new Array(height);
for (var i = 0; i < width; i++) {
main[i] = new Array(height);
}
var rcrand = new Rc4Random(settings.seed.toString());
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
main[x][y] = 0;
if (Math.floor(rcrand.getRandomNumber()*100)<settings.density) {
main[x][y] = Math.floor(rcrand.getRandomNumber()*2)+1;
}
}
}
var rule = new Array();
var ruleString = settings.rule.toString(3);
for (var i = 0; i < 27; i++) {
var q = ruleString.length-(27-i);
if (q < 0) {
rule.push(0);
} else {
rule.push(+ruleString.charAt(ruleString.length-(27-i)));
}
}
var engine = new BML(width, height, main, rule, {first: settings.firstColor, second: settings.secondColor});
(function animate() {
window.visRequest = requestAnimationFrame(animate);
engine.move_step(1);
engine.render(ctx, width, height, stats.begin, stats.end);
})();;
}
window.onload = function() {
var vars = getUrlVars();
var settings = {
width: 512,
height: 512,
density: parseInt(vars['d']) || 40,
seed: vars['s'] || 'Javascript',
rule: parseInt(vars['r']) || 89096871199,
firstColor: [200,0,0],
secondColor: [0,0,200],
backgroundColor: "#ffffff",
steps: 1,
fps: 60,
randomizeRule: function () {
this.rule = Math.floor(Math.random()*7625597484986);
this.resetAnimation();
},
randomizeBoth: function () {
this.density = Math.floor(Math.random()*100);
this.randomizeRule();
},
resetRule: function () {
this.rule = 89096871199;
this.resetAnimation();
},
logRule: function () {
console.log("Current rule: " + this.rule);
},
inputRule: function () {
this.rule = +(window.prompt("Enter rule")) || this.rule;
this.resetAnimation();
},
resetAnimation: function () {
loadVisualisation(this);
this.setLocation();
},
valuesFromSeed: function () {
var rcrand = new Rc4Random(this.seed.toString());
this.rule = Math.floor(rcrand.getRandomNumber()*7625597484986);
this.density = Math.floor(rcrand.getRandomNumber()*100);
this.resetAnimation();
},
setLocation: function () {
document.location.hash = "?s=" + encodeURIComponent(this.seed) + "&d="+this.density+"&r=" + this.rule;
}
}
var gui = new dat.GUI();
gui.add(settings, 'width', 0, 1024).step(1);
gui.add(settings, 'height', 0, 1024).step(1);
gui.add(settings, 'seed');
gui.add(settings, 'valuesFromSeed');
gui.add(settings, 'density', 0, 100).step(1).listen();
gui.add(settings, 'rule', 0, 7625597484986).step(1).listen();
var anifolder = gui.addFolder('Animation');
anifolder.add(settings, 'steps').step(1);
anifolder.add(settings, 'fps').step(1);
var colorfolder = gui.addFolder('Colors');
colorfolder.addColor(settings, 'firstColor');
colorfolder.addColor(settings, 'secondColor');
colorfolder.addColor(settings, 'backgroundColor');
gui.add(settings, 'randomizeRule');
gui.add(settings, 'randomizeBoth');
gui.add(settings, 'resetRule');
gui.add(settings, 'logRule');
gui.add(settings, 'inputRule');
gui.add(settings, 'resetAnimation');
loadVisualisation(settings);
function getUrlVars() {
var vars = {};
var parts = window.location.hash.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = decodeURIComponent(value);
});
return vars;
}
};
</script>
</body>
</html>