-
Notifications
You must be signed in to change notification settings - Fork 27
/
sigplot.js
9213 lines (8398 loc) · 343 KB
/
sigplot.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
/**
* @license
* File: sigplot.js
* Copyright (c) 2012-2017, LGS Innovations Inc., All rights reserved.
*
* This file is part of SigPlot.
*
* Licensed to the LGS Innovations (LGS) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. LGS licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*jslint nomen: true, browser: true, devel: true */
/* global module */
/* global require */
(function() {
var version = "version-PLACEHOLDER";
var _ = require("underscore");
var Spinner = require("spin");
var common = require("./common");
var sigfile = require("sigfile");
var bluefile = sigfile.bluefile;
var matfile = sigfile.matfile;
var m = require("./m");
var mx = require("./mx");
var Layer1D = require("./sigplot.layer1d");
var Layer2D = require("./sigplot.layer2d");
function sigplot(element, options) {
if (!(this instanceof sigplot)) {
return new sigplot.Plot(element, options);
}
}
sigplot.bluefile = bluefile;
sigplot.matfile = matfile;
sigplot.m = m;
sigplot.mx = mx;
sigplot.Layer1D = Layer1D;
sigplot.Layer2D = Layer2D;
sigplot.version = version;
/**
* Text of the keypress help dialog.
*
* @memberOf sigplot
* @private
*/
var KEYPRESS_HELP = "Keypress Table:\n" +
"--------------\n" +
"? - Main help box.\n" +
"A - Toggle display x,y readouts:\n" +
" (absc) -> (index) -> (1/absc) -> (time).\n" +
"B - Toggle LM Drag Mode:\n" +
" (box) -> (horizontal) -> (vertical).\n" +
"C - Toggle controls.\n" +
"K - Show Marker.\n" +
"L - Toggle legend.\n" +
"M - Pops up main menu\n" +
"R - Toggle display specs (x/y readout)\n" +
"S - Toggle display specs and axes.\n" +
"T - Popup box with timecode value at mouse.\n" +
"X - In 1D mode, popup box with X value at mouse.\n" +
" - In 2D mode, toggle x-cut display.\n" +
"Y - In 1D mode, popup box with Y value at mouse.\n" +
" - In 2D mode, toggle y-cut display.\n" +
"P - In 2D mode, displays p-cuts along side and bottom.\n" +
"F - Toggle fullscreen.\n" +
"Cntrl+I - Invert colors.";
/**
* Text of the main help dialog.
*
* @memberOf sigplot
* @private
*/
var MAIN_HELP = "To zoom, press and drag the left mouse (LM) over the region of interest and release. " +
"To unzoom, press right mouse (RM). Press the middle mouse (MM) button or press the 'M' key to open the main menu." +
"View the function of all keypresses by selecting 'Keypress Info' from the main menu.";
/**
* Options used when displaying the spinner.
*
* @memberOf sigplot
* @private
*/
var SPINNER_OPTS = {
lines: 13, // The number of lines to draw
length: 7, // The length of each line
width: 4, // The line thickness
radius: 10, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 0, // The rotation offset
color: '#FFF', // #rgb or #rrggbb
speed: 1, // Rounds per second
trail: 60, // Afterglow percentage
shadow: false, // Whether to render a shadow
hwaccel: false, // Whether to use hardware acceleration
className: 'spinner', // The CSS class to assign to the spinner
zIndex: 2e9, // The z-index (defaults to 2000000000)
top: 'auto', // Top position relative to parent in px
left: 'auto' // Left position relative to parent in px
};
/**
* Attempts basic checks to determine if the browser is compatible with
* sigplot.
*
* @memberOf sigplot
* @private
*/
sigplot.browserIsCompatible = function browserIsCompatible() {
// We need a Canvas
var test_canvas = document.createElement('canvas');
var hascanvas = (test_canvas.getContext) ? true : false;
// We need ArrayBuffer
var hasarraybuf = ("ArrayBuffer" in window);
// File and FileReader are optional...and only
// required if the user wants to plot local files
return (hascanvas && hasarraybuf);
};
/**
* Construct and render a plot.
*
* @memberOf sigplot
* @constructor
*
* @example plot = new sigplot.Plot(document.getElementById('plot'), {[options]});
*
* @param element
* a 'div' DOM element
* @param [options]
* Key-value pairs whose values alter the behavior of the plot.
*
* @param {String}
* options.cmode the plot rendering mode "IN" = Index, "AB" =
* Abscissa (both of these, along with "__" can be added as prefixes to the other modes),
* "MA", "Magnitude" = Magnitude, "PH", "Phase" = Phase, "RE", "Real" = Real,
* "IM","Imaginary" = Imaginary, "LO", "D1", "10*log10" = 10*log, "L2" or "D2"
* , "20*log10" = 20*log, "RI", "Real/Imag", "Imag/Real","IR" = Real vs. Imaginary
*
* @param {String}
* options.phunits the phase units "D" = Degrees, "R" = Radians,
* "C" = Cycles
*
* @param {Boolean}
* options.cross display cross hairs on the plot
*
* @param {Boolean}
* options.nogrid hide the background grid
*
* @param {Boolean}
* options.legend set to false to hide the legend
*
* @param {Boolean}
* options.no_legend_button set to true to hide the legend button
*
* @param {Boolean}
* options.nopan disable panning on the plot
*
* @param {Boolean}
* options.nomenu disable the middle-click menu
*
* @param {Boolean}
* options.nospec hide all plot specification displays
*
* @param {Boolean}
* options.noxaxis hide the x-axis
*
* @param {Boolean}
* options.noyaxis hide the y-axis
*
* @param {Boolean}
* options.noreadout hide the plot readout area
*
* @param {Boolean}
* options.nodragdrop prevent file drag drop
*
* @param {Number}
* options.scroll_time_interval set the time interval for scrolling
*
* @param {Boolean}
* options.index use the data-index in the X axis
*
* @param {Number}
* options.autox auto-scaling settings for X axis !!!!CHANGED
*
* @param {Number}
* options.xmin the minimum range to display on the X axis
*
* @param {Number}
* options.xmax the maximum range to display on the X axis
*
* @param {Number}
* options.xlab the units that X-axis uses (see m.UNITS)
*
* @param {Object}
* options.xlabel function or string for custom X-axis label
*
* @param {Number}
* options.xdiv the number of divisions on the X axis
*
* @param {Number}
* options.xcnt configure the mtag mouse controls 0 = Off, 1
* (default) = LM Click, 2 = Continuous
*
* @param {String}
* options.rubberbox_mode controls the behavior of the rubberbox
* "zoom" (default) = zoom to the selected area "box" = trigger
* an mtag action on the selected area
*
* @param {String}
* options.rightclick_rubberbox_mode controls the behavior of the rubberbox
* "zoom" = zoom to the selected area "box" = trigger
* an mtag action on the selected area. By default is null to disable
* right-click boxes
*
* @param {Number}
* options.line the line type to draw 0 = None, 1 = Verticals, 2 =
* Horizontals, 3 (default) = Connecting
*
* @param {Number}
* options.autoy auto-scaling settings for Y axis !!!! CHANGED
* 0 = Fix , 1 = Auto Min , 2 = Auto Max, 3 = Full Auto
*
* @param {Number}
* options.ylab the units that Y-axis uses (see m.UNITS)
*
* @param {Object}
* options.ylabel function or string for custom Y-axis label
*
* @param {Number}
* options.ymin the minimum range to display on the Y axis
*
* @param {Number}
* options.ymax the maximum range to display on the Y axis
*
* @param {Number}
* options.ydiv the number of divisions on the Y axis
*
* @param {Number}
* options.zmin the minimum range to display on the Z axis
*
* @param {Number}
* options.zmax the maximum range to display on the Z axis
*
* @param {Boolean}
* options.yinv invert the y-axis
*
* @param {String}
* options.colors.fg the foreground color as a CSS color
*
* @param {String}
* options.colors.bg the background color as a CSS color
*
* @param {Boolean}
* options.xi invert the foreground/background colors
*
* @param {Boolean}
* options.all show all of the data on the plot instead of just
* one buffer
*
* @param {Boolean}
* options.expand auto-scale the plot based on all the data (when
* combined with the all option)
*
* @param {Number}
* options.origin 1 = x1:xmin, x2:xmax, y1:ymax, y2:ymin
* (default), 2 = x1:xmax, x2:xmin, y1:ymax, y2:ymin (x
* inverted), 3 = x1:xmax, x2:xmin, y1:ymin, y2:ymax (x & y
* inverted), 4 = x1:xmin, x2:xmax, y1:ymin, y2:ymax (y inverted)
*
* @param {Number}
* options.bufmax the buffer size to use
*
* @param {Boolean}
* options.nokeypress disable key press actions
*
* @param options.font_family
* the font family to use for text rendered on the plot. Monospace
* font will generally work best.
*
* @param {Boolean}
* options.font_scaled mimic the MIDAS plotting behaviour where the
* plot font-size is scaled relative to the width of the
*
* @param {Boolean}
* options.font_width sets the font width (default=8); if scaled_font
* is set to true, then the font width will be the minimum of font_width
* or plot width/64.
*
* @param {Number}
* options.panxpad
* Pad the x-axis panning with this amount. If a string in the form of
* "XX%" the padding will be a percentage.
*
* @param {Number}
* options.panypad
* Pad the y-axis panning with this amount. If a string in the form of
* "XX%" the padding will be a percentage.
*
* @returns {Plot}
*/
var Plot = function(element, options) {
if (!sigplot.browserIsCompatible()) {
throw "Browser is not compatible";
}
// Register with the Mx structure - Step #4
this._Mx = mx.open(element);
var Mx = this._Mx;
this._Gx = new GX();
this._Gx.parent = element;
// Variable which stores state of mouse position relative to the canvas
this.mouseOnCanvas = false;
if (!options) {
options = {};
}
plot_init(this, options);
this.mimicListeners = {
other: null,
listeners: {
zoom: null,
unzoom: null,
xpan: null,
ypan: null
}
};
this._refresh(); // Draw immediately
this.onmousemove = (function(plot) {
return function(e) {
var Mx = plot._Mx;
var Gx = plot._Gx;
var rect = e.target.getBoundingClientRect();
var xpos = (e.offsetX === undefined) ? (e.pageX - rect.left - window.scrollX) : e.offsetX;
var ypos = (e.offsetX === undefined) ? (e.pageY - rect.top - window.scrollY) : e.offsetY;
// var xpos = (e.offsetX === undefined) ? e.layerX : e.offsetX;
// var ypos = (e.offsetY === undefined) ? e.layerY : e.offsetY;
var re = pixel_to_real(plot, xpos, ypos);
Gx.retx = re.x;
Gx.rety = re.y;
if (Mx.widget) {
return;
}
display_specs(plot);
var evt = document.createEvent('Event');
evt.initEvent('mmove', true, true);
evt.originalEvent = e;
evt.xpos = xpos;
evt.ypos = ypos;
evt.x = Gx.retx;
evt.y = Gx.rety;
var executeDefault = mx.dispatchEvent(Mx, evt);
if (!executeDefault) {
return;
}
// The crosshair logic in websigplot is different
// because we need to clear the previous position
// of the line (via XOR) and then draw the new line
//
// The Mx.xpos and Mx.ypos may have already been
// updated to their new location so we need to store
// the crosshair position in the Gx structure
if (Gx.cross) {
if (Mx.warpbox) {
// during zoom operations undraw the crosshairs
if (Gx.cross_xpos !== undefined) {
mx.rubberline(Mx, Gx.cross_xpos, Mx.t,
Gx.cross_xpos, Mx.b);
}
if (Gx.cross_ypos !== undefined) {
mx.rubberline(Mx, Mx.l, Gx.cross_ypos, Mx.r,
Gx.cross_ypos);
}
Gx.cross_xpos = undefined;
Gx.cross_ypos = undefined;
} else {
if (plot.mouseOnCanvas) {
draw_crosshairs(plot);
if (Gx.p_cuts && (Gx.lyr.length === 1) && (Gx.lyr[0].hcb["class"] === 2)) {
if (!Gx.y_cut_press_on && !Gx.x_cut_press_on) {
draw_p_cuts(plot);
}
}
}
}
}
if (Gx.cntrls === 2) {
var evt = document.createEvent('Event');
evt.initEvent('mtag', true, true);
evt.originalEvent = e;
evt.x = Gx.retx;
evt.y = Gx.rety;
evt.xpos = xpos;
evt.ypos = ypos;
mx.dispatchEvent(Mx, evt);
}
};
}(this));
this.throttledOnMouseMove = m.throttle(this._Gx.scroll_time_interval,
this.onmousemove);
mx.addEventListener(Mx, "mousemove", this.throttledOnMouseMove, false);
this.onmouseout = (function(plot) {
return function(event) {
var Gx = plot._Gx;
var Mx = plot._Mx;
if (plot.mouseOnCanvas) {
plot.mouseOnCanvas = false;
if (Gx.autohide_readout) {
display_specs(plot);
}
if (Gx.autohide_panbars) {
draw_panbars(plot);
}
if (Mx.prompt) {
Mx.prompt.input.enableBlur();
}
}
};
}(this));
mx.addEventListener(Mx, "mouseout", this.onmouseout, false);
this.onmouseover = (function(plot) {
return function(event) {
var Gx = plot._Gx;
var Mx = plot._Mx;
plot.mouseOnCanvas = true;
if (Gx.autohide_panbars) {
draw_panbars(plot);
}
if (Mx.prompt) {
Mx.prompt.input.disableBlur();
}
};
}(this));
mx.addEventListener(Mx, "mouseover", this.onmouseover, false);
this.onmousedown = (function(plot) {
return function(event) {
event.preventDefault(); // mouse down on the canvas should never do a browser default action
var Mx = plot._Mx;
var Gx = plot._Gx;
if (Mx.widget && (Mx.widget.type === "ONESHOT")) {
Mx.widget = null;
plot.refresh();
}
// Update Mx event fields
mx.ifevent(Mx, event);
var evt = document.createEvent('Event');
evt.initEvent('mdown', true, true);
evt.originalEvent = event;
evt.xpos = Mx.xpos;
evt.ypos = Mx.ypos;
evt.x = Gx.retx;
evt.y = Gx.rety;
evt.which = event.which;
var executeDefault = mx.dispatchEvent(Mx, evt);
if (!executeDefault) {
return false;
}
// Check if event occured in the pan region
var inPan = inPanRegion(plot);
// Event processing
if (inPan.inPanRegion) { // Mouse position lies in a pan
// region
event.preventDefault();
if (inPan.command !== ' ') {
var scrollbar = null;
var position = null;
if (inPan.command === "XPAN") {
scrollbar = Mx.scrollbar_x;
} else if (inPan.command === "YPAN") {
scrollbar = Mx.scrollbar_y;
}
if (event.which === 2) {
position = {
x: Mx.xpos,
y: Mx.ypos
};
if ((scrollbar !== undefined) && (onScrollbar(position, scrollbar))) {
// Only show menu if on the scrollbar itself
sigplot_scrollScaleMenu(plot, inPan.command);
}
} else {
if (inPan.command !== ' ') {
position = {
x: Mx.xpos,
y: Mx.ypos
};
if (!onScrollbar(position, scrollbar) && event.which === 1) { // Left-clicking
// not on a
// scrollbar -
// handle
// typical pan
pan(plot, inPan.command, 0, event); // Execute
// the
// first
// pan
var repeatPan = function() {
if (!onScrollbar({
"x": Mx.xpos,
"y": Mx.ypos
}, scrollbar)) {
pan(plot, inPan.command, 0, event);
// execute
// a
// pan
// on
// every
// interval
} else {
// stop
// panning
// once you
// hit the
// scrollbar
if (Gx.stillPanning) {
window.clearInterval(Gx.stillPanning);
Gx.repeatPanning = undefined;
}
}
};
// Make scrolling smooth, the longer initial prevents
// a single click from counting twice
Gx.stillPanning = window.setTimeout(
function() {
Gx.repeatPanning = window.setInterval(repeatPan, 50);
}, 250);
}
}
}
}
} else { // Mouse not in a pan region, handle other cases
if (event.which === 1 || event.which === 3) {
var lButtonPressed = false;
if (Gx.legendBtnLocation) {
lButtonPressed = coordsInRectangle(Mx.xpos,
Mx.ypos, Gx.legendBtnLocation.x,
Gx.legendBtnLocation.y,
Gx.legendBtnLocation.width,
Gx.legendBtnLocation.height);
}
// a variable to hold the legend y positon of each layer
if (Gx.legend) {
var legendPos = get_legend_pos(plot);
var layerheight = legendPos.height / Gx.lyr.length;
for (var i = 0; i < Gx.lyr.length; i++) {
if ((legendPos.x <= Mx.xpos) && ((legendPos.x + legendPos.width) >= Mx.xpos) &&
((legendPos.y <= Mx.ypos)) && (legendPos.y + layerheight) >= Mx.ypos) {
//find a way to pull up the menu
Mx.mouseUpLatch = true;
sigplot_legend_menu(plot, i);
return false;
}
legendPos.y += layerheight;
}
}
// If we have a large colorbar, we also have buttons:
if (Gx.lg_colorbar && (Gx.lyr[0].hcb["class"] === 2)) {
if (event.which === 1 || event.which === 3) {
var mouse_x = Mx.xpos;
var mouse_y = Mx.ypos;
// Find vertex positions of top and bottom buttons
var top_x1 = Gx.cbb_top_x1;
var top_y1 = Gx.cbb_top_y1;
var top_x2 = top_x1 + Gx.cbb_width;
var top_y2 = top_y1;
var top_x3 = top_x1 + (1 / 2) * Gx.cbb_width;
var top_y3 = top_y1 - Gx.cbb_height;
var topButtonPressed = coordsInTriangle(mouse_x, mouse_y, top_x1, top_y1, top_x2, top_y2, top_x3, top_y3);
//console.log("Top ", topButtonPressed);
if (topButtonPressed) {
var cur_cmap = Gx.cmap;
plot.get_layer(0).img = undefined;
var current_map = m.Mc.colormap[cur_cmap];
for (var i = 0; i < current_map.colors.length; i++) {
current_map.colors[i].pos += 5.0;
}
mx.colormap(Mx, current_map.colors, 16);
//Gx.zoff += (1/10)*(Gx.zmax - Gx.zmin);
plot.refresh();
}
// bottom
var bot_x1 = Gx.cbb_bot_x1;
var bot_y1 = Gx.cbb_bot_y1;
var bot_x2 = bot_x1 + Gx.cbb_width;
var bot_y2 = bot_y1;
var bot_x3 = bot_x1 + (1 / 2) * Gx.cbb_width;
var bot_y3 = bot_y1 + Gx.cbb_height;
var botButtonPressed = coordsInTriangle(mouse_x, mouse_y, bot_x1, bot_y1, bot_x2, bot_y2, bot_x3, bot_y3);
if (botButtonPressed) {
//Gx.zoff -= (1/10)*(Gx.zmax - Gx.zmin);
var cur_cmap = Gx.cmap;
var current_map = m.Mc.colormap[cur_cmap];
plot.get_layer(0).img = undefined;
for (var i = 0; i < current_map.colors.length; i++) {
current_map.colors[i].pos -= 5.0;
}
mx.colormap(Mx, current_map.colors, 16);
plot.refresh();
}
}
}
if (lButtonPressed) {
plot.change_settings({
legend: !Gx.legend
}); // toggle the legend
} else {
display_specs(plot);
// Styles for rubberbox
var zoom_style = {
opacity: 0,
return_value: "zoom"
};
var select_style = {
opacity: 0.4,
fill_color: Mx.hi,
return_value: "select"
};
if (event.which === 1) {
if (Gx.default_rubberbox_action === "zoom") {
mx.rubberbox(Mx, rubberbox_cb(plot, event.which),
Gx.default_rubberbox_mode, zoom_style,
select_style);
} else if (Gx.default_rubberbox_action === "select") {
mx.rubberbox(Mx, rubberbox_cb(plot, event.which),
Gx.default_rubberbox_mode,
select_style, zoom_style);
} // otherwise rubber-box is considered disabled
} else if (event.which === 3) {
if (Gx.default_rightclick_rubberbox_action === "zoom") {
mx.rubberbox(Mx, rubberbox_cb(plot, event.which),
Gx.default_rightclick_rubberbox_mode, zoom_style,
select_style);
} else if (Gx.default_rightclick_rubberbox_action === "select") {
mx.rubberbox(Mx, rubberbox_cb(plot, event.which),
Gx.default_rightclick_rubberbox_mode,
select_style, zoom_style);
} // otherwise right-click rubber-box is considered disabled
}
}
} else if (event.which === 2) {
if (!Gx.nomenu) {
sigplot_mainmenu(plot);
}
}
}
return false;
};
}(this));
mx.addEventListener(Mx, "mousedown", this.onmousedown, false);
// Putting a finger on the screen and moving it, simulates
// pan.
this.ontouchstart = (function(plot) {
return function(event) {
event.preventDefault();
// See how many fingers are on the screen
// 1 finger == pan and/or unzoom
if (event.targetTouches.length === 1) {
// See if this is a double-tap
if (Mx.touchClear && Mx.touches) {
// Double tap unzooms to L=0 and fully expands the plot
window.clearTimeout(Mx.touchClear);
plot.unzoom();
middleClickScrollMenuAction(plot, mx.SB_FULL, "XPAN");
middleClickScrollMenuAction(plot, mx.SB_FULL, "YPAN");
} else {
// Normal touch prepares for panning
var touchEvent = event.targetTouches[0];
// Determine the touch position, relative to the canvas
var rect = touchEvent.target.getBoundingClientRect();
var position = {
x: (touchEvent.pageX - rect.left - window.scrollX),
y: (touchEvent.pageY - rect.top - window.scrollY)
};
// Update the Mx coordinates
Mx.xpos = m.bound(position.x, 0, Mx.width);
Mx.ypos = m.bound(position.y, 0, Mx.height);
// See if the finger lies on the pan-bars
var inPan = inPanRegion(plot, position);
if (!inPan.inPanRegion) {
Mx.touches = event.targetTouches;
} // TODO support touch 'pan' on the panbars
}
} else if (event.targetTouches.length === 2) {
Mx.touch_distance = m.touch_distance(event.targetTouches[0], event.targetTouches[1]);
}
};
}(this));
mx.addEventListener(Mx, "touchstart", this.ontouchstart, false);
this.ontouchmove = (function(plot) {
return function(event) {
var Mx = plot._Mx;
var Gx = plot._Gx;
var k = Mx.level;
event.preventDefault();
if (event.targetTouches.length === 1) {
// Determine the touch event position
var touchStart = Mx.touches[0];
var rect = touchStart.target.getBoundingClientRect();
var startPosition = {
x: (touchStart.pageX - rect.left - window.scrollX),
y: (touchStart.pageY - rect.top - window.scrollY)
};
var touchEvent = event.targetTouches[0];
var rect = touchEvent.target.getBoundingClientRect();
var position = {
x: (touchEvent.pageX - rect.left - window.scrollX),
y: (touchEvent.pageY - rect.top - window.scrollY)
};
var new_xpos = m.bound(position.x, 0, Mx.width);
var new_ypos = m.bound(position.y, 0, Mx.height);
var delta_xpos = new_xpos - Mx.xpos;
var delta_ypos = new_ypos - Mx.ypos;
Mx.xpos = new_xpos;
Mx.ypos = new_ypos;
var inPan = inPanRegion(plot, position);
// If we are in the pan region, don't take any action
if (inPan.inPanRegion) {
return;
}
// Pan proportionally to the movement of the touch
var xdelta = (Mx.stk[k].xscl * delta_xpos);
var ydelta = (Mx.stk[k].yscl * delta_ypos);
if (Mx.origin === 1) {
// regular x, regular y
xdelta *= -1;
} else if (Mx.origin === 2) {
// inverted x, regular y
ydelta *= -1;
} else if (Mx.origin === 3) {
// inverted x, inverted y
ydelta *= -1;
} else if (Mx.origin === 4) {
// regular x, inverted y
xdelta *= -1;
ydelta *= -1;
}
var xmin = Mx.stk[k].xmin + xdelta;
var xmax = Mx.stk[k].xmax + xdelta;
var ymin = Mx.stk[k].ymin + ydelta;
var ymax = Mx.stk[k].ymax + ydelta;
if ((xmin >= Gx.xmin) && (xmax <= Gx.xmax)) {
Mx.stk[k].xmin = xmin;
Mx.stk[k].xmax = xmax;
}
if ((ymin >= Gx.ymin) && (ymax <= Gx.ymax)) {
Mx.stk[k].ymin = ymin;
Mx.stk[k].ymax = ymax;
}
if (Gx.cmode === Gx.basemode && Mx.level === 1) {
Gx.xmin = Math.min(Gx.xmin, xmin);
Gx.xmax = Math.max(Gx.xmax, xmax);
Gx.ymin = Math.min(Gx.ymin, ymin);
Gx.ymax = Math.max(Gx.ymax, ymax);
}
plot.refresh();
} else if (event.targetTouches.length === 2) {
var cur_distance = m.touch_distance(event.targetTouches[0], event.targetTouches[1]);
var scaling = (1 - (Mx.touch_distance / cur_distance)) * 0.05;
var xran = Mx.stk[k].xmax - Mx.stk[k].xmin;
var yran = Mx.stk[k].ymax - Mx.stk[k].ymin;
var xmin = Mx.stk[k].xmin + (scaling * xran);
var xmax = Mx.stk[k].xmax - (scaling * xran);
var ymin = Mx.stk[k].ymin + (scaling * yran);
var ymax = Mx.stk[k].ymax - (scaling * yran);
Mx.stk[k].xmin = Math.max(Gx.xmin, xmin);
Mx.stk[k].xmax = Math.min(Gx.xmax, xmax);
Mx.stk[k].ymin = Math.max(Gx.ymin, ymin);
Mx.stk[k].ymax = Math.min(Gx.ymax, ymax);
plot.refresh();
}
};
}(this));
this.throttledOnTouchMove = m.throttle(
this._Gx.scroll_time_interval,
this.ontouchmove);
mx.addEventListener(Mx, "touchmove", this.throttledOnTouchMove, false);
this.ontouchend = (function(plot) {
return function(event) {
var Gx = plot._Gx;
var Mx = plot._Mx;
event.preventDefault();
console.log("on touch end ", event.targetTouches.length);
Gx.panning = undefined;
plot._Mx.scrollbar_x.action = 0;
plot._Mx.scrollbar_y.action = 0;
Mx.touch_distance = undefined;
mx.widget_callback(Mx, event);
// Only clear the touches after a slight delay so we can
// detect double-tap
Mx.touchClear = window.setTimeout(
function() {
Mx.touches = undefined;
Mx.touchClear = undefined;
}, 100);
};
}(this));
mx.addEventListener(Mx, "touchend", this.ontouchend, false);
this.docMouseUp = (function(plot) {
return function(event) {
var Gx = plot._Gx;
if (event.which === 1) {
// in general, you shouldn't put anything in here
// ...instead it should go into rubberbox_cb
Gx.panning = undefined;
plot._Mx.scrollbar_x.action = 0; // TODO Is this
// necessary?
plot._Mx.scrollbar_y.action = 0;
} //else if (event.which === 2) {
// nothing
//} else if (event.which === 3) {
// nothing
//}
if (Gx.stillPanning) {
window.clearTimeout(Gx.stillPanning);
Gx.stillPanning = undefined;
}
if (Gx.repeatPanning) { // Clear the panning interval on any
// mouse up in the document
window.clearInterval(Gx.repeatPanning);
Gx.repeatPanning = undefined;
}
return false;
};
}(this));
document.addEventListener("mouseup", this.docMouseUp, false);
this.mouseup = (function(plot) {
return function(event) {
event.preventDefault(); // mouse up on the canvas should never do a browser default action
var Gx = plot._Gx;
var Mx = plot._Mx;
// Update Mx event fields
mx.ifevent(plot._Mx, event);
var evt = document.createEvent('Event');
evt.initEvent('mup', true, true);
evt.originalEvent = event;
evt.xpos = Mx.xpos;
evt.ypos = Mx.ypos;
evt.x = Gx.retx;
evt.y = Gx.rety;
evt.which = event.which;
var executeDefault = mx.dispatchEvent(Mx, evt);
if (executeDefault) {
if (Mx.warpbox || Mx.widget || Mx.prompt) {
// If any of these are true, the mouseup is going
// to be handled by them...but this is a fragile approach
// because it relies upon implicit ordering of event dispatch
// for mouseup events. It should be improved/refactored at some point
return;
}
// Normal mouse up handling
if (event.which === 1) {
// If we are in the pan region, perform the pan
// otherwise emit an mtag
var inCenter = inPanCenterRegion(plot);
if (inCenter.inCenterRegion) {
if (inCenter.command !== ' ') {
pan(plot, inCenter.command, 0, event); // pan
}
} else if (Gx.cntrls === 1) {
// Update the mark
Gx.xmrk = Gx.retx;
Gx.ymrk = Gx.rety;
var mtagevt = document.createEvent('Event');
mtagevt.initEvent('mtag', true, true);
mtagevt.originalEvent = event;
mtagevt.x = Gx.xmrk;
mtagevt.y = Gx.ymrk;
mtagevt.xpos = event.x || event.clientX;
mtagevt.ypos = event.y || event.clientY;
mtagevt.w = undefined;
mtagevt.h = undefined;
mtagevt.shift = event.shiftKey;
mx.dispatchEvent(Mx, mtagevt);
// Refresh to draw the new marker position
//if (Gx.always_show_marker || Gx.show_marker) {
plot.redraw();
//}
}
} else if (event.which === 2) {
if (Gx.nomenu) {
// Send an event so that a custom menu can be displayed
// if desired
var evt = document.createEvent('Event');
evt.initEvent('showmenu', true, true);
evt.originalEvent = event;
evt.x = event.x || event.clientX;
evt.y = event.y || event.clientY;
var executeDefault = mx.dispatchEvent(Mx, evt);
if (executeDefault) {
if (event.stopPropagation) {
event.stopPropagation();
}
event.cancelBubble = true;
mx.removeEventListener(Mx, "mousedown", plot.onmousedown, false);
var emit_hidemenu = function() {