-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspirals.js
365 lines (327 loc) · 9.63 KB
/
spirals.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
/**
* The entirety of the shape game.
* @param {Canvas} newCanvas The canvas element.
* @param {number=} fps The fps to be used. Default is 60.
* @public
*/
var SpiralControl = function(newCanvas, fps) {
fps = typeof fps == 'undefined' ? 60 : fps;
// True iff debug messages are being shown.
this.debugMessages = false;
// True iff function call messages are being shown.
this.funcDebugMessages = false;
this.canvas = newCanvas;
this.context = this.canvas.getContext('2d');
this.imageData = this.context.createImageData(
this.canvas.width, this.canvas.height);
this.spirals = [];
setTimeout(function() {
this.spirals.push(new Spiral(this.canvas.width / 2, this.canvas.height / 2, 0, 1, this.randomSize()))}.bind(this), 0);
setTimeout(function() {
this.spirals.push(new Spiral(this.canvas.width / 2, this.canvas.height / 2-1, 0, -1, this.randomSize()))}.bind(this), 1000);
setTimeout(function() {
this.spirals.push(new Spiral(this.canvas.width / 2, this.canvas.height / 2, Math.PI, 1, this.randomSize()))}.bind(this), 1500);
setTimeout(function() {
this.spirals.push(new Spiral(this.canvas.width / 2, this.canvas.height / 2-1, Math.PI, -1, this.randomSize()))}.bind(this), 2000);
};
/**
* @return A random size.
*/
SpiralControl.prototype.randomSize = function() {
return Math.random() * .04 + .3;
};
/**
* The spiral shape.
* @param {number} startX The x coordinate of the root.
* @param {number} startY The y coordinate of the root.
* @param {number} startDir The direction, in radians, of the spiral.
* @param {number} startAccel The sign of the acceleration.
* @param {number} startSizeMult The size multiplier.
* @param {Array.<number>=} newColor The color of the spiral. Default is random.
*/
var Spiral = function(startX, startY, startDir, startAccel, startSizeMult, newColor) {
/**
* The coordinates of the root.
* @type {Array.<number>}
*/
this.root = [startX, startY];
/**
* The color of the spiral.
* @type {Array.<number>}
*/
this.color = typeof newColor == 'undefined' ?
SpiralControl.randomColor() : newColor;
/**
* The direction of the spiral.
* @type {number}
*/
this.dir = startDir;
/**
* The angular acceleration of the spiral.
* @type {number}
*/
this.angularAccel = startAccel * .05;
/**
* Time since the branch started.
* @type {number}
*/
this.timeSinceBranch = 0;
/**
* Size multiplier of the spiral.
*/
this.sizeMult = startSizeMult;
/**
* The speed of the spiral.
* @type {number}
*/
this.speed = 3 * this.sizeMult;
/**
* The tip of the spiral, where it grows.
* @type {Array.<number>}
*/
this.tip = [startX, startY];
/**
* The root of the branch. Null if not discovered yet.
* @type {?Array.<number>}
*/
this.branchRoot = null;
/**
* The updated coordinates that haven't been on the image yet.
* The controller needs to clear this.
* @type {Array.<Array.<number>>}
*/
this.updated = [];
/**
* True iff the spiral is stopped.
*/
this.stopped = false;
};
/**
* Updates the spiral.
*/
Spiral.prototype.update = function() {
if (this.stopped) {
return;
}
var oldTip = [this.tip[0], this.tip[1]];
this.tip[0] += this.speed * Math.cos(this.dir);
this.tip[1] += this.speed * Math.sin(this.dir);
this.updateLine(oldTip, this.tip);
this.speed = this.updateSpeed(this.speed);
console.log(this.speed);
if (this.timeSinceBranch > 25) {
if (this.branchRoot == null) {
this.branchRoot = [this.tip[0], this.tip[1], this.dir, this.angularAccel];
}
this.dir += this.angularAccel;
} else {
this.dir += this.angularAccel / 2;
this.timeSinceBranch ++;
}
};
/**
* Updates the speed.
* @param {number} x The original speed.
* @return The new speed.
*/
Spiral.prototype.updateSpeed = function(x) {
return x * .993 - .0016 * this.sizeMult;
};
/**
* Updates the line with the color.
* @param {number} p1 The first point.
* @param {number} p2 The second point.
*/
Spiral.prototype.updateLine = function(p1, p2) {
if (Math.abs(p1[1] - p2[1]) < Math.abs(p1[0] - p2[0])) {
var x1 = p1[0]; var y1 = p1[1];
var x2 = p2[0]; var y2 = p2[1];
if (p2[0] < p1[0]) {
x1 = p2[0]; y1 = p2[1];
x2 = p1[0]; y2 = p1[1];
}
var y = y1;
var xstep = .5;
var ystep = xstep * (y1 - y2) / (x1 - x2);
for (var x = x1; x < x2; x += xstep) {
y += ystep;
this.updated.push([Math.round(x), Math.round(y), this.color]);
}
} else {
var x1 = p1[0]; var y1 = p1[1];
var x2 = p2[0]; var y2 = p2[1];
if (p2[1] < p1[1]) {
x1 = p2[0]; y1 = p2[1];
x2 = p1[0]; y2 = p1[1];
}
var x = x1;
var ystep = .5;
var xstep = ystep * (x1 - x2) / (y1 - y2);
for (var y = y1; y < y2; y += ystep) {
x += xstep;
this.updated.push([Math.round(x), Math.round(y), this.color]);
}
}
console.log('updating line ' + x1 + ',' + y1 + ':' + x2 + ',' + y2);
};
/**
* Updates the spirals.
*/
SpiralControl.prototype.update = function() {
this.spirals.forEach(function(spiral, index, spirals) {
if (!spiral.stopped) {
spiral.update();
this.absorbUpdates(spiral);
this.decorateCanvas();
if (spiral.speed < 0) {
spiral.stopped = true;;
}
}
if (spiral.branchRoot != null) {
if (Math.random() < .001) {
console.log('what a chance!');
this.spirals.push(new Spiral(spiral.branchRoot[0], spiral.branchRoot[1], spiral.branchRoot[2],
-1 * SpiralControl.sign(spiral.branchRoot[3]), this.randomSize()));
spiral.branchRoot = null;
}
}
}.bind(this));
};
/**
* Takes the recent updates from the spiral into the image data.
* @param {Spiral} spiral The spiral to be taken from.
*/
SpiralControl.prototype.absorbUpdates = function(spiral) {
while (spiral.updated.length > 0) {
// Each update is [x, y, [r, g, b]]
var u = spiral.updated.pop();
if (this.inCanvas(u[0], u[1])) {
this.setPixel(u[0], u[1], u[2][0], u[2][1], u[2][2]);
}
}
};
/**
* Decorates the canvas with pretty.
*/
SpiralControl.prototype.decorateCanvas = function() {
this.context.putImageData(this.imageData, 0, 0); // at coords 0,0
};
/**
* Prints out a debug message to the console.
* @param {string} message The debug message.
*/
SpiralControl.prototype.debug = function(message) {
if (this.debugMessage) {
console.log('[SpiralControl]: ' + message);
}
};
/**
* Prints out that a function is being called to the console.
* @param {string} message The function name.
*/
SpiralControl.prototype.funcDebug = function(message) {
if (this.funcDebugMessages) {
console.log('[SpiralControl]: Function ' + message + ' was called.');
}
};
/**
* Prints out a warning message to the console.
* @param {string} message The warning message.
*/
SpiralControl.prototype.warning = function(message) {
console.log('[SpiralControl]: (WARNING) ' + message);
};
/**
* Sets pixel pos to r, g, b, alpha.
* @param {!number} x The x position of the pixel.
* @param {!number} y The y position of the pixel.
* @param {number} r The red value.
* @param {number} g The green value.
* @param {number} b The blue value.
* @param {number=} alpha The opaque value. Default is 255.
*/
SpiralControl.prototype.setPixel = function(x, y, r, g, b, alpha) {
alpha = alpha != undefined ? alpha : 255;
position = this.canvas.width * y + x;
this.imageData.data[position*4] = r;
this.imageData.data[position*4+1] = g;
this.imageData.data[position*4+2] = b;
this.imageData.data[position*4+3] = alpha;
};
/**
* Starts the refresh loop.
* @param {function()=} functionToRun The function to run.
*/
SpiralControl.prototype.start = function(functionToRun) {
functionToRun = typeof functionToRun == 'undefined' ?
(function() {
this.update();
}).bind(this) : functionToRun;
this.refreshInterval = setInterval(functionToRun, this.mspf);
};
/**
* Stops the refresh loop.
*/
SpiralControl.prototype.stop = function() {
clearInterval(this.refreshInterval);
this.refreshInterval = null;
};
/**
* Returns if the point is inside the canvas.
* @param {!number} x The x coordinate.
* @param {!number} y The y coordinate.
* @return {!boolean} True if the point is inside the canvas, false otherwise.
* @public
*/
SpiralControl.prototype.inCanvas = function(x, y) {
return this.inXRange(x) && this.inYRange(y);
};
/**
* Returns if the number is in the range.
* @param {!number} num The number to be tested.
* @param {!number} end1 The first end of the range, inclusive.
* @param {!number} end2 The second end of the range, exclusive.
* @public
*/
SpiralControl.inRange = function(num, end1, end2) {
return end1 <= num && num < end2;
};
/**
* Returns if the number is in the x range, inclusive.
* @param {!number} num The number to be tested.
* @public
*/
SpiralControl.prototype.inXRange = function(num) {
return SpiralControl.inRange(num, 0, this.canvas.width);
};
/**
* Returns if the number is in the y range, inclusive.
* @param {!number} num The number to be tested.
* @public
*/
SpiralControl.prototype.inYRange = function(num) {
return SpiralControl.inRange(num, 0, this.canvas.height);
};
/**
* Returns a random color.
* @return {!Array.<number>} An array of length 3 with values between 0 and 256.
* @public
*/
SpiralControl.randomColor = function() {
return [Math.floor(Math.random()*256), Math.floor(Math.random()*256),
Math.floor(Math.random()*256)];
};
/**
* Returns the sign of x.
* @param {number} x The number to be signed.
* @return The sign of x.
*/
SpiralControl.sign = function(x) {
if (x > 0) {
return 1;
} else if (x < 0) {
return -1;
} else {
return 0;
}
};