-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
254 lines (209 loc) · 9.28 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Game of Life Tutorial | Spicy Yoghurt</title>
<meta name="description" content="Game of Life tutorial by Spicy Yoghurt">
<meta name="author" content="Spicy Yoghurt">
</head>
<body>
<label for="scale" onchange="refresh()">Scale</label>
<input type="number" id="scale" name="scale" min="1" max="20" value="5">
<label for="speed">Speed</label>
<input type="number" id="speed" name="speed" min="50" max="2000" value="80">
<label for="type">Type</label>
<select id="type" name="type">
<option value="cell-based">Cell Based</option>
<option value="smooth-vanilla">Smooth Vanilla</option>
<option value="smooth-vanilla-stable">Smooth Vanilla Stable</option>
<option value="sigmoid">Sigmoid</option>
<option value="relu">Relu</option>
</select>
<label for="color">Color</label>
<input type="number" id="color" name="color" min="1" max="50" step="1" value="10">
<canvas id="canvas" width="1500" height="700" style="border:1px solid lightgrey;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
function refresh() {
scale = document.getElementById("scale").value;
speed = document.getElementById("speed").value;
type = document.getElementById("type").value;
numbersOfGrey = document.getElementById("color").value;
// remove timeout in gameLoopTimeout
clearTimeout(gameLoopTimeout);
new GameWorld('canvas');
}
<!-- populate following from html -->
var canvasWidth = 1500; // don't change these
var canvasHeight = 700; // don't change these
const scaleElement = document.getElementById("scale");
var scale = scaleElement.value; // change this to change the size of the cells
const speedElement = document.getElementById("speed");
var speed = speedElement.value; // change this to change the speed of the simulation
const typeElement = document.getElementById("type");
var type = typeElement.value; // this defines the method used for next generation
const colorElement = document.getElementById("color");
var numbersOfGrey = colorElement.value; // this defines number of colors used for printing the cell
scaleElement.addEventListener("change", refresh);
speedElement.addEventListener("change", refresh);
typeElement.addEventListener("change", refresh);
colorElement.addEventListener("change", refresh);
class Cell {
constructor(context, gridX, gridY) {
this.context = context;
// Store the position of this cell in the grid
this.gridX = gridX;
this.gridY = gridY;
this.width = scale;
this.height = scale;
// Make random cells alive
this.alive = Math.random();
}
draw() {
// Draw a simple square
const aliveScaled = Math.floor(this.alive * (numbersOfGrey + 1)) / numbersOfGrey;
this.context.fillStyle = `rgb(${255 * (1 - aliveScaled)}, ${255 * (1 - aliveScaled)}, ${255 * (1 - aliveScaled)})`;
this.context.fillRect(this.gridX * this.width, this.gridY * this.height, this.width, this.height);
}
}
class GameWorld {
constructor(canvasId) {
this.canvas = document.getElementById(canvasId);
this.context = this.canvas.getContext('2d');
this.gameObjects = [];
this.numColumns = canvasWidth / scale;
this.numRows = canvasHeight / scale;
this.createGrid();
// Request an animation frame for the first time
// The gameLoop() function will be called as a callback of this request
window.requestAnimationFrame(() => this.gameLoop());
}
createGrid() {
for (let y = 0; y < this.numRows; y++) {
for (let x = 0; x < this.numColumns; x++) {
this.gameObjects.push(new Cell(this.context, x, y));
}
}
}
isAlive(x, y) {
if (x < 0 || x >= this.numColumns || y < 0 || y >= this.numRows) {
return 0;
}
return this.gameObjects[this.gridToIndex(x, y)].alive;
}
gridToIndex(x, y) {
return x + (y * this.numColumns);
}
checkSurrounding() {
const sigmoid = (numAlive) => {
const a = 4;
const k = 1.1;
return 1 / (1 + Math.exp(-(numAlive - a) / k));
};
const relu = (numAlive) => {
const c = .3;
const k = 5;
return Math.min(Math.max(0, numAlive / k - c), 1);
};
const smoothVanilla = (numAlive) => {
const nothingStart = 1.1; // higher makes less cells alive
const mid = 2; // lower makes more cells alive
const aliveStop = 2.74; // higher makes more cells alive
const killFactor = 50; // higher is more aggressive, more cells will die
if (numAlive >= nothingStart && numAlive <= mid) {
return numAlive / 8;
} else if (numAlive >= mid && numAlive <= aliveStop) {
return numAlive / (2 * (aliveStop - mid)) + 1 - aliveStop / (2 * (aliveStop - mid));
} else {
return numAlive / killFactor;
}
};
const smoothVanillaStable = (numAlive, alive) => {
const nothingStart = 1.5; // higher makes less cells alive
const mid = 2.2; // lower makes more cells alive
const aliveStop = 3.5; // higher makes more cells alive
const killFactor = 150; // higher is more aggressive, more cells will die
if (numAlive >= nothingStart && numAlive <= mid) {
return alive;
} else if (numAlive >= mid && numAlive <= aliveStop) {
return numAlive / (2 * (aliveStop - mid)) + 1 - aliveStop / (2 * (aliveStop - mid));
} else {
return numAlive / killFactor;
}
};
const cellBased = (numAlive, alive) => {
const a = 1.4;
const b = 2;
const c = 3;
const killFactor = 90;
if (numAlive >= a && numAlive <= b) {
return alive;
} else if (numAlive >= b && numAlive <= c) {
return (alive + 1) / 2;
} else {
return numAlive / killFactor;
}
};
// Loop over all cells
for (let x = 0; x < this.numColumns; x++) {
for (let y = 0; y < this.numRows; y++) {
// Count the nearby population
let numAlive =
this.isAlive(x - 1, y - 1)
+ this.isAlive(x, y - 1)
+ this.isAlive(x + 1, y - 1)
+ this.isAlive(x - 1, y)
+ this.isAlive(x + 1, y)
+ this.isAlive(x - 1, y + 1)
+ this.isAlive(x, y + 1)
+ this.isAlive(x + 1, y + 1);
let centerIndex = this.gridToIndex(x, y);
const alive = this.gameObjects[centerIndex].alive;
switch (type) {
case 'sigmoid':
this.gameObjects[centerIndex].nextAlive = sigmoid(numAlive);
break;
case 'relu':
this.gameObjects[centerIndex].nextAlive = relu(numAlive);
break;
case 'smooth-vanilla':
this.gameObjects[centerIndex].nextAlive = smoothVanilla(numAlive);
break;
case 'smooth-vanilla-stable':
this.gameObjects[centerIndex].nextAlive = smoothVanillaStable(numAlive, alive);
break;
case 'cell-based':
this.gameObjects[centerIndex].nextAlive = cellBased(numAlive, alive);
break;
}
}
}
// Apply the new state to the cells
for (let i = 0; i < this.gameObjects.length; i++) {
this.gameObjects[i].alive = this.gameObjects[i].nextAlive;
}
}
gameLoop() {
// Check the surrounding of each cell
this.checkSurrounding();
// Clear the screen
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
// Draw all the gameobjects
for (let i = 0; i < this.gameObjects.length; i++) {
this.gameObjects[i].draw();
}
// The loop function has reached it's end, keep requesting new frames
gameLoopTimeout = setTimeout(() => {
window.requestAnimationFrame(() => this.gameLoop());
}, speed)
}
}
var gameLoopTimeout;
window.onload = () => {
// The page has loaded, start the game
let gameWorld = new GameWorld('canvas');
}
</script>
</body>
</html>