-
Notifications
You must be signed in to change notification settings - Fork 18
/
flares.js
95 lines (78 loc) · 2.04 KB
/
flares.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
/**
* flares.js
*
* TODO: REFACTOR
*/
/*
var flare_count = 16;
var current_flare = 0;
var flare_pause = 20;
var flares = Array(flare_count);
for (var i = 0; i < flare_count; i++) {
//flares[i] = new Flare([255, 255, 255], Math.floor(length/i), i, i*2);
flares[i] = new Flare();
}
*/
function Flares(color, position, amplitude, speed) {
this.color = color || [255,255,255];
this.position = position || 0;
this.amplitude = amplitude || 0;
this.speed = speed || 0;
return this;
}
Flares.prototype.step = function (buf) {
this._step();
this._set(buf);
}
Flares.prototype._step = function() {
if (this.speed < 0 && -this.speed > this.amplitude) {
this.amplitude = 0;
} else {
this.amplitude += this.speed;
if (this.amplitude > 256) {
this.amplitude = 256;
this.speed = -this.speed >> 2 + 1;
}
}
}
Flares.prototype._set = function (buf) {
buf[this.position] = this._scale(this.color);
}
Flares.prototype._scale = function(color) {
var r, g, b;
var amp = this.amplitude;
r = (color[0] * amp) >> 8;
g = (color[1] * amp) >> 8;
b = (color[2] * amp) >> 8;
return [r, g, b];
}
// Modified from original.
Flares.prototype._randomBrightness = function () {
return 255 - Math.floor((Math.random() * 200));
}
Flares.prototype._randomize = function (count) {
this.color = [this._randomBrightness(), this._randomBrightness(), this._randomBrightness()];
this.amplitude = Math.floor(Math.random() * 55) + 200;
this.position = Math.floor(Math.random() * count);
this.speed = 2 * Math.floor(Math.random() * 10) + 4;
}
Flares.prototype.animate = function() {
animation = requestAnimationFrame(flare);
// slow things down. 1 == full speed
if ((count++ % 1)) return;
if (flare_pause) {
--flare_pause;
} else {
//console.log(flares);
if(!flares[current_flare].amplitude) {
flares[current_flare]._randomize(length);
++current_flare;
if (current_flare >= flare_count) current_flare = 0;
flare_pause = Math.floor(Math.random() * 50);
}
}
for (var i = 0; i < flare_count; i++) {
flares[i].step(strip.leds);
}
strip.send();
}