-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.js
More file actions
272 lines (182 loc) · 6.55 KB
/
Copy pathplotter.js
File metadata and controls
272 lines (182 loc) · 6.55 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
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
class Plotter {
constructor(x, y, w, h, _oW = 0, _oH = 0) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.oW = _oW;
this.oH = _oH;
this.rgb = [120, 120, 255];
this.workData = 0;
this.data = [];
this.autoScale = true; //Scale the Y axis to fit perfectly
this.cumulative = true; //Scale the X axis to fit every value in data
//Hard Limits for axis
this.hardLimit = 10000; //Limit on the Y axis
this.hardTime = 3600; //60secs * 60 frames in 1 sec * X min //Limit on the X axis
//Values for axis scale
this.length = 360; //360 datapoints to show, at 60fps is 6secs.
this.maxVal = 1; //Top value
this.minVal = -1; //Bottom value for autoscale
//Desired/Manual values for axis
this.desiredMax = 1;
this.desiredMin = -1;
this.desiredTime = 360;
}
setHardLimit(hLim) {
this.hardLimit = hLim;
}
setLength(lngth) {
this.desiredLength = lngth;
}
accu() {
this.cumulative ^= true;
return this;
}
autoScl() {
this.autoScale ^= true;
return this;
}
setRGB(r, g, b) {
this.rgb = [r, g, b];
return this; //Testing chainable methods.
}
setOffset(_oW, _oH) {
this.oW = oW;
this.oH = oH;
return this;
}
click(x, y) {
//Avoid any other check if cursor was not pressed inside plotter
if (this.x + this.oW > x || this.y + this.oH > y || this.x + this.w + this.oW < x || this.y + this.h + this.oH < y) {return false;}
//Accumulate button
if (x > this.x + this.oW + 60 && y > this.y + this.h + this.oH - 105 && x < this.x + this.oW + 105 && y < this.y + this.h + this.oH - 60 && x - this.x - this.oW - 60 > - y + this.y + this.oH + this.h - 60) {plotter.accu(); return true;}
//AutoScale button
if (x > this.x + this.oW + 60 && y > this.y + this.h + this.oH - 105 && x < this.x + this.oW + 105 && y < this.y + this.h + this.oH - 60 && x - this.x - this.oW - 60 <= - y + this.y + this.oH + this.h - 60) {plotter.autoScl(); return true;}
return false;
}
hover(x, y) {
//Avoid any other check if cursor was not hovered inside
if (this.x + this.oW > x || this.y + this.oH > y || this.x + this.w + this.oW < x || this.y + this.h + this.oH < y) {return false;}
return false;
}
scrll(x, y, dScrll) {
//Avoid any other check if cursor was not scrolled inside enabled zones
if (this.x + this.oW > x || this.y + this.oH > y || this.x + this.w + this.oW < x || this.y + this.h + this.oH < y) {return false;}
if (x > this.x + this.oW + 105 && x < this.x + this.oW + this.w - 20 && y > this.y + this.oH + this.h - 105 && y < this.y + this.oH + this.h - 45) {this.desired}
return false;
}
inpt(datapoint) {
this.workData = datapoint;
return this;
}
output() {
return this.workData;
}
rset() {
this.hardLimit = 1000;
this.cumulative = false;
this.data = [];
this.length = 360; //360 datapoints to show, at 60fps is 6secs.
this.maxVal = 1;
this.minVal = -1;
this.workData = 0;
return this;
}
snap() {
this.data.push(this.workData);
if (this.data.length > this.hardTime) {this.data.splice(0, this.data.length - this.hardTime);}
}
updt() {
//TODO: Add a proper responsive scale to the time axis just like the Y axis so that it doesnt snap into place, but slide accordingly.
//Prune data until desirable length is reached
if (!this.cumulative) {
let deltaTime = abs(this.data.length - this.length);
if (this.data.length >= this.length) {
this.data.splice(0, ceil(deltaTime * 0.1));
}
}
//Calculate the desired Y axis scale
if (this.autoScale) {
this.desiredMax = 1;
this.desiredMin = -1;
for (let point of this.data) {
if (point * 1.1 > this.desiredMax && point < this.hardLimit) {this.desiredMax = point * 1.1;}
if (point * 1.1 < this.desiredMin && point > -this.hardLimit) {this.desiredMin = point * 1.1;}
}
}
//Slowly bring it up or down.
let deltaMax = this.desiredMax - this.maxVal;
let deltaMin = this.desiredMin - this.minVal;
let factor = 0.2;
this.maxVal += deltaMax * factor;
this.minVal += deltaMin * factor;
return this;
}
draw() {
//Filler
noStroke();
fill(10);
rect(this.x + this.oW, this.y + this.oH, this.w, this.h);
//Display region
noStroke();
fill(255);
rect(this.x + this.oW + 105, this.y + this.oH + 20, this.w - 125, this.h - 125);
//Data line/Points
let zeroY = map(0, this.maxVal, this.minVal, this.y + this.oH + 20, this.x + this.oH + this.h - 100, true);
let dX = (this.w - 125)/(/*this.data.length*/ max(this.length, this.data.length) - 1);
noStroke();
fill(this.rgb[0], this.rgb[1], this.rgb[2]);
beginShape();
vertex(this.x + this.oW + 105, zeroY);
for (let i = 0; i < this.data.length; i++) {
let y1 = map(this.data[i], this.maxVal * 1.0, this.minVal * 1.0, this.y + this.oH + 20, this.y + this.oH + this.h - 105, true);
if (y1 == NaN) {y1 = 0;} //Avoid division by zero
vertex(this.x + this.oW + 105 + dX * i, y1);
}
vertex(this.x + this.oW + 105 + dX * (this.data.length - 1), zeroY);
endShape(CLOSE);
//Zero line
stroke(0);
strokeWeight(2);
line(this.x + this.oW + 100, zeroY, this.x + this.w + this.oW - 20, zeroY);
//Accumulate button
stroke(0);
if (!this.cumulative) {fill(255, 255, 120);}
else {fill(120, 255, 120);}
//rect(this.x + this.oW + 15, this.y + this.h + this.oH - 45, 30, 30);
beginShape();
vertex(this.x + this.oW + 59, this.y + this.h + this.oH - 59);
vertex(this.x + this.oW + 104, this.y + this.oH + this.h - 59);
vertex(this.x + this.oW + 104, this.y + this.oH + this.h - 104);
endShape(CLOSE);
//AutoScale button
stroke(0);
if (!this.autoScale) {fill(255, 255, 120);}
else {fill(120, 255, 120);}
//rect(this.x + this.oW + 60, this.y + this.h + this.oH - 90, 30, 30);
beginShape();
vertex(this.x + this.oW + 59, this.y + this.h + this.oH - 59);
vertex(this.x + this.oW + 104, this.y + this.oH + this.h - 104);
vertex(this.x + this.oW + 59, this.y + this.oH + this.h - 104);
endShape(CLOSE);
//TEST BUTTONS
fill(200,200,200);
rect(this.x + this.oW + 15, this.y + this.h + this.oH - 90, 30, 30);
rect(this.x + this.oW + 60, this.y + this.h + this.oH - 45, 30, 30);
//Scale ruler Y
//TODO:
fill(255);
strokeWeight(2);
stroke(0);
rect(this.x + this.oW + 59, this.y + this.oH + 19, 45, this.h - 123);
//Determine steps to draw
//Scale ruler Time
//TODO:
fill(255);
strokeWeight(2);
stroke(0);
rect(this.x + this.oW + 104, this.y + this.oH + this.h - 104, this.w - 123, 45);
return this;
}
}