-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconwayify.js
More file actions
107 lines (83 loc) · 3.1 KB
/
conwayify.js
File metadata and controls
107 lines (83 loc) · 3.1 KB
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
javascript:(function() {
window.GRAPH_OF_LIFE = window.GRAPH_OF_LIFE || {
interval_id: null,
};
var svg = document.getElementsByClassName('js-calendar-graph-svg')[0],
graph = svg.children[0],
columns = [].filter.call(graph.children, function(node) { return node.tagName === 'g'; }),
grid = create_grid_from_columns(columns);
hide_edge_columns(columns);
if (window.GRAPH_OF_LIFE.interval_id) {
clearInterval(window.GRAPH_OF_LIFE.interval_id);
window.GRAPH_OF_LIFE.interval_id = null;
} else {
window.GRAPH_OF_LIFE.interval_id = setInterval(life_iteration, 500);
}
function create_grid_from_columns(svg_columns) {
/* ignore first and last cols, since they might be different heights */
return [].map.call(svg_columns.slice(1, svg_columns.length - 1), function(col) {
return [].map.call(col.children, function(node) {
var is_alive = node.getAttribute('fill') !== '#ebedf0';
return new Cell(is_alive, node);
});
});
}
function hide_edge_columns(svg_columns) {
function color_white(svg_node) { svg_node.setAttribute('fill', '#ffffff'); }
[].forEach.call(svg_columns[0].children, color_white);
[].forEach.call(svg_columns[svg_columns.length-1].children, color_white);
}
function life_iteration() {
/* iterate over full grid twice, first to count neighbors, then to apply Life rules */
grid.forEach(function(column, x) {
column.forEach(function(cell, y) {
cell.living_neighbors_count = count_living_neighbors_at(x, y);
});
});
grid.forEach(function(column, x) {
column.forEach(function(cell, y) {
cell.apply_life_rules();
});
});
}
function count_living_neighbors_at(x, y) {
var neighbs = 0,
positions = [[x-1,y-1], [x-0,y-1], [x+1,y-1],
[x-1,y-0], /* ^_^ */ [x+1,y-0],
[x-1,y+1], [x-0,y+1], [x+1,y+1]];
positions.forEach(function(point) {
var x = point[0], y = point[1];
if (x < 0 ||
y < 0 ||
x > grid.length - 1 ||
y > grid[0].length - 1) return;
if (grid[x][y].alive) neighbs++;
});
return neighbs;
}
function Cell(alive, svg_el_reference) {
this.alive = alive || false;
this.svg_el_reference = svg_el_reference || null;
this.living_neighbors_count = 0;
}
Cell.prototype.set_alive = function() {
this.alive = true;
var greens = ['#d6e685', '#8cc665', '#44a340', '#1e6823'],
random_green = greens[Math.floor(Math.random() * greens.length)];
this.svg_el_reference.setAttribute('fill', random_green);
};
Cell.prototype.set_dead = function() {
this.alive = false;
this.svg_el_reference.setAttribute('fill', '#ebedf0');
};
Cell.prototype.apply_life_rules = function() {
if (this.living_neighbors_count < 2)
this.set_dead();
else if (this.alive && (this.living_neighbors_count === 2 || this.living_neighbors_count === 3))
this.set_alive();
else if (this.living_neighbors_count > 3)
this.set_dead();
else if (this.living_neighbors_count === 3)
this.set_alive();
};
})();