-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.js
370 lines (302 loc) · 11.8 KB
/
frontend.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
var DIMENSIONS = [115, 80];
var MARGIN_RATIO = 10;
var CANVAS_ID = "#canv1";
// LOGIC: 0 or 1 = dead; 2 = alive
// GRAPHICS: 0 = not visible; 1 and 2 = visible
var Cellstate = {};
Cellstate.dead = 0;
Cellstate.dying = 1;
Cellstate.alive = 2;
var board = {};
board.cell_size = []; // array of x and y-size
board.margin; // array of x and y-dimensions
board.inter1; // interval
board.is_running = false;
board.context = null; // $(CANVAS_ID)[0].getContext("2d");
board.cells_alive = 0;
board.round_nr = 0;
var mouse = {};
mouse.left_button_drawing = false;
mouse.value_on_down = Cellstate.dead;
mouse.left_button_drag = false;
mouse.dragged_pattern = [];
// array with historical data about # of living cells
var histogram = {};
histogram.data1 = null;
histogram.context = null; // $("#histogram")[0].getContext("2d");
histogram.heightof = 0;
histogram.widthof = 0;
histogram.max = 0;
// **************************************************************************
// ******************************** FRONT-END *******************************
// **************************************************************************
$(document).ready(function() {
board.context = $(CANVAS_ID)[0].getContext("2d");
// setting up histogram
histogram.context = $("#histogram")[0].getContext("2d");
histogram.widthof = $('#histogram').width();
histogram.heightof = $('#histogram').height();
histogram.max = histogram.heightof;
histogram.data1 = populate_fixedqueue(size=histogram.widthof, value=0);
$('#divcanv1').show();
redraw_all();
// HANDLERS
$(CANVAS_ID).mousedown(function(event) {
// left mouse button was pressed
if(event.which === 1) {
// draw alive cell
var coords = get_coords(event);
coords = pixel_coords_to_cell_coords(coords);
mouse.value_on_down = main_grid.contents[coords[0]][coords[1]];
cell_toggle(main_grid, coords);
redraw_all();
mouse.left_button_drawing = true;
};
});
$(CANVAS_ID).mouseup(function(event) {
// left mouse button was released
if(event.which === 1) {
mouse.left_button_drawing = false;
// putting pattern if dragging
if (mouse.left_button_drag) {
var coords = get_coords(event);
var coords = pixel_coords_to_cell_coords(coords);
put_pattern(main_grid, mouse.dragged_pattern, coords);
mouse.left_button_drag = false;
ghost_grid = new Grid(DIMENSIONS, false);
redraw_all();
}
}
});
// right mouse button clicked on canvas - rotate right
$(CANVAS_ID).mousedown(function(event) {
if(event.button == 2) {
var temp_pattern = [];
for (var idx in mouse.dragged_pattern) {
var x = mouse.dragged_pattern[idx][0];
var y = mouse.dragged_pattern[idx][1];
temp_pattern[idx] = [];
temp_pattern[idx][0] = -y;
temp_pattern[idx][1] = x;
}
mouse.dragged_pattern = temp_pattern;
};
});
// mouseup anywhere but on main canvas
$(document).mouseup(function(event) {
var coords = get_coords(event);
// true if *coords* are outside of canvas
if ((coords[0] < 0) || (coords[0] > $(CANVAS_ID).width())
|| (coords[1] < 0) || (coords[1] > $(CANVAS_ID).height())) {
mouse.left_button_drag = false;
mouse.left_button_drawing = false;
ghost_grid = new Grid(DIMENSIONS, false);
redraw_all();
}
});
$(CANVAS_ID).mousemove(function(event) {
var coords = get_coords(event);
var cell_coords = pixel_coords_to_cell_coords(coords);
// dragging a pattern
if (mouse.left_button_drag) {
ghost_grid = new Grid(DIMENSIONS, false);
put_pattern(ghost_grid, mouse.dragged_pattern, cell_coords);
}
// drawing cells
else if (mouse.left_button_drawing) {
if (mouse.value_on_down != Cellstate.alive) {
cell_on(main_grid, cell_coords);
}
else if (mouse.value_on_down == Cellstate.alive) {
cell_off(main_grid, cell_coords);
};
};
redraw_all();
});
// redraws all on activate (strange issue with resizing and scrollbar)
$(window).focus(function(){
redraw_all();
});
// disabling right mouse click on canvas
$('body').on('contextmenu', CANVAS_ID, function() {
return false;
});
// setting mouse cursors
$('body').mousemove(function(event) {
if (mouse.left_button_drag) {
$(this).css('cursor', 'move');
}
else {
$(this).css('cursor', 'default');
}
});
// BUTTON EVENT HANDLERS
$("#start-stop").click(function(event) {
if (board.is_running) {
make_it_stop();
}
else {
make_it_start();
};
});
$("#step").click(function(event) {
if (!board.is_running) {
one_round();
};
make_it_stop();
});
$("#reset").click(function(event) {
make_it_stop();
main_grid = new Grid(DIMENSIONS, Cellstate.dead);
histogram.data1 = populate_fixedqueue(size=histogram.widthof, value=0);
histogram.max = histogram.heightof;
board.round_nr = 0;
redraw_all();
});
$("#pattern-pulsar").mousedown(function(event) {
if (event.which === 1) {
mouse.dragged_pattern = patterns["pulsar"];
mouse.left_button_drag = true;
}
});
$("#pattern-glider").mousedown(function() {
mouse.dragged_pattern = patterns["glider"];
mouse.left_button_drag = true;
});
$("#pattern-glider-gun").mousedown(function() {
mouse.dragged_pattern = patterns["glider-gun"];
mouse.left_button_drag = true;
});
$("#pattern-r-pentomino").mousedown(function() {
mouse.dragged_pattern = patterns["r-pentomino"];
mouse.left_button_drag = true;
});
$("#pattern-LWSS").mousedown(function() {
mouse.dragged_pattern = patterns["LWSS"];
mouse.left_button_drag = true;
});
// on window resize
$(window).resize(function() {
// calling it twice so scrollbar won't affect resizing
redraw_all();
redraw_all();
});
// HELPER FUNCTIONS
// executes one round of the simulation
function one_round() {
// simulate next generation
next_generation();
// increment round number
board.round_nr++;
// redraw canvas
redraw_all();
};
function make_it_stop() {
clearInterval(board.inter1);
board.is_running = false;
$("#start-stop").attr('value', ' go ');
};
function make_it_start() {
board.is_running = true;
board.inter1 = setInterval(one_round, 0);
$("#start-stop").attr('value', 'stop');
};
// translates canvas click coords to coords of grid
function pixel_coords_to_cell_coords(coords) {
return [Math.floor(coords[0] / board.cell_size[0]),
Math.floor(coords[1] / board.cell_size[1])];
};
// returns event coords relative to canvas
function get_coords(event) {
return [(event.clientX - ($(CANVAS_ID)).offset().left),
(event.clientY - ($(CANVAS_ID)).offset().top)];
};
// function drawing real (living, dying or dead) and ghost cells
// on canvas and calling update_counters_and_histogram()
function redraw_all() {
var canvas = $(CANVAS_ID);
var width = $(window).width() - 580;
var height = $(window).height() - 27;
board.cells_alive = 0;
resize_canvas(canvas, width, height);
for (var x = 0; x < DIMENSIONS[0]; x++) {
for (var y = 0; y < DIMENSIONS[1]; y++) {
// drawing cells
// --alive
if (main_grid.contents[x][y] == Cellstate.alive) {
var fill_style = "#FFFFFF";
board.cells_alive++;
}
// --dying
else if (main_grid.contents[x][y] == Cellstate.dying) {
var fill_style = "#6C5EB5";
}
// --dead
else { // if (main_grid.contents[x][y] == Cellstate.dead)
var fill_style = "#352879";
};
// draws a real cell (living, dying or dead)
draw_a_cell(board.context, x, y, fill_style);
// draws a drag&drop ghost-cell on top of the real ones
if (ghost_grid.contents[x][y]) {
// draw the cell itself
var fill_style = "rgba(0, 0, 0, 0.6)";
draw_a_cell(board.context, x, y, fill_style);
// draw cell's border
var line_width = 1;
var stroke_style = "#000000";
draw_cells_border(board.context, x, y, line_width, stroke_style);
};
};
};
// updating cell counter
update_counters_and_histogram();
};
function update_counters_and_histogram() {
$("#counter").html(board.cells_alive);
histogram.context.fillStyle = "#352879";
histogram.context.fillRect(0, 0, histogram.widthof, histogram.heightof);
histogram.context.fillStyle = "#6C5EB5";
for (var i = 0; i < histogram.widthof; i++) {
if (histogram.data1[i] > histogram.max) {
histogram.max = histogram.data1[i]
}
histogram.context.fillRect(i, histogram.heightof,
1, -histogram.data1[i] *
(histogram.heightof / (histogram.max)));
};
$("#generations").html(board.round_nr);
};
// draws a cell on canvas
function draw_a_cell(context, x, y, fill_style) {
context.fillStyle = fill_style;
context.fillRect(x * board.cell_size[0] + board.margin[0] / 2,
y * board.cell_size[1] + board.margin[1] / 2,
board.cell_size[0] - board.margin[0],
board.cell_size[1] - board.margin[1]);
};
// draws border of a cell
function draw_cells_border(context, x, y, line_width, stroke_style) {
context.lineWidth = line_width;
context.strokeStyle = stroke_style;
context.strokeRect(x * board.cell_size[0] + board.margin[0] / 2,
y * board.cell_size[1] + board.margin[1] / 2,
board.cell_size[0] - board.margin[0],
board.cell_size[1] - board.margin[1]);
};
function resize_canvas(canvas, width, height) {
canvas.attr("width", width);
canvas.attr("height", height);
board.cell_size = [(width / DIMENSIONS[0]),
(height / DIMENSIONS[1])];
board.margin = [board.cell_size[0] / MARGIN_RATIO,
board.cell_size[1] / MARGIN_RATIO];
};
function populate_fixedqueue(size, value) {
ret = FixedQueue(size=size, initialValues=[]);
for (var x = 0; x < size; x++) {
ret.push(value);
};
return ret;
};
});