-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
278 lines (256 loc) · 7.56 KB
/
controller.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
/*
Execution point after DOM has loaded
*/
function main () {
// optional parameters
let weight = getUrlParameter("weight") || "none";
let squareSize = parseInt(getUrlParameter("squareSize") || 5);
let refresh = parseInt(getUrlParameter("refresh") || 500);
// canvas element and color options
let canvas = document.getElementById("canvas");
let colors = [
"green",
"blue",
"red",
"yellow",
"orange"
];
// setup initial grid of color squares
let grid = initGrid(canvas, squareSize, colors);
draw(canvas, grid);
// every interval, update all squares and re-draw
setInterval(function () {
updateGrid(grid, weight);
draw(canvas, grid);
}, refresh);
};
/*
Updates all squares in grid with random order using updateSquare()
@param {DoubleArray} grid rectangular grid of squares
@param {Array} colors all possible colors for a square
@return null
*/
function updateGrid (grid, weight) {
let numRows = grid.length;
let numCols = grid[0].length;
let squareIndexes = shuffle(range(0, numRows * numCols));
let gridColorCounts = colorCounter(grid);
Object.keys(gridColorCounts).forEach(function (key) {
gridColorCounts[key] /= (numRows * numCols);
});
squareIndexes.forEach(function (index) {
let x = Math.floor(index / numCols);
let y = Math.floor(index % numCols);
updateSquare(grid, x, y, gridColorCounts, weight);
});
};
/*
Updates a single square color based on neighbor colors and some randomness
@param {DoubleArray} grid rectangular grid of squares
@param {Int} x row index of square in grid
@param {Int} y column index of square in grid
@param {Object} gridColorPercents percent of each color as part of grid
@param {String} weight algorithm change for protection against take over
@return null
*/
function updateSquare (grid, x, y, gridColorPercents, weight) {
let currentColor = grid[x][y].color;
// change chance of color not being changed based on algorithm
if (weight === "progressive") {
if (randomChance(100 - (gridColorPercents[currentColor] * 100), 100)) {
return;
}
}
else if (weight === "regressive") {
if (randomChance((gridColorPercents[currentColor] * 100), 100)) {
return;
}
}
else if (weight === "random") {
if (randomChance(Math.random() * 100, 100)) {
return;
}
}
// find nearby neighbors color distribution
let adjacent = colorCounter(neighbors(grid, x, y));
// find different adjacent color(s) with greatest frequency above threshold
let threshold = 3;
let replacements = [];
for (let color in adjacent) {
if (color !== currentColor) {
let colorCount = adjacent[color];
if (colorCount === threshold) {
replacements.push(color);
}
else if (colorCount > threshold) {
replacements = [color];
threshold = colorCount;
}
}
}
// square can be taken over by a new color
if (replacements.length > 0) {
// randomize a chance to not be taken over
grid[x][y].color = randomElement(replacements);
}
};
/*
Initializes grid of squares based on canvas size with random colors
@param {Canvas} canvas HTML canvas element stretched to entire page
@param {Int} size of square width and height
@param {Array} colors all possible colors for a square
@return {DoubleArray} rectangular grid of squares
*/
function initGrid (canvas, squareSize, colors) {
let grid = [];
let columns = Math.floor(canvas.width / squareSize);
let rows = Math.floor(canvas.height / squareSize);
for (let x = 0; x < rows; x++) {
grid[x] = [];
for (let y = 0; y < columns; y++) {
grid[x][y] = {
"color": randomElement(colors),
"size": squareSize
};
}
}
return grid;
};
/*
Given grid of squares, paint them on to the canvas
@param {Canvas} canvas HTML canvas element to paint on
@param {DoubleArray} grid rectangular grid of squares
@return null
*/
function draw (canvas, grid) {
let c = canvas.getContext("2d");
let xPointer = 0;
let yPointer = 0;
let size = grid[0][0].size;
for (let x = 0; x < grid.length; x++) {
xPointer = 0;
for (let y = 0; y < grid[x].length; y++) {
let s = grid[x][y];
c.fillStyle = s.color;
c.fillRect(xPointer, yPointer, size, size);
xPointer += size;
}
yPointer += size;
}
c.stroke();
};
/*
Counts instances of colors in array or double array
@param {Array} list array of squares
@param {Object} key of colors with count of occurance in list
*/
function colorCounter (list, colors) {
if (!(colors)) {
colors = {};
}
list.forEach(function (element) {
if (element instanceof Array) {
colors = colorCounter(element, colors);
}
else {
if (!(element.color in colors)) {
colors[element.color] = 0;
}
colors[element.color] += 1;
}
});
return colors;
};
/*
Generate list of all squares adjacent to single square in grid
@param {DoubleArray} grid rectangular grid of squares
@param {Int} x row index of square in grid
@return {Array} list of squares adjacent to one
*/
function neighbors (grid, x, y) {
let results = [];
let numRows = grid.length;
let numCols = grid[0].length;
for (let xOffset = -1; xOffset <= 1; xOffset++) {
for (let yOffset = -1; yOffset <= 1; yOffset++) {
if (xOffset !== 0 || yOffset !== 0) {
let xCoord = x + xOffset;
let yCoord = y + yOffset;
// handle wrapping around bounds
if (xCoord < 0) {
xCoord = numRows - 1;
}
else if (xCoord === numRows) {
xCoord = 0;
}
if (yCoord < 0) {
yCoord = numCols - 1;
}
else if (yCoord === numCols) {
yCoord = 0;
}
results.push(grid[xCoord][yCoord]);
}
}
}
return results;
};
/*
Randomly change order of list
@param {Array} list of things to re-order
@return {Array} shuffled list
*/
function shuffle (list) {
let newList = [];
while (list.length > 0) {
newList.push(list.splice(Math.random() * list.length, 1)[0]);
}
return newList;
};
/*
Generate list of integers between two values
@param {Int} start append to range starting with this value (inclusive)
@param {Int} end stop appending to range at this value (non-inclusive)
*/
function range (start, end) {
let list = [];
for (var i = start; i < end; i++) {
list.push(i);
}
return list;
};
/*
Given a chance, randomly decide if something should happen
@param {Int} percent possibility of occurance
@param {Int} total all possible range out outcomes
@param {Boolean} if the possible chance has random occured
*/
function randomChance (percent, total) {
return Math.random() * total <= percent;
};
/*
Choose a random element from a list
@param {Array} list of elements to select from
@param {Any} element which was selected
*/
function randomElement (array) {
return array[Math.floor(Math.random() * array.length)];
};
/*
Extract a URL param value
@param {String} name of URL param to find
@return {String} value of URL param by name
*/
function getUrlParameter (name) {
name = name.replace(/[[]/, "\\[").replace(/[\]]/, "\\]");
let regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
let results = regex.exec(window.location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};
/*
Initialize the application after DOM has loaded since we need to
bind with the canvas element on the page
*/
document.addEventListener("DOMContentLoaded", function (event) {
main();
});