-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpureknob.js
1182 lines (1018 loc) · 28.5 KB
/
pureknob.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
/*
* pure-knob
*
* Canvas-based JavaScript UI element implementing touch,
* keyboard, mouse and scroll wheel support.
*
* Copyright 2018 - 2021 Andre Plötze
*
* Licensed 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.
*/
"use strict";
(function (root, factory) {
if ((typeof define === 'function') && define.amd) {
/* AMD. Register as an anonymous module. */
define([], factory);
} else if ((typeof module === 'object') && module.exports) {
/*
* Node.
*
* Does not work with strict CommonJS, but
* only CommonJS-like environments that support
* module.exports, like Node.
*/
module.exports = factory();
} else {
/* Browser globals (root is window). */
root.pureknob = factory();
}
} (typeof self !== 'undefined' ? self : this, function () {
function PureKnob() {
/*
* Creates a bar graph element.
*/
this.createBarGraph = function(width, height) {
const heightString = height.toString();
const widthString = width.toString();
const canvas = document.createElement('canvas');
const div = document.createElement('div');
div.style.display = 'inline-block';
div.style.height = heightString + 'px';
div.style.position = 'relative';
div.style.textAlign = 'center';
div.style.width = widthString + 'px';
div.appendChild(canvas);
/*
* The bar graph object.
*/
const graph = {
'_canvas': canvas,
'_div': div,
'_height': height,
'_width': width,
/*
* Properties of this bar graph.
*/
'_properties': {
'colorBG': '#181818',
'colorFG': '#ff8800',
'colorMarkers': '#888888',
'markerStart': 0,
'markerEnd': 100,
'markerStep': 20,
'trackWidth': 0.5,
'valMin': 0,
'valMax': 100,
'valPeaks': [],
'val': 0
},
/*
* Returns the peak values for this bar graph.
*/
'getPeaks': function() {
const properties = this._properties;
const peaks = properties.valPeaks;
const numPeaks = peaks.length;
const peaksCopy = [];
/*
* Iterate over the peak values and copy them.
*/
for (let i = 0; i < numPeaks; i++) {
const peak = peaks[i];
peaksCopy.push(peak);
}
return peaksCopy;
},
/*
* Returns the value of a property of this bar graph.
*/
'getProperty': function(key) {
const properties = this._properties;
const value = properties[key];
return value;
},
/*
* Returns the current value of the bar graph.
*/
'getValue': function() {
const properties = this._properties;
const value = properties.val;
return value;
},
/*
* Return the DOM node representing this bar graph.
*/
'node': function() {
const div = this._div;
return div;
},
/*
* Redraw the bar graph on the canvas.
*/
'redraw': function() {
this.resize();
const properties = this._properties;
const colorTrack = properties.colorBG;
const colorFilling = properties.colorFG;
const colorMarkers = properties.colorMarkers;
const markerStart = properties.markerStart;
const markerEnd = properties.markerEnd;
const markerStep = properties.markerStep;
const trackWidth = properties.trackWidth;
const valMin = properties.valMin;
const valMax = properties.valMax;
const peaks = properties.valPeaks;
const value = properties.val;
const height = this._height;
const width = this._width;
const lineWidth = Math.round(trackWidth * height);
const halfWidth = 0.5 * lineWidth;
const centerY = 0.5 * height;
const lineTop = centerY - halfWidth;
const lineBottom = centerY + halfWidth;
const relativeValue = (value - valMin) / (valMax - valMin);
const fillingEnd = width * relativeValue;
const numPeaks = peaks.length;
const canvas = this._canvas;
const ctx = canvas.getContext('2d');
/*
* Clear the canvas.
*/
ctx.clearRect(0, 0, width, height);
/*
* Check if markers should be drawn.
*/
if ((markerStart !== null) & (markerEnd !== null) & (markerStep !== null) & (markerStep !== 0)) {
ctx.lineCap = 'butt';
ctx.lineWidth = '2';
ctx.strokeStyle = colorMarkers;
/*
* Draw the markers.
*/
for (let v = markerStart; v <= markerEnd; v += markerStep) {
const relativePos = (v - valMin) / (valMax - valMin);
const pos = Math.round(width * relativePos);
ctx.beginPath();
ctx.moveTo(pos, 0);
ctx.lineTo(pos, height);
ctx.stroke();
}
}
/*
* Draw the track.
*/
ctx.beginPath();
ctx.rect(0, lineTop, width, lineWidth);
ctx.fillStyle = colorTrack;
ctx.fill();
/*
* Draw the filling.
*/
ctx.beginPath();
ctx.rect(0, lineTop, fillingEnd, lineWidth);
ctx.fillStyle = colorFilling;
ctx.fill();
/*
* Prepare for drawing the peaks.
*/
ctx.strokeStyle = colorFilling;
/*
* Draw the peaks.
*/
for (let i = 0; i < numPeaks; i++) {
const peak = peaks[i];
const relativePeak = (peak - valMin) / (valMax - valMin);
const pos = Math.round(width * relativePeak);
ctx.beginPath();
ctx.moveTo(pos, lineTop);
ctx.lineTo(pos, lineBottom);
ctx.stroke();
}
},
/*
* This is called as the canvas or the surrounding DIV is resized.
*/
'resize': function() {
const canvas = this._canvas;
const ctx = canvas.getContext('2d');
const scale = window.devicePixelRatio;
canvas.style.height = this._height + 'px';
canvas.style.width = this._width + 'px';
canvas.height = Math.floor(this._height * scale);
canvas.width = Math.floor(this._width * scale);
ctx.scale(scale, scale);
},
/*
* Sets the peak values of this bar graph.
*/
'setPeaks': function(peaks) {
const properties = this._properties;
const peaksCopy = [];
const numPeaks = peaks.length;
/*
* Iterate over the peak values and append them to the array.
*/
for (let i = 0; i < numPeaks; i++) {
const peak = peaks[i];
peaksCopy.push(peak);
}
this.setProperty('valPeaks', peaksCopy);
},
/*
* Sets the value of a property of this bar graph.
*/
'setProperty': function(key, value) {
this._properties[key] = value;
this.redraw();
},
/*
* Sets the value of this bar graph.
*/
'setValue': function(value) {
const properties = this._properties;
const valMin = properties.valMin;
const valMax = properties.valMax;
/*
* Clamp the actual value into the [valMin; valMax] range.
*/
if (value < valMin) {
value = valMin;
} else if (value > valMax) {
value = valMax;
}
value = Math.round(value);
this.setProperty('val', value);
}
};
/*
* This is called when the size of the canvas changes.
*/
const resizeListener = function(e) {
graph.redraw();
};
/*
* Listen for device pixel ratio changes.
*/
const updatePixelRatio = function() {
const pixelRatio = window.devicePixelRatio;
graph.redraw();
const pixelRatioString = pixelRatio.toString();
const matcher = '(resolution:' + pixelRatioString + 'dppx)';
const params = {
'once': true
};
window.matchMedia(matcher).addEventListener('change', updatePixelRatio, params);
}
canvas.addEventListener('resize', resizeListener);
updatePixelRatio();
return graph;
}
/*
* Creates a knob element.
*/
this.createKnob = function(width, height) {
const heightString = height.toString();
const widthString = width.toString();
const smaller = width < height ? width : height;
const fontSize = 0.2 * smaller;
const fontSizeString = fontSize.toString();
const canvas = document.createElement('canvas');
const div = document.createElement('div');
div.style.display = 'inline-block';
div.style.height = heightString + 'px';
div.style.position = 'relative';
div.style.textAlign = 'center';
div.style.width = widthString + 'px';
div.appendChild(canvas);
const input = document.createElement('input');
input.style.appearance = 'textfield';
input.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
input.style.border = 'none';
input.style.color = '#ff8800';
input.style.fontFamily = 'sans-serif';
input.style.fontSize = fontSizeString + 'px';
input.style.height = heightString + 'px';
input.style.margin = 'auto';
input.style.padding = '0px';
input.style.textAlign = 'center';
input.style.width = widthString + 'px';
const inputMode = document.createAttribute('inputmode');
inputMode.value = 'numeric';
input.setAttributeNode(inputMode);
const inputDiv = document.createElement('div');
inputDiv.style.bottom = '0px';
inputDiv.style.display = 'none';
inputDiv.style.left = '0px';
inputDiv.style.position = 'absolute';
inputDiv.style.right = '0px';
inputDiv.style.top = '0px';
inputDiv.appendChild(input);
div.appendChild(inputDiv);
/*
* The knob object.
*/
const knob = {
'_canvas': canvas,
'_div': div,
'_height': height,
'_input': input,
'_inputDiv': inputDiv,
'_listeners': [],
'_mousebutton': false,
'_previousVal': 0,
'_timeout': null,
'_timeoutDoubleTap': null,
'_touchCount': 0,
'_width': width,
/*
* Notify listeners about value changes.
*/
'_notifyUpdate': function() {
const properties = this._properties;
const value = properties.val;
const listeners = this._listeners;
const numListeners = listeners.length;
/*
* Call all listeners.
*/
for (let i = 0; i < numListeners; i++) {
const listener = listeners[i];
/*
* Call listener, if it exists.
*/
if (listener !== null) {
listener(this, value);
}
}
},
/*
* Properties of this knob.
*/
'_properties': {
'angleEnd': 2.0 * Math.PI,
'angleOffset': -0.5 * Math.PI,
'angleStart': 0,
'colorBG': '#181818',
'colorFG': '#ff8800',
'colorLabel': '#ffffff',
'fnStringToValue': function(string) { return parseInt(string); },
'fnValueToString': function(value) { return value.toString(); },
'label': null,
'needle': false,
'readonly': false,
'textScale': 1.0,
'trackWidth': 0.4,
'valMin': 0,
'valMax': 100,
'val': 0
},
/*
* Abort value change, restoring the previous value.
*/
'abort': function() {
const previousValue = this._previousVal;
const properties = this._properties;
properties.val = previousValue;
this.redraw();
},
/*
* Adds an event listener.
*/
'addListener': function(listener) {
const listeners = this._listeners;
listeners.push(listener);
},
/*
* Commit value, indicating that it is no longer temporary.
*/
'commit': function() {
const properties = this._properties;
const value = properties.val;
this._previousVal = value;
this.redraw();
this._notifyUpdate();
},
/*
* Returns the value of a property of this knob.
*/
'getProperty': function(key) {
const properties = this._properties;
const value = properties[key];
return value;
},
/*
* Returns the current value of the knob.
*/
'getValue': function() {
const properties = this._properties;
const value = properties.val;
return value;
},
/*
* Return the DOM node representing this knob.
*/
'node': function() {
const div = this._div;
return div;
},
/*
* Redraw the knob on the canvas.
*/
'redraw': function() {
this.resize();
const properties = this._properties;
const needle = properties.needle;
const angleStart = properties.angleStart;
const angleOffset = properties.angleOffset;
const angleEnd = properties.angleEnd;
const actualStart = angleStart + angleOffset;
const actualEnd = angleEnd + angleOffset;
const label = properties.label;
const value = properties.val;
const valueToString = properties.fnValueToString;
const valueStr = valueToString(value);
const valMin = properties.valMin;
const valMax = properties.valMax;
const relValue = (value - valMin) / (valMax - valMin);
const relAngle = relValue * (angleEnd - angleStart);
const angleVal = actualStart + relAngle;
const colorTrack = properties.colorBG;
const colorFilling = properties.colorFG;
const colorLabel = properties.colorLabel;
const textScale = properties.textScale;
const trackWidth = properties.trackWidth;
const height = this._height;
const width = this._width;
const smaller = width < height ? width : height;
const centerX = 0.5 * width;
const centerY = 0.5 * height;
const radius = 0.4 * smaller;
const labelY = centerY + radius;
const lineWidth = Math.round(trackWidth * radius);
const labelSize = Math.round(0.8 * lineWidth);
const labelSizeString = labelSize.toString();
const fontSize = (0.2 * smaller) * textScale;
const fontSizeString = fontSize.toString();
const canvas = this._canvas;
const ctx = canvas.getContext('2d');
/*
* Clear the canvas.
*/
ctx.clearRect(0, 0, width, height);
/*
* Draw the track.
*/
ctx.beginPath();
ctx.arc(centerX, centerY, radius, actualStart, actualEnd);
ctx.lineCap = 'butt';
ctx.lineWidth = lineWidth;
ctx.strokeStyle = colorTrack;
ctx.stroke();
/*
* Draw the filling.
*/
ctx.beginPath();
/*
* Check if we're in needle mode.
*/
if (needle) {
ctx.arc(centerX, centerY, radius, angleVal - 0.1, angleVal + 0.1);
} else {
ctx.arc(centerX, centerY, radius, actualStart, angleVal);
}
ctx.lineCap = 'butt';
ctx.lineWidth = lineWidth;
ctx.strokeStyle = colorFilling;
ctx.stroke();
/*
* Draw the number.
*/
ctx.font = fontSizeString + 'px sans-serif';
ctx.fillStyle = colorFilling;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(valueStr, centerX, centerY);
/*
* Draw the label
*/
if (label !== null) {
ctx.font = labelSizeString + 'px sans-serif';
ctx.fillStyle = colorLabel;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(label, centerX, labelY);
}
/*
* Set the color and font size of the input element.
*/
const elemInput = this._input;
elemInput.style.color = colorFilling;
elemInput.style.fontSize = fontSizeString + 'px';
},
/*
* This is called as the canvas or the surrounding DIV is resized.
*/
'resize': function() {
const canvas = this._canvas;
const ctx = canvas.getContext('2d');
const scale = window.devicePixelRatio;
canvas.style.height = this._height + 'px';
canvas.style.width = this._width + 'px';
canvas.height = Math.floor(this._height * scale);
canvas.width = Math.floor(this._width * scale);
ctx.scale(scale, scale);
},
/*
* Sets the value of a property of this knob.
*/
'setProperty': function(key, value) {
this._properties[key] = value;
this.redraw();
},
/*
* Sets the value of this knob.
*/
'setValue': function(value) {
this.setValueFloating(value);
this.commit();
},
/*
* Sets floating (temporary) value of this knob.
*/
'setValueFloating': function(value) {
const properties = this._properties;
const valMin = properties.valMin;
const valMax = properties.valMax;
/*
* Clamp the actual value into the [valMin; valMax] range.
*/
if (value < valMin) {
value = valMin;
} else if (value > valMax) {
value = valMax;
}
value = Math.round(value);
this.setProperty('val', value);
}
};
/*
* Convert mouse event to value.
*/
const mouseEventToValue = function(e, properties) {
const canvas = e.target;
const width = canvas.scrollWidth;
const height = canvas.scrollHeight;
const centerX = 0.5 * width;
const centerY = 0.5 * height;
const x = e.offsetX;
const y = e.offsetY;
const relX = x - centerX;
const relY = y - centerY;
const angleStart = properties.angleStart;
const angleEnd = properties.angleEnd;
const angleDiff = angleEnd - angleStart;
let angle = Math.atan2(relX, -relY) - angleStart;
const twoPi = 2.0 * Math.PI;
/*
* Make negative angles positive.
*/
if (angle < 0) {
if (angleDiff >= twoPi) {
angle += twoPi;
} else {
angle = 0;
}
}
const valMin = properties.valMin;
const valMax = properties.valMax;
let value = ((angle / angleDiff) * (valMax - valMin)) + valMin;
/*
* Clamp values into valid interval.
*/
if (value < valMin) {
value = valMin;
} else if (value > valMax) {
value = valMax;
}
return value;
};
/*
* Convert touch event to value.
*/
const touchEventToValue = function(e, properties) {
const canvas = e.target;
const rect = canvas.getBoundingClientRect();
const offsetX = rect.left;
const offsetY = rect.top;
const width = canvas.scrollWidth;
const height = canvas.scrollHeight;
const centerX = 0.5 * width;
const centerY = 0.5 * height;
const touches = e.targetTouches;
let touch = null;
/*
* If there are touches, extract the first one.
*/
if (touches.length > 0) {
touch = touches.item(0);
}
let x = 0.0;
let y = 0.0;
/*
* If a touch was extracted, calculate coordinates relative to
* the element position.
*/
if (touch !== null) {
const touchX = touch.clientX;
x = touchX - offsetX;
const touchY = touch.clientY;
y = touchY - offsetY;
}
const relX = x - centerX;
const relY = y - centerY;
const angleStart = properties.angleStart;
const angleEnd = properties.angleEnd;
const angleDiff = angleEnd - angleStart;
const twoPi = 2.0 * Math.PI;
let angle = Math.atan2(relX, -relY) - angleStart;
/*
* Make negative angles positive.
*/
if (angle < 0) {
if (angleDiff >= twoPi) {
angle += twoPi;
} else {
angle = 0;
}
}
const valMin = properties.valMin;
const valMax = properties.valMax;
let value = ((angle / angleDiff) * (valMax - valMin)) + valMin;
/*
* Clamp values into valid interval.
*/
if (value < valMin) {
value = valMin;
} else if (value > valMax) {
value = valMax;
}
return value;
};
/*
* Show input element on double click.
*/
const doubleClickListener = function(e) {
const properties = knob._properties;
const readonly = properties.readonly;
/*
* If knob is not read-only, display input element.
*/
if (!readonly) {
e.preventDefault();
const inputDiv = knob._inputDiv;
inputDiv.style.display = 'block';
const inputElem = knob._input;
inputElem.focus();
knob.redraw();
}
};
/*
* This is called when the mouse button is depressed.
*/
const mouseDownListener = function(e) {
const btn = e.buttons;
/*
* It is a left-click.
*/
if (btn === 1) {
const properties = knob._properties;
const readonly = properties.readonly;
/*
* If knob is not read-only, process mouse event.
*/
if (!readonly) {
e.preventDefault();
const val = mouseEventToValue(e, properties);
knob.setValueFloating(val);
}
knob._mousebutton = true;
}
/*
* It is a middle click.
*/
if (btn === 4) {
const properties = knob._properties;
const readonly = properties.readonly;
/*
* If knob is not read-only, display input element.
*/
if (!readonly) {
e.preventDefault();
const inputDiv = knob._inputDiv;
inputDiv.style.display = 'block';
const inputElem = knob._input;
inputElem.focus();
knob.redraw();
}
}
};
/*
* This is called when the mouse cursor is moved.
*/
const mouseMoveListener = function(e) {
const btn = knob._mousebutton;
/*
* Only process event, if mouse button is depressed.
*/
if (btn) {
const properties = knob._properties;
const readonly = properties.readonly;
/*
* If knob is not read-only, process mouse event.
*/
if (!readonly) {
e.preventDefault();
const val = mouseEventToValue(e, properties);
knob.setValueFloating(val);
}
}
};
/*
* This is called when the mouse button is released.
*/
const mouseUpListener = function(e) {
const btn = knob._mousebutton;
/*
* Only process event, if mouse button was depressed.
*/
if (btn) {
const properties = knob._properties;
const readonly = properties.readonly;
/*
* If knob is not read only, process mouse event.
*/
if (!readonly) {
e.preventDefault();
const val = mouseEventToValue(e, properties);
knob.setValue(val);
}
}
knob._mousebutton = false;
};
/*
* This is called when the drag action is canceled.
*/
const mouseCancelListener = function(e) {
const btn = knob._mousebutton;
/*
* Abort action if mouse button was depressed.
*/
if (btn) {
knob.abort();
knob._mousebutton = false;
}
};
/*
* This is called when a user touches the element.
*/
const touchStartListener = function(e) {
const properties = knob._properties;
const readonly = properties.readonly;
/*
* If knob is not read-only, process touch event.
*/
if (!readonly) {
const touches = e.targetTouches;
const numTouches = touches.length;
const singleTouch = (numTouches === 1);
/*
* Only process single touches, not multi-touch
* gestures.
*/
if (singleTouch) {
knob._mousebutton = true;
/*
* If this is the first touch, bind double tap
* interval.
*/
if (knob._touchCount === 0) {
/*
* This is executed when the double tap
* interval times out.
*/
const f = function() {
/*
* If control was tapped exactly
* twice, enable on-screen keyboard.
*/
if (knob._touchCount === 2) {
const properties = knob._properties;
const readonly = properties.readonly;
/*
* If knob is not read-only,
* display input element.
*/
if (!readonly) {
e.preventDefault();
const inputDiv = knob._inputDiv;
inputDiv.style.display = 'block';
const inputElem = knob._input;
inputElem.focus();
knob.redraw();
}
}
knob._touchCount = 0;
};
let timeout = knob._timeoutDoubleTap;
window.clearTimeout(timeout);
timeout = window.setTimeout(f, 500);
knob._timeoutDoubleTap = timeout;
}
knob._touchCount++;
const val = touchEventToValue(e, properties);
knob.setValueFloating(val);
}
}
};
/*
* This is called when a user moves a finger on the element.
*/
var touchMoveListener = function(e) {
const btn = knob._mousebutton;
/*
* Only process event, if mouse button is depressed.
*/
if (btn) {
const properties = knob._properties;
const readonly = properties.readonly;
/*
* If knob is not read-only, process touch event.
*/
if (!readonly) {
const touches = e.targetTouches;
const numTouches = touches.length;
const singleTouch = (numTouches === 1);
/*
* Only process single touches, not multi-touch
* gestures.
*/
if (singleTouch) {