-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (142 loc) · 3.1 KB
/
index.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
function screen (neopixels, xDim, yDim) {
this.x = xDim || 8;
this.y = yDim || 8;
this.frame = new Buffer(xDim * yDim * 3);
this.frame.fill(0);
this.neo = neopixels;
return this;
};
function init(neopixels, xDim, yDim) {
return new screen (neopixels, xDim, yDim);
};
screen.prototype.render = function () {
this.neo.animate(this.frame.length / 3, this.frame);
};
screen.prototype.modern = function (intensity) {
intensity = intensity || 30;
for (var i = 0; i < this.frame.length; i++) {
this.frame[i] = Math.random() * intensity;
}
};
screen.prototype.party = function () {
var self = this;
self.modern();
self.render();
self.neo.once('end', function() {
self.party();
});
};
screen.prototype.setPixel = function (x, y, options, cb) {
/*
options
r - 0-255
g - 0-255
b - 0-255
o - 0-1.0
*/
var err = null;
if (x >= 0 && x <= this.x-1 && y >=0 && y <= this.y-1) {
options.o = options.o || 1;
this.frame[3 * (x + this.y * y)] =
this.frame[3 * (x + this.y * y)] * (1 - options.o) +
options.o * options.g;
this.frame[3 * (x + this.y * y) + 1] =
this.frame[3 * (x + this.y * y) + 1] * (1 - options.o) +
options.o * options.r;
this.frame[3 * (x + this.y * y) + 2] =
this.frame[3 * (x + this.y * y) + 2] * (1 - options.o) +
options.o * options.b;
} else {
err = new Error('invalid coordiantes');
}
cb && cb(err);
};
screen.prototype.hLine = function (options) {
/*
x
y
l
r
g
b
o
*/
for (var i = 0; i < Math.abs(options.l); i++) {
var pix = (options.x + (sign(options.l) * i));
if ( pix >= 0 && pix <= (this.x - 1)) {
this.setPixel(pix, options.y, options);
} else {
break;
}
}
};
screen.prototype.vLine = function (options) {
/*
x
y
l
r
g
b
o
*/
for (var i = 0; i < Math.abs(options.l); i++) {
var pix = (options.y + (sign(options.l) * i));
if ( pix >= 0 && pix <= (this.y - 1)) {
this.setPixel(options.x, pix, options);
} else {
break;
}
}
};
screen.prototype.fade = function (options) {
options.percent = options.percent || 0.05;
for (var i = 0; i < this.frame.length; i++) {
this.frame[i] = this.frame[i] * (1.0-options.percent);
}
}
screen.prototype.line = function (options) {
/*
options
r - 0-255
g - 0-255
b - 0-255
o - 0-1.0
x1 - 0-this.x
y1 - 0-this.x
x2 - 0-this.y
y2 - 0-this.y
*/
var x1 = options.x1;
var x2 = options.x2;
var y1 = options.y1;
var y2 = options.y2;
if (options.x1 > options.x2) {
x1 = options.x2;
x2 = options.x1;
y1 = options.y2;
y2 = options.y1;
}
var slope = (y2 - y1) / (x2 - x1);
var theta = Math.atan(slope);
var x0 = Math.floor(x1);
var y0 = Math.floor(y1);
var xf = Math.ciel(x2);
var yf = Math.ciel(y2);
for (var x = x0; x < xf; x++) {
for (var y = y0; y < yf; y++) {
this.setPixel(x, y, options);
}
}
}
function sign (x) {
if (isNaN(x)) {
return NaN;
} else if (x === 0) {
return x;
} else {
return (x > 0 ? 1 : -1);
}
};
exports.screen = screen;
exports.init = init;