-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvascontroller.js
462 lines (418 loc) · 14.1 KB
/
canvascontroller.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright 2018-2019 Campbell Crowley. All rights reserved.
// Author: Campbell Crowley (web@campbellcrowley.com)
(function(TraX, undefined) {
/**
* The script that control the canvases on the pages that visualize G-Force as
* well as the Orientation.
* @class Canvases
* @augments TraX
*/
(function(Canvases, undefined) {
/**
* Number of points to draw to show a line.
* @private
* @default
* @constant
* @type {number}
*/
const numAccelPoints = 15;
/**
* The delta angle between points to draw a sphere in radians.
* @private
* @default
* @constant
* @type {number}
*/
const pointAngleDelta = Math.PI / 5.0;
// DOM Elements.
let gyroCanvas;
let accelCanvas;
/**
* Canvas context for orientation sphere.
* @private
* @type {Canvas.Context2d}
*/
let gyroCtx;
/**
* Canvas context for acceleration graph.
* @private
* @type {Canvas.Context2d}
*/
let accelCtx;
/**
* Sphere being drawn in canvas.
* @private
* @type {Canvases.Sphere3d}
*/
let sphere;
/**
* Rotation where -Z is down.
* @default
* @public
* @type {{a: number, b: number, g: number}}
*/
TraX.downRotation = {a: 0, b: 0, g: 0};
/**
* Current given rotation of device.
* @default
* @public
* @type {{a: number, b: number, g: number}}
*/
Canvases.rotation = {a: 0, b: 0, g: 0};
/**
* Current acceleration being exerted on device.
* @default
* @public
* @type {{0: number, 1: number, 2: number}}
*/
Canvases.drawAccel = [0, 0, 0];
/**
* Vector where +X is forwards.
* @default
* @public
* @type {{0: number, 1: number, 2: number}}
*/
Canvases.forwardVector = [0, 1, 0];
/**
* Accumulator for resetting forward vector.
* @private
* @default
* @type {number}
*/
let forwardAcc = 0;
/**
* Calculated offset between what we think is forwards and the direction GPS
* tells us we are going.
* @default
* @public
* @type {number}
*/
Canvases.headingOffset = 0;
/**
* The current rate at which the device is rotating.
* @public
* @default
* @type {{a: number, b: number, g: number}}
*/
Canvases.rotationRate = {a: 0, b: 0, g: 0};
/**
* The distance the sphere is away from front of page. (Depth into screen)
* @default
* @private
* @type {number}
*/
const distance = 100;
/**
* Width of orientation canvas context.
* @private
* @type {number}
*/
let gyroWidth;
/**
* Height of orientation canvas context.
* @private
* @type {number}
*/
let gyroHeight;
/**
* Width of acceleration canvas context.
* @private
* @type {number}
*/
let accelWidth;
/**
* Height of acceleration canvas context.
* @private
* @type {number}
*/
let accelHeight;
/**
* Initialize Canvases
*
* @public
*/
Canvases.init = function() {
gyroCanvas = document.getElementById('realtimeGyroSphere');
accelCanvas = document.getElementById('realtimeAccelCanvas');
gyroCtx = gyroCanvas.getContext('2d');
accelCtx = accelCanvas.getContext('2d');
gyroWidth = gyroCanvas.width;
gyroHeight = gyroCanvas.height;
accelWidth = accelCanvas.width;
accelHeight = accelCanvas.height;
sphere = new Canvases.Sphere3d(50);
// Render one frame.
Canvases.renderGyro();
Canvases.renderAccel();
};
/**
* Class storing information about how to draw a sphere on the canvas.
* @class
*
* @public
* @param {number} [radius=20] The radius of the sphere.
* @property {TraX.Common.Point3d[]} point Points that make up the sphere.
* @property {string} color Color of all points in the sphere.
* @property {number} radius The radius of the sphere.
* @property {number} numberOfVertexes The number of points defining the
* sphere.
*/
Canvases.Sphere3d = function(radius) {
this.point = [];
this.color = '#FFFFFF';
this.radius = (typeof radius === 'undefined') ? 20.0 : radius;
this.radius = (typeof radius !== 'number') ? 20.0 : radius;
this.numberOfVertexes = 0;
for (let alpha = 0.0; alpha < 2.0 * Math.PI; alpha += pointAngleDelta) {
const point = new TraX.Common.Point3d();
point.x = Math.cos(alpha) * this.radius;
point.y = 0;
point.z = Math.sin(alpha) * this.radius;
this.point[this.numberOfVertexes++] = point;
}
for (let direction = 1; direction >= -1; direction -= 2) {
for (let beta = pointAngleDelta; beta < Math.PI / 2.0 - pointAngleDelta;
beta += pointAngleDelta) {
const r = Math.cos(beta) * this.radius;
const Y = Math.sin(beta) * this.radius * direction;
for (let alpha = 0; alpha < Math.PI * 2.0; alpha += pointAngleDelta) {
const point = new TraX.Common.Point3d();
point.x = Math.cos(alpha) * r;
point.y = Y;
point.z = Math.sin(alpha) * r;
this.point[this.numberOfVertexes++] = point;
}
}
}
};
/**
* Project a point onto the screen plane.
*
* @private
* @param {number} xy The X or Y scalar of the point to project.
* @param {number} z The Z scalar of the point.
* @param {number} xyOffset The offset of the X or Y scalar.
* @param {number} zOffset Offset the Z scalar.
* @param {number} distance Multiplies the X or Y scalar.
* @return {number} The projected X or Y scalar.
*/
function projection(xy, z, xyOffset, zOffset, distance) {
return (distance * xy) / (z - zOffset) + xyOffset;
}
/**
* Render the canvas showing orientation/gyro data.
*
* @param {boolean} [makeDown=false] Make the orientation sphere point down
* to world down, or screen down.
*/
Canvases.renderGyro = function(makeDown) {
gyroCtx.save();
gyroCtx.clearRect(0, 0, gyroWidth, gyroHeight);
const rotatedPoints = [];
const rotation =
[Canvases.rotation.a, Canvases.rotation.b, Canvases.rotation.g];
// TODO: Nothing differnet anymore. Remove?
const downRotationAdjusted = {
a: TraX.downRotation.a,
b: TraX.downRotation.b,
g: TraX.downRotation.g,
};
// Gyro Sphere
for (let i = 0; i < sphere.numberOfVertexes; i++) {
let point = new TraX.Common.Point3d(sphere.point[i]);
point = TraX.Common.rotateVector(point, rotation, makeDown);
rotatedPoints.push(point);
}
// Calculated down vector
for (let i = 0; i < numAccelPoints; i++) {
const percent = i / numAccelPoints;
let point =
new TraX.Common.Point3d(0, 0, -sphere.radius * percent, 'magenta');
point = TraX.Common.rotateVector(point, downRotationAdjusted, false);
point = TraX.Common.rotateVector(point, rotation, makeDown);
rotatedPoints.push(point);
}
if (!makeDown) {
// Measured down vector
for (let i = 0; i < numAccelPoints; i++) {
const percent = i / numAccelPoints;
let point =
new TraX.Common.Point3d(0, 0, -sphere.radius * percent, 'green');
point = TraX.Common.rotateVector(point, downRotationAdjusted, false);
rotatedPoints.push(point);
}
}
// Calculated forwards vector
for (let i = 0; i < numAccelPoints; i++) {
const percent = i / numAccelPoints;
let point = new TraX.Common.Point3d(
Canvases.forwardVector[0] * sphere.radius * percent,
Canvases.forwardVector[1] * sphere.radius * percent,
Canvases.forwardVector[2] * sphere.radius * percent, 'blue');
point = TraX.Common.rotateVector(point, downRotationAdjusted, true);
point = TraX.Common.rotateVector(point, rotation, makeDown);
rotatedPoints.push(point);
}
// Flip z and y axes so down is down on the screen, not into the screen.
if (makeDown) {
for (let i = 0; i < rotatedPoints.length; i++) {
// TraX.Common.rotateZ(rotatedPoints[i], Canvases.headingOffset);
const tmp = new TraX.Common.Point3d(rotatedPoints[i]);
rotatedPoints[i].y = tmp.z;
rotatedPoints[i].z = -tmp.y;
}
}
// Sort based off Z to draw things in back first.
sortPoints(rotatedPoints);
// Draw all points.
for (let i = 0; i < rotatedPoints.length; i++) {
const point = rotatedPoints[i];
const x =
projection(point.x, point.z, gyroWidth / 2.0, 100.0, distance);
const y =
projection(point.y, point.z, gyroHeight / 2.0, 100.0, distance);
if (x >= 0 && x < gyroWidth) {
if (y >= 0 && y < gyroHeight) {
if (point.z < 0) {
drawPoint(gyroCtx, x, y, 2, point.color);
} else {
drawPoint(gyroCtx, x, y, 4, point.color);
}
}
}
}
gyroCtx.restore();
};
/**
* Sort points by z-index to draw things in back first. Points are sorted
* in-place.
*
* @param {Array.<Common.Point3d>} points The points to sort.
*/
function sortPoints(points) {
points.sort(function(a, b) {
return (a.z == b.z) ? 0 : ((a.z < b.z) ? -1 : 1);
});
}
/**
* Draw data on canvas showing acceleration data.
*
* @param {boolean} [externalDevice=false] Is this device a different device
* from the one recording the data.
*/
Canvases.renderAccel = function(externalDevice) {
accelCtx.save();
accelCtx.clearRect(0, 0, accelWidth, accelHeight);
// Border
accelCtx.strokeStyle = 'rgba(0, 0, 0, 1.0)';
accelCtx.beginPath();
accelCtx.rect(
accelWidth * 0.05, accelHeight * 0.05, accelWidth * 0.90,
accelHeight * 0.90);
accelCtx.stroke();
accelCtx.closePath();
// 1G guide ring
accelCtx.strokeStyle = 'rgba(125, 125, 125, 0.6)';
accelCtx.beginPath();
accelCtx.arc(
accelWidth * 0.5, accelHeight * 0.5,
accelWidth * 0.90 * 3.0 / 4.0 / 2.0, 0, Math.PI * 2.0);
accelCtx.stroke();
accelCtx.closePath();
// 2G guide ring
accelCtx.beginPath();
accelCtx.arc(
accelWidth * 0.5, accelHeight * 0.5, accelWidth * 0.90 * 3.0 / 4.0, 0,
Math.PI * 2.0);
accelCtx.stroke();
accelCtx.closePath();
// Acceleration vector3d
let point = new TraX.Common.Point3d(
-Canvases.drawAccel[0], Canvases.drawAccel[1], Canvases.drawAccel[2]);
point = TraX.Common.rotateVector(point, Canvases.rotation, true);
let more = 0;
if (screen && screen.orientation && !externalDevice) {
more = -screen.orientation.angle || 0;
more /= 180.0 / Math.PI;
}
TraX.Common.rotateZ(point, -Canvases.rotation.a + more);
const Ax = point.x;
const Ay = point.y;
const Az = point.z + Agrav;
// Draw lateral acceleration line/points.
for (let i = 0; i < numAccelPoints; i++) {
const percent = i / numAccelPoints;
const x = accelWidth / 2.0 +
Ax / Agrav / 2.0 * 0.90 * percent * accelWidth * 3.0 / 4.0;
const y = accelHeight / 2.0 +
Ay / Agrav / 2.0 * 0.90 * percent * accelHeight * 3.0 / 4.0;
drawPoint(accelCtx, x, y, 3, 'red');
}
// Draw vertical acceleration bar.
accelCtx.fillStyle = Az < 0 ? 'rgb(255, 50, 50)' : 'red';
accelCtx.rect(
accelWidth * 0.95, accelHeight * 0.50, accelWidth * 0.05,
Az / Agrav / 2.0 * 0.90 * 3.0 / 4.0 * accelHeight);
accelCtx.fill();
accelCtx.restore();
if (!externalDevice) {
const accelMag = Math.abs(Math.hypot(Ax, Ay, Az));
const rotMag = Math.abs(
Math.hypot(
Canvases.rotationRate.a, Canvases.rotationRate.b,
Canvases.rotationRate.g));
// If device is not rotating, and acceleration is greater than 0-60mph
// in 17.8816 seconds (1.5 m/s^2), reset which direction is forwards.
if (rotMag < 0.1 && accelMag > 1.5) {
// After 50 samples (50 * 16ms = 0.8s), then update forward vector.
forwardAcc++;
if (forwardAcc > 50) {
forwardAcc = 0;
Canvases.forwardVector =
[-Ax / accelMag, -Ay / accelMag, -Az / accelMag];
}
} else {
forwardAcc = 0;
}
}
};
/**
* Draw a point on the canvas with given settings.
*
* @param {Canvas.Context2d} ctx The canvas context to draw on.
* @param {number} x The X coordinate to draw the point.
* @param {number} y The Y coordinate to draw the point.
* @param {number} size The size of the point to draw.
* @param {string} color The color to make the point.
*/
function drawPoint(ctx, x, y, size, color) {
ctx.save();
ctx.beginPath();
ctx.fillStyle = color;
ctx.arc(x, y, size, 0, 2.0 * Math.PI);
ctx.fill();
ctx.restore();
}
/**
* Take the passed in values as the current correct speed and heading, and
* reset
* our estimated velocity.
*
* @public
* @param {number} gpsSpeed The speed reported by the GPS. (m/s?)
* @param {number} gpsHeading The heading reported by the GPS. (deg cw of
* N?)
*/
Canvases.resetEstimatedVelocity = function(gpsSpeed, gpsHeading) {
// Need to be moving faster than 1m/s, otherwise the UI will go a little
// crazy.
if (gpsSpeed < 1.0) return;
const newHeading = gpsHeading / 180.0 * Math.PI;
let point = TraX.Common.rotateVector(
Canvases.forwardVector, TraX.downRotation, true);
point = TraX.Common.rotateVector(point, Canvases.rotation, false);
const estimatedHeading = Math.atan2(point.y, point.x);
Canvases.headingOffset = newHeading - estimatedHeading;
};
}(window.TraX.Canvases = window.TraX.Canvases || {}));
}(window.TraX = window.TraX || {}));