-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace-lib.js
2428 lines (2020 loc) · 364 KB
/
trace-lib.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = _interopDefault(require('react'));
var memoize = _interopDefault(require('memoize-one'));
var debounce = _interopDefault(require('debounce'));
var ReactDOM = _interopDefault(require('react-dom'));
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function (key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
}
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArray(iter) {
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance");
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
// renderable trace has vertical layout precaluated:
// overlapping measures stack vertically based on start time
var EDGETYPES_SORT_WEIGHTS = {
end: 0,
start: 1
};
function calculateTraceLayout(trace) {
// we need to find all overlapping measures, so we will create points
// representing the start and end of each measure, then sort the points by
// point time, breaking ties by putting points representing closing measures
// first (in reverse order of start time), then opening measures (in order of
var edges = trace // .slice(0, 3)
.reduce(function (edges, measure) {
// zero size measures are omitted from the trace
if (measure.duration > 0) {
edges.push({
type: 'start',
time: measure.startTime,
measure: measure
});
edges.push({
type: 'end',
time: measure.startTime + measure.duration,
measure: measure
});
}
return edges;
}, []).sort(function (a, b) {
if (a.time !== b.time) {
return a.time - b.time;
} // break ties between different types
if (a.type !== b.type) {
return EDGETYPES_SORT_WEIGHTS[a.type] - EDGETYPES_SORT_WEIGHTS[b.type];
} // break ties between same type
switch (a.type) {
case 'end':
// start time desc (so inner measures close first)
return b.measure.startTime - a.measure.startTime;
case 'start':
// end time desc (so outer measures open first)
return b.measure.startTime + b.measure.duration - (a.measure.startTime + a.measure.duration);
default:
a.type;
throw new Error('panic');
}
}); // to implement the trace layout we need to calculate the vertical offset of
// each measure as they stack up. we want to place a measure at the highest
// offset for which there isn't a currently open measure. to do so we keep an
// array of currently open measures
var renderableTrace = [];
var openStack = [];
var measuresStackIndexes = new Map();
for (var i = 0; i < edges.length; i++) {
var edge = edges[i];
switch (edge.type) {
case 'start':
var nextStackIndex = openStack.length;
renderableTrace.push({
stackIndex: nextStackIndex,
measure: edge.measure
});
measuresStackIndexes.set(edge.measure, nextStackIndex);
openStack.push(edge.measure);
break;
case 'end':
var stackIndexToRemove = measuresStackIndexes.get(edge.measure); // should never be null
if (stackIndexToRemove != null) {
openStack[stackIndexToRemove] = null;
} else {
console.log('couldnt find measure to remove', edge);
} // truncate stack
var newLength = openStack.length;
while (newLength > 0 && openStack[newLength - 1] == null) {
newLength--;
}
if (openStack.length !== newLength) {
openStack.length = newLength;
}
break;
default:
edge.type;
throw new Error('panic');
}
}
return renderableTrace;
}
function memoizeWeak(fn) {
var cache = new WeakMap();
return function (arg) {
if (!cache.has(arg)) {
cache.set(arg, fn(arg));
}
return cache.get(arg) || fn(arg);
};
}
var PX_PER_MS = 1;
var BAR_HEIGHT = 16;
var BAR_Y_GUTTER = 1;
var BAR_X_GUTTER = 1;
var MIN_ZOOM_SMALL = 0.2;
var MIN_ZOOM_LARGE = 0.01;
var LARGE = window.location.search.slice(1).includes('large');
var MIN_ZOOM = LARGE ? MIN_ZOOM_LARGE : MIN_ZOOM_SMALL; // TODO: determine from trace extents
var MAX_ZOOM = 100;
var TOOLTIP_OFFSET = 8;
function getRandomColor() {
return [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)];
}
function getLayout(state, measure, startY) {
var centerOffset = state.center;
var width = Math.max(measure.measure.duration * PX_PER_MS * state.zoom - BAR_X_GUTTER, 0);
var height = BAR_HEIGHT;
var x = (measure.measure.startTime - centerOffset) * PX_PER_MS * state.zoom + state.viewportWidth / 2;
var y = measure.stackIndex * (BAR_HEIGHT + BAR_Y_GUTTER) + startY;
return {
width: width,
height: height,
x: x,
y: y,
inView: !(x + width < 0 || state.viewportWidth < x)
};
}
var UtilsWithCache =
/*#__PURE__*/
function () {
function UtilsWithCache() {
var _this = this;
_classCallCheck(this, UtilsWithCache);
_defineProperty(this, "_getMeasureColor", memoizeWeak(function (measure) {
return getRandomColor();
}));
_defineProperty(this, "_getMeasureColorRGB", memoizeWeak(function (measure) {
var color = _this._getMeasureColor(measure);
return "rgb(".concat(color[0], ",").concat(color[1], ",").concat(color[2], ")");
}));
_defineProperty(this, "_getMeasureHoverColorRGB", memoizeWeak(function (measure) {
var color = _this._getMeasureColor(measure);
return "rgb(".concat(Math.min(color[0] + 20, 255), ",").concat(Math.min(color[1] + 20, 255), ",").concat(Math.min(color[2] + 20, 255), ")");
}));
}
_createClass(UtilsWithCache, [{
key: "_getMeasureColorRGBA",
value: function _getMeasureColorRGBA(measure, opacity) {
var color = this._getMeasureColor(measure);
return "rgba(".concat(color[0], ",").concat(color[1], ",").concat(color[2], ",").concat(opacity, ")");
}
}]);
return UtilsWithCache;
}();
var Controls =
/*#__PURE__*/
function (_React$Component) {
_inherits(Controls, _React$Component);
function Controls() {
var _getPrototypeOf2;
var _this;
_classCallCheck(this, Controls);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Controls)).call.apply(_getPrototypeOf2, [this].concat(args)));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleZoom", function (event) {
var updated = parseFloat(event.currentTarget.value);
_this.props.onChange({
zoom: updated
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleCenter", function (event) {
var updated = parseFloat(event.currentTarget.value);
_this.props.onChange({
center: updated
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleLeft", function (event) {
var updated = _this.props.center - _this.props.extents.size * 0.01 / _this.props.zoom;
_this.props.onChange({
center: updated
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleRight", function (event) {
var updated = _this.props.center + _this.props.extents.size * 0.01 / _this.props.zoom;
_this.props.onChange({
center: updated
});
});
return _this;
}
_createClass(Controls, [{
key: "render",
value: function render() {
var _this$props$extents = this.props.extents,
startOffset = _this$props$extents.startOffset,
endOffset = _this$props$extents.endOffset;
return React.createElement("div", {
style: {
height: 50
}
}, React.createElement("label", null, "Zoom"), React.createElement("input", {
type: "range",
value: this.props.zoom,
step: "0.0001",
min: "0",
max: "20",
onChange: this._handleZoom
}), React.createElement("label", null, "Center"), React.createElement("input", {
type: "range",
value: this.props.center,
step: String((endOffset - startOffset) * 0.0001),
min: String(startOffset),
max: String(endOffset),
style: {
width: 300
},
onChange: this._handleCenter
}), React.createElement("button", {
onClick: this._handleLeft
}, "-"), React.createElement("button", {
onClick: this._handleRight
}, "+"));
}
}]);
return Controls;
}(React.Component);
var DOM_DRAW_LIMIT = 500;
var DOM_DRAW_MIN_PERCENT = 0.3;
var DOMRenderer =
/*#__PURE__*/
function (_React$Component) {
_inherits(DOMRenderer, _React$Component);
function DOMRenderer() {
var _getPrototypeOf2;
var _this;
_classCallCheck(this, DOMRenderer);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(DOMRenderer)).call.apply(_getPrototypeOf2, [this].concat(args)));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_utils", new UtilsWithCache());
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleMeasureClick", function (event) {
var selected = _this.props.renderableTrace[parseInt(event.currentTarget.getAttribute('data-index'))];
if (selected) {
_this.props.onSelectionChange(selected);
}
});
return _this;
}
_createClass(DOMRenderer, [{
key: "_getContentWidth",
value: function _getContentWidth() {
var size = this.props.extents.size;
return size * PX_PER_MS * this.props.zoom;
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props$extents = this.props.extents,
startOffset = _this$props$extents.startOffset,
endOffset = _this$props$extents.endOffset; // number of measures drawn, used to cap render complexity
var drawn = 0;
return React.createElement("div", null, React.createElement("div", {
style: {
width: this.props.viewportWidth,
overflowX: null,
height: this.props.viewportHeight
}
}, React.createElement("div", {
style: {
position: 'relative',
fontSize: '10px',
whiteSpace: 'nowrap',
width: null
}
}, this.props.renderableTrace.map(function (measure, index) {
var _getLayout = getLayout(_this2.props, measure, 0),
width = _getLayout.width,
height = _getLayout.height,
x = _getLayout.x,
y = _getLayout.y,
inView = _getLayout.inView;
if (!inView) {
return null;
}
if (width < _this2.props.viewportWidth * (DOM_DRAW_MIN_PERCENT / 100)) {
return null;
}
drawn++;
if (drawn > DOM_DRAW_LIMIT) {
return null;
}
return React.createElement("div", {
key: index,
"data-index": index,
title: "".concat((measure.measure.duration || 0).toFixed(2), "ms"),
style: {
position: 'absolute',
width: width,
height: height,
overflow: 'hidden',
backgroundColor: _this2._utils._getMeasureColorRGB(measure.measure),
// transform: `translate(${x}px, ${y}px) translateZ(0)`,
left: x,
top: y,
border: _this2.props.selection == measure ? 'solid red 1px' : null
},
onClick: _this2._handleMeasureClick
}, "\xA0", measure.measure.name);
}))), React.createElement("span", null, "drawn=", drawn));
}
}]);
return DOMRenderer;
}(React.Component);
/**
* Common utilities
* @module glMatrix
*/
var ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array;
var degree = Math.PI / 180;
/**
* 4x4 Matrix<br>Format: column-major, when typed out it looks like row-major<br>The matrices are being post multiplied.
* @module mat4
*/
/**
* Creates a new identity mat4
*
* @returns {mat4} a new 4x4 matrix
*/
function create() {
var out = new ARRAY_TYPE(16);
if (ARRAY_TYPE != Float32Array) {
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
}
out[0] = 1;
out[5] = 1;
out[10] = 1;
out[15] = 1;
return out;
}
/**
* Set a mat4 to the identity matrix
*
* @param {mat4} out the receiving matrix
* @returns {mat4} out
*/
function identity(out) {
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = 1;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = 1;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
/**
* Multiplies two mat4s
*
* @param {mat4} out the receiving matrix
* @param {mat4} a the first operand
* @param {mat4} b the second operand
* @returns {mat4} out
*/
function multiply(out, a, b) {
var a00 = a[0],
a01 = a[1],
a02 = a[2],
a03 = a[3];
var a10 = a[4],
a11 = a[5],
a12 = a[6],
a13 = a[7];
var a20 = a[8],
a21 = a[9],
a22 = a[10],
a23 = a[11];
var a30 = a[12],
a31 = a[13],
a32 = a[14],
a33 = a[15]; // Cache only the current line of the second matrix
var b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3];
out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[4];
b1 = b[5];
b2 = b[6];
b3 = b[7];
out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[8];
b1 = b[9];
b2 = b[10];
b3 = b[11];
out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[12];
b1 = b[13];
b2 = b[14];
b3 = b[15];
out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
return out;
}
/**
* Translate a mat4 by the given vector
*
* @param {mat4} out the receiving matrix
* @param {mat4} a the matrix to translate
* @param {vec3} v vector to translate by
* @returns {mat4} out
*/
function translate(out, a, v) {
var x = v[0],
y = v[1],
z = v[2];
var a00, a01, a02, a03;
var a10, a11, a12, a13;
var a20, a21, a22, a23;
if (a === out) {
out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
} else {
a00 = a[0];
a01 = a[1];
a02 = a[2];
a03 = a[3];
a10 = a[4];
a11 = a[5];
a12 = a[6];
a13 = a[7];
a20 = a[8];
a21 = a[9];
a22 = a[10];
a23 = a[11];
out[0] = a00;
out[1] = a01;
out[2] = a02;
out[3] = a03;
out[4] = a10;
out[5] = a11;
out[6] = a12;
out[7] = a13;
out[8] = a20;
out[9] = a21;
out[10] = a22;
out[11] = a23;
out[12] = a00 * x + a10 * y + a20 * z + a[12];
out[13] = a01 * x + a11 * y + a21 * z + a[13];
out[14] = a02 * x + a12 * y + a22 * z + a[14];
out[15] = a03 * x + a13 * y + a23 * z + a[15];
}
return out;
}
/**
* Scales the mat4 by the dimensions in the given vec3 not using vectorization
*
* @param {mat4} out the receiving matrix
* @param {mat4} a the matrix to scale
* @param {vec3} v the vec3 to scale the matrix by
* @returns {mat4} out
**/
function scale(out, a, v) {
var x = v[0],
y = v[1],
z = v[2];
out[0] = a[0] * x;
out[1] = a[1] * x;
out[2] = a[2] * x;
out[3] = a[3] * x;
out[4] = a[4] * y;
out[5] = a[5] * y;
out[6] = a[6] * y;
out[7] = a[7] * y;
out[8] = a[8] * z;
out[9] = a[9] * z;
out[10] = a[10] * z;
out[11] = a[11] * z;
out[12] = a[12];
out[13] = a[13];
out[14] = a[14];
out[15] = a[15];
return out;
}
/**
* Generates a orthogonal projection matrix with the given bounds
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {number} left Left bound of the frustum
* @param {number} right Right bound of the frustum
* @param {number} bottom Bottom bound of the frustum
* @param {number} top Top bound of the frustum
* @param {number} near Near bound of the frustum
* @param {number} far Far bound of the frustum
* @returns {mat4} out
*/
function ortho(out, left, right, bottom, top, near, far) {
var lr = 1 / (left - right);
var bt = 1 / (bottom - top);
var nf = 1 / (near - far);
out[0] = -2 * lr;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = -2 * bt;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = 2 * nf;
out[11] = 0;
out[12] = (left + right) * lr;
out[13] = (top + bottom) * bt;
out[14] = (far + near) * nf;
out[15] = 1;
return out;
}
function getRandomColor$1() {
return [1 - Math.random() * 0.5, 1 - Math.random() * 0.7, 1 - Math.random() * 0.3, 1.0];
}
var getColorForMeasure = memoizeWeak(function (measure) {
return getRandomColor$1();
});
// just applies a transform matrix each render
//
var vsSource = "\n attribute vec4 aVertexPosition;\n attribute vec4 aVertexColor;\n\n uniform mat4 uModelViewMatrix;\n uniform mat4 uProjectionMatrix;\n\n varying lowp vec4 vColor;\n\n void main(void) {\n gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;\n vColor = aVertexColor;\n }\n ";
var fsSource = "\n varying lowp vec4 vColor;\n\n void main(void) {\n gl_FragColor = vColor;\n }\n "; //
// Initialize a shader program, so WebGL knows how to draw our data
//
function initShaderProgram(gl, vsSource, fsSource) {
var vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
var fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource); // Create the shader program
var shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram); // If creating the shader program failed, alert
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
throw new Error('Unable to initialize the shader program: ' + (gl.getProgramInfoLog(shaderProgram) || ''));
}
return shaderProgram;
} //
// creates a shader of the given type, uploads the source and
// compiles it.
//
function loadShader(gl, type, source) {
var shader = gl.createShader(type); // Send the source to the shader object
gl.shaderSource(shader, source); // Compile the shader program
gl.compileShader(shader); // See if it compiled successfully
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw new Error('An error occurred compiling the shaders: ' + (gl.getShaderInfoLog(shader) || ''));
}
return shader;
}
var SQUARE_VERTICES = 4; // square
function initBuffers(gl, state, programInfo) {
var positions = [];
var colors = [];
var positionLength = 0; // init vertices using some zoom+center that we can use as the basis for
// view transforms later
var defaultLayoutState = {
center: state.defaultCenter,
viewportWidth: state.viewportWidth,
viewportHeight: state.viewportHeight,
zoom: state.defaultZoom
};
for (var i = 0; i < state.renderableTrace.length; i++) {
var measure = state.renderableTrace[i];
var layout = getLayout(defaultLayoutState, measure, 0
/*startY*/
); // TODO: move these transformations to screen coords to transform stage
var x = layout.x / state.viewportWidth * 2 - 1;
var y = layout.y / state.viewportHeight * 2 - 1; // flip sign
var width = layout.width / state.viewportWidth * 2;
var height = layout.height / state.viewportHeight * 2;
positions.push(x, y, 1);
positions.push(x + width, y, 1);
positions.push(x, y + height, 1);
positions.push(x + width, y + height, 1);
positionLength += SQUARE_VERTICES;
var color = getColorForMeasure(measure.measure);
for (var k = 0; k < SQUARE_VERTICES; k++) {
colors.push.apply(colors, _toConsumableArray(color));
}
} // vertices that will be reused each render
var positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); // vertex colors that will be reused each render
var colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
var buffers = {
position: positionBuffer,
positionLength: positionLength,
color: colorBuffer
};
return buffers;
}
function drawScene(gl, programInfo, buffers, state) {
gl.clearColor(1, 1, 1, 1.0); // white, fully opaque
gl.clearDepth(1.0); // Clear everything
gl.enable(gl.DEPTH_TEST); // Enable depth testing
gl.depthFunc(gl.LEQUAL); // Near things obscure far things
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); // execute clear canvas
// Orthographic projection with a width/height
// ratio that matches the display size of the canvas
// and we only want to see objects between 0.1 units
// and 100 units away from the camera.
var zNear = 0.1;
var zFar = 100.0;
var projectionMatrix = create(); // orthographic projection. flip y axis
ortho(projectionMatrix, -1, 1, 1, -1, zNear, zFar); // drawing position starts as the identity point, which is the center of the
// scene
var modelViewMatrix = create(); // offset to top left
translate(modelViewMatrix, // destination matrix
modelViewMatrix, // matrix to translate
[0.0, 0.0, -6.0]); // amount to translate
// TODO: extract transformation logic to render utils
var offsetX = (state.defaultCenter - state.center) * PX_PER_MS;
if (state.defaultZoom == 0) {
throw new Error('will divide by state.defaultZoom of zero');
}
var scale$1 = state.zoom / state.defaultZoom;
translate(modelViewMatrix, // destination matrix
modelViewMatrix, // matrix to translate
[// TODO: extract transformation logic to render utils
scale$1 * offsetX / state.viewportWidth * 2, // transform to clip space coords
0.0, 0.0]);
scale(modelViewMatrix, // destination matrix
modelViewMatrix, // matrix to translate
[scale$1, 1.0, 1.0]); // Tell WebGL to use our program when drawing
gl.useProgram(programInfo.program); // Set the shader uniforms
gl.uniformMatrix4fv(programInfo.uniformLocations.projectionMatrix, false, projectionMatrix);
gl.uniformMatrix4fv(programInfo.uniformLocations.modelViewMatrix, false, modelViewMatrix); // Tell WebGL how to pull out the positions from the position
// buffer into the vertexPosition attribute
{
var numComponents = 3;
var type = gl.FLOAT;
var normalize = false;
var stride = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.position);
gl.vertexAttribPointer(programInfo.attribLocations.vertexPosition, numComponents, type, normalize, stride, 0 //offset
);
gl.enableVertexAttribArray(programInfo.attribLocations.vertexPosition);
} // Tell WebGL how to pull out the colors from the color buffer
// into the vertexColor attribute.
{
var _numComponents = 4;
var _type = gl.FLOAT;
var _normalize = false;
var _stride = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.color);
gl.vertexAttribPointer(programInfo.attribLocations.vertexColor, _numComponents, _type, _normalize, _stride, 0 //offset
);
gl.enableVertexAttribArray(programInfo.attribLocations.vertexColor);
}