forked from HanSolo/SteelSeries-Canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
steelseries.js
15832 lines (13916 loc) · 694 KB
/
steelseries.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
/*!
* Name : steelseries.js
* Authors : Gerrit Grunwald, Mark Crossley
* Last modified : 25.01.2016
* Revision : 0.14.17
*
* Copyright (c) 2011, Gerrit Grunwald, Mark Crossley
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* # Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* # Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*globals Tween */
/*jshint onevar:false,plusplus:false,nomen:false,bitwise:false*/
var steelseries = (function () {
// Constants
var HALF_PI = Math.PI * 0.5,
TWO_PI = Math.PI * 2,
PI = Math.PI,
RAD_FACTOR = Math.PI / 180,
DEG_FACTOR = 180 / Math.PI,
doc = document,
lcdFontName = 'LCDMono2Ultra,Arial,Verdana,sans-serif',
stdFontName = 'Arial,Verdana,sans-serif';
//************************************* C O M P O N O N E N T S ************************************************
var radial = function (canvas, parameters) {
parameters = parameters || {};
var gaugeType = (undefined === parameters.gaugeType ? steelseries.GaugeType.TYPE4 : parameters.gaugeType),
size = (undefined === parameters.size ? 0 : parameters.size),
minValue = (undefined === parameters.minValue ? 0 : parameters.minValue),
maxValue = (undefined === parameters.maxValue ? (minValue + 100) : parameters.maxValue),
niceScale = (undefined === parameters.niceScale ? true : parameters.niceScale),
threshold = (undefined === parameters.threshold ? (maxValue - minValue) / 2 + minValue: parameters.threshold),
thresholdRising = (undefined === parameters.thresholdRising ? true : parameters.thresholdRising),
section = (undefined === parameters.section ? null : parameters.section),
area = (undefined === parameters.area ? null : parameters.area),
titleString = (undefined === parameters.titleString ? '' : parameters.titleString),
unitString = (undefined === parameters.unitString ? '' : parameters.unitString),
frameDesign = (undefined === parameters.frameDesign ? steelseries.FrameDesign.METAL : parameters.frameDesign),
frameVisible = (undefined === parameters.frameVisible ? true : parameters.frameVisible),
backgroundColor = (undefined === parameters.backgroundColor ? steelseries.BackgroundColor.DARK_GRAY : parameters.backgroundColor),
backgroundVisible = (undefined === parameters.backgroundVisible ? true : parameters.backgroundVisible),
pointerType = (undefined === parameters.pointerType ? steelseries.PointerType.TYPE1 : parameters.pointerType),
pointerColor = (undefined === parameters.pointerColor ? steelseries.ColorDef.RED : parameters.pointerColor),
knobType = (undefined === parameters.knobType ? steelseries.KnobType.STANDARD_KNOB : parameters.knobType),
knobStyle = (undefined === parameters.knobStyle ? steelseries.KnobStyle.SILVER : parameters.knobStyle),
lcdColor = (undefined === parameters.lcdColor ? steelseries.LcdColor.STANDARD : parameters.lcdColor),
lcdVisible = (undefined === parameters.lcdVisible ? true : parameters.lcdVisible),
lcdDecimals = (undefined === parameters.lcdDecimals ? 2 : parameters.lcdDecimals),
digitalFont = (undefined === parameters.digitalFont ? false : parameters.digitalFont),
fractionalScaleDecimals = (undefined === parameters.fractionalScaleDecimals ? 1 : parameters.fractionalScaleDecimals),
ledColor = (undefined === parameters.ledColor ? steelseries.LedColor.RED_LED : parameters.ledColor),
ledVisible = (undefined === parameters.ledVisible ? true : parameters.ledVisible),
userLedColor = (undefined === parameters.userLedColor ? steelseries.LedColor.GREEN_LED : parameters.userLedColor),
userLedVisible = (undefined === parameters.userLedVisible ? false : parameters.userLedVisible),
thresholdVisible = (undefined === parameters.thresholdVisible ? true : parameters.thresholdVisible),
minMeasuredValueVisible = (undefined === parameters.minMeasuredValueVisible ? false : parameters.minMeasuredValueVisible),
maxMeasuredValueVisible = (undefined === parameters.maxMeasuredValueVisible ? false : parameters.maxMeasuredValueVisible),
foregroundType = (undefined === parameters.foregroundType ? steelseries.ForegroundType.TYPE1 : parameters.foregroundType),
foregroundVisible = (undefined === parameters.foregroundVisible ? true : parameters.foregroundVisible),
labelNumberFormat = (undefined === parameters.labelNumberFormat ? steelseries.LabelNumberFormat.STANDARD : parameters.labelNumberFormat),
playAlarm = (undefined === parameters.playAlarm ? false : parameters.playAlarm),
alarmSound = (undefined === parameters.alarmSound ? false : parameters.alarmSound),
customLayer = (undefined === parameters.customLayer ? null : parameters.customLayer),
tickLabelOrientation = (undefined === parameters.tickLabelOrientation ? (gaugeType === steelseries.GaugeType.TYPE1 ? steelseries.TickLabelOrientation.TANGENT : steelseries.TickLabelOrientation.NORMAL) : parameters.tickLabelOrientation),
trendVisible = (undefined === parameters.trendVisible ? false : parameters.trendVisible),
trendColors = (undefined === parameters.trendColors ? [steelseries.LedColor.RED_LED, steelseries.LedColor.GREEN_LED, steelseries.LedColor.CYAN_LED] : parameters.trendColors),
useOdometer = (undefined === parameters.useOdometer ? false : parameters.useOdometer),
odometerParams = (undefined === parameters.odometerParams ? {} : parameters.odometerParams),
odometerUseValue = (undefined === parameters.odometerUseValue ? false : parameters.odometerUseValue),
fullScaleDeflectionTime = (undefined === parameters.fullScaleDeflectionTime ? 2.5 : parameters.fullScaleDeflectionTime);
// Get the canvas context and clear it
var mainCtx = getCanvasContext(canvas);
// Has a size been specified?
if (size === 0) {
size = Math.min(mainCtx.canvas.width, mainCtx.canvas.height);
}
// Set the size - also clears the canvas
mainCtx.canvas.width = size;
mainCtx.canvas.height = size;
// Create audio tag for alarm sound
var audioElement;
if (playAlarm && alarmSound !== false) {
audioElement = doc.createElement('audio');
audioElement.setAttribute('src', alarmSound);
audioElement.setAttribute('preload', 'auto');
}
var value = minValue;
var odoValue = minValue;
var self = this;
// Properties
var minMeasuredValue = maxValue;
var maxMeasuredValue = minValue;
var ledBlinking = false;
var userLedBlinking = false;
var ledTimerId = 0;
var userLedTimerId = 0;
var tween;
var repainting = false;
var trendIndicator = steelseries.TrendState.OFF;
var trendSize = size * 0.06;
var trendPosX = size * 0.29;
var trendPosY = size * 0.36;
// GaugeType specific private variables
var freeAreaAngle;
var rotationOffset;
var tickmarkOffset;
var angleRange;
var angleStep;
var angle = rotationOffset + (value - minValue) * angleStep;
var imageWidth = size;
var imageHeight = size;
var centerX = imageWidth / 2;
var centerY = imageHeight / 2;
// Misc
var ledSize = size * 0.093457;
var ledPosX = 0.6 * imageWidth;
var ledPosY = 0.4 * imageHeight;
var userLedPosX = gaugeType === steelseries.GaugeType.TYPE3 ? 0.6 * imageWidth : centerX - ledSize / 2;
var userLedPosY = gaugeType === steelseries.GaugeType.TYPE3 ? 0.72 * imageHeight : 0.75 * imageHeight;
var lcdFontHeight = Math.floor(imageWidth / 10);
var stdFont = lcdFontHeight + 'px ' + stdFontName;
var lcdFont = lcdFontHeight + 'px ' + lcdFontName;
var lcdHeight = imageHeight * 0.13;
var lcdWidth = imageWidth * 0.4;
var lcdPosX = (imageWidth - lcdWidth) / 2;
var lcdPosY = imageHeight * 0.57;
var odoPosX, odoPosY = imageHeight * 0.61;
var shadowOffset = imageWidth * 0.006;
// Constants
var initialized = false;
// Tickmark specific private variables
var niceMinValue = minValue;
var niceMaxValue = maxValue;
var niceRange = maxValue - minValue;
var range = niceMaxValue - niceMinValue;
var minorTickSpacing = 0;
var majorTickSpacing = 0;
var maxNoOfMinorTicks = 10;
var maxNoOfMajorTicks = 10;
// Method to calculate nice values for min, max and range for the tickmarks
var calculate = function calculate() {
if (niceScale) {
niceRange = calcNiceNumber(maxValue - minValue, false);
majorTickSpacing = calcNiceNumber(niceRange / (maxNoOfMajorTicks - 1), true);
niceMinValue = Math.floor(minValue / majorTickSpacing) * majorTickSpacing;
niceMaxValue = Math.ceil(maxValue / majorTickSpacing) * majorTickSpacing;
minorTickSpacing = calcNiceNumber(majorTickSpacing / (maxNoOfMinorTicks - 1), true);
minValue = niceMinValue;
maxValue = niceMaxValue;
range = maxValue - minValue;
} else {
niceRange = (maxValue - minValue);
niceMinValue = minValue;
niceMaxValue = maxValue;
range = niceRange;
majorTickSpacing = calcNiceNumber(niceRange / (maxNoOfMajorTicks - 1), true);
minorTickSpacing = calcNiceNumber(majorTickSpacing / (maxNoOfMinorTicks - 1), true);
}
// Make sure values are still in range
value = value < minValue ? minValue : value > maxValue ? maxValue : value;
minMeasuredValue = minMeasuredValue < minValue ? minValue : minMeasuredValue > maxValue ? maxValue : minMeasuredValue;
maxMeasuredValue = maxMeasuredValue < minValue ? minValue : maxMeasuredValue > maxValue ? maxValue : maxMeasuredValue;
threshold = threshold < minValue ? minValue : threshold > maxValue ? maxValue : threshold;
switch (gaugeType.type) {
case 'type1':
freeAreaAngle = 0;
rotationOffset = PI;
tickmarkOffset = HALF_PI;
angleRange = HALF_PI;
angleStep = angleRange / range;
break;
case 'type2':
freeAreaAngle = 0;
rotationOffset = PI;
tickmarkOffset = HALF_PI;
angleRange = PI;
angleStep = angleRange / range;
break;
case 'type3':
freeAreaAngle = 0;
rotationOffset = HALF_PI;
tickmarkOffset = 0;
angleRange = 1.5 * PI;
angleStep = angleRange / range;
break;
case 'type4':
/* falls through */
default:
freeAreaAngle = 60 * RAD_FACTOR;
rotationOffset = HALF_PI + (freeAreaAngle / 2);
tickmarkOffset = 0;
angleRange = TWO_PI - freeAreaAngle;
angleStep = angleRange / range;
break;
}
angle = rotationOffset + (value - minValue) * angleStep;
};
// ************** Buffer creation ********************
// Buffer for the frame
var frameBuffer = createBuffer(size, size);
var frameContext = frameBuffer.getContext('2d');
// Buffer for the background
var backgroundBuffer = createBuffer(size, size);
var backgroundContext = backgroundBuffer.getContext('2d');
var lcdBuffer;
// Buffer for led on painting code
var ledBufferOn = createBuffer(ledSize, ledSize);
var ledContextOn = ledBufferOn.getContext('2d');
// Buffer for led off painting code
var ledBufferOff = createBuffer(ledSize, ledSize);
var ledContextOff = ledBufferOff.getContext('2d');
// Buffer for current led painting code
var ledBuffer = ledBufferOff;
// Buffer for user led on painting code
var userLedBufferOn = createBuffer(ledSize, ledSize);
var userLedContextOn = userLedBufferOn.getContext('2d');
// Buffer for user led off painting code
var userLedBufferOff = createBuffer(ledSize, ledSize);
var userLedContextOff = userLedBufferOff.getContext('2d');
// Buffer for current user led painting code
var userLedBuffer = userLedBufferOff;
// Buffer for the minMeasuredValue indicator
var minMeasuredValueBuffer = createBuffer(Math.ceil(size * 0.028037), Math.ceil(size * 0.028037));
var minMeasuredValueCtx = minMeasuredValueBuffer.getContext('2d');
// Buffer for the maxMeasuredValue indicator
var maxMeasuredValueBuffer = createBuffer(Math.ceil(size * 0.028037), Math.ceil(size * 0.028037));
var maxMeasuredValueCtx = maxMeasuredValueBuffer.getContext('2d');
// Buffer for pointer image painting code
var pointerBuffer = createBuffer(size, size);
var pointerContext = pointerBuffer.getContext('2d');
// Buffer for static foreground painting code
var foregroundBuffer = createBuffer(size, size);
var foregroundContext = foregroundBuffer.getContext('2d');
// Buffers for trend indicators
var trendUpBuffer, trendSteadyBuffer, trendDownBuffer, trendOffBuffer;
// Buffer for odometer
var odoGauge, odoBuffer, odoContext;
if (useOdometer && lcdVisible) {
odoBuffer = createBuffer(10, 10); // size doesn't matter, it will get reset by odometer code
odoContext = odoBuffer.getContext('2d');
}
// ************** Image creation ********************
var drawLcdText = function (ctx, value) {
ctx.restore();
ctx.save();
ctx.textAlign = 'right';
ctx.strokeStyle = lcdColor.textColor;
ctx.fillStyle = lcdColor.textColor;
if (lcdColor === steelseries.LcdColor.STANDARD || lcdColor === steelseries.LcdColor.STANDARD_GREEN) {
ctx.shadowColor = 'gray';
ctx.shadowOffsetX = imageWidth * 0.007;
ctx.shadowOffsetY = imageWidth * 0.007;
ctx.shadowBlur = imageWidth * 0.007;
}
if (digitalFont) {
ctx.font = lcdFont;
} else {
ctx.font = stdFont;
}
ctx.fillText(value.toFixed(lcdDecimals), lcdPosX + lcdWidth - lcdWidth * 0.05, lcdPosY + lcdHeight * 0.5 + lcdFontHeight * 0.38, lcdWidth * 0.9);
ctx.restore();
};
var drawPostsImage = function (ctx) {
ctx.save();
if ('type1' === gaugeType.type) {
// Draw max center top post
ctx.drawImage(createKnobImage(Math.ceil(imageHeight * 0.037383), steelseries.KnobType.STANDARD_KNOB, knobStyle), imageWidth * 0.523364, imageHeight * 0.130841);
}
if ('type1' === gaugeType.type || 'type2' === gaugeType.type) {
// Draw min left post
ctx.drawImage(createKnobImage(Math.ceil(imageHeight * 0.037383), steelseries.KnobType.STANDARD_KNOB, knobStyle), imageWidth * 0.130841, imageHeight * 0.514018);
}
if ('type2' === gaugeType.type || 'type3' === gaugeType.type) {
// Draw max right post
ctx.drawImage(createKnobImage(Math.ceil(imageHeight * 0.037383), steelseries.KnobType.STANDARD_KNOB, knobStyle), imageWidth * 0.831775, imageHeight * 0.514018);
}
if ('type3' === gaugeType.type) {
// Draw min center bottom post
ctx.drawImage(createKnobImage(Math.ceil(imageHeight * 0.037383), steelseries.KnobType.STANDARD_KNOB, knobStyle), imageWidth * 0.523364, imageHeight * 0.831775);
}
if ('type4' === gaugeType.type) {
// Min post
ctx.drawImage(createKnobImage(Math.ceil(imageHeight * 0.037383), steelseries.KnobType.STANDARD_KNOB, knobStyle), imageWidth * 0.336448, imageHeight * 0.803738);
// Max post
ctx.drawImage(createKnobImage(Math.ceil(imageHeight * 0.037383), steelseries.KnobType.STANDARD_KNOB, knobStyle), imageWidth * 0.626168, imageHeight * 0.803738);
}
ctx.restore();
};
var createThresholdImage = function () {
var thresholdBuffer = doc.createElement('canvas');
thresholdBuffer.width = Math.ceil(size * 0.046728);
thresholdBuffer.height = Math.ceil(thresholdBuffer.width * 0.9);
var thresholdCtx = thresholdBuffer.getContext('2d');
thresholdCtx.save();
var gradThreshold = thresholdCtx.createLinearGradient(0, 0.1, 0, thresholdBuffer.height * 0.9);
gradThreshold.addColorStop(0, '#520000');
gradThreshold.addColorStop(0.3, '#fc1d00');
gradThreshold.addColorStop(0.59, '#fc1d00');
gradThreshold.addColorStop(1, '#520000');
thresholdCtx.fillStyle = gradThreshold;
thresholdCtx.beginPath();
thresholdCtx.moveTo(thresholdBuffer.width * 0.5, 0.1);
thresholdCtx.lineTo(thresholdBuffer.width * 0.9, thresholdBuffer.height * 0.9);
thresholdCtx.lineTo(thresholdBuffer.width * 0.1, thresholdBuffer.height * 0.9);
thresholdCtx.lineTo(thresholdBuffer.width * 0.5, 0.1);
thresholdCtx.closePath();
thresholdCtx.fill();
thresholdCtx.strokeStyle = '#FFFFFF';
thresholdCtx.stroke();
thresholdCtx.restore();
return thresholdBuffer;
};
var drawAreaSectionImage = function (ctx, start, stop, color, filled) {
if (start < minValue) {
start = minValue;
} else if (start > maxValue) {
start = maxValue;
}
if (stop < minValue) {
stop = minValue;
} else if (stop > maxValue) {
stop = maxValue;
}
if (start >= stop) {
return;
}
ctx.save();
ctx.strokeStyle = color;
ctx.fillStyle = color;
ctx.lineWidth = imageWidth * 0.035;
var startAngle = (angleRange / range * start - angleRange / range * minValue);
var stopAngle = startAngle + (stop - start) / (range / angleRange);
ctx.translate(centerX, centerY);
ctx.rotate(rotationOffset);
ctx.beginPath();
if (filled) {
ctx.moveTo(0, 0);
ctx.arc(0, 0, imageWidth * 0.365 - ctx.lineWidth / 2, startAngle, stopAngle, false);
} else {
ctx.arc(0, 0, imageWidth * 0.365, startAngle, stopAngle, false);
}
if (filled) {
ctx.moveTo(0, 0);
ctx.fill();
} else {
ctx.stroke();
}
ctx.translate(-centerX, -centerY);
ctx.restore();
};
var drawTickmarksImage = function (ctx, labelNumberFormat) {
var fontSize = Math.ceil(imageWidth * 0.04),
alpha = rotationOffset, // Tracks total rotation
rotationStep = angleStep * minorTickSpacing,
textRotationAngle,
valueCounter = minValue,
majorTickCounter = maxNoOfMinorTicks - 1,
OUTER_POINT = imageWidth * 0.38,
MAJOR_INNER_POINT = imageWidth * 0.35,
MED_INNER_POINT = imageWidth * 0.355,
MINOR_INNER_POINT = imageWidth * 0.36,
TEXT_TRANSLATE_X = imageWidth * 0.3,
TEXT_WIDTH = imageWidth * 0.1,
HALF_MAX_NO_OF_MINOR_TICKS = maxNoOfMinorTicks / 2,
MAX_VALUE_ROUNDED = parseFloat(maxValue.toFixed(2)),
i;
backgroundColor.labelColor.setAlpha(1);
ctx.save();
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = fontSize + 'px ' + stdFontName;
ctx.strokeStyle = backgroundColor.labelColor.getRgbaColor();
ctx.fillStyle = backgroundColor.labelColor.getRgbaColor();
ctx.translate(centerX, centerY);
ctx.rotate(rotationOffset);
if (gaugeType.type === 'type1' || gaugeType.type === 'type2') {
TEXT_WIDTH = imageWidth * 0.04;
}
for (i = minValue; parseFloat(i.toFixed(2)) <= MAX_VALUE_ROUNDED; i += minorTickSpacing) {
textRotationAngle = rotationStep + HALF_PI;
majorTickCounter++;
// Draw major tickmarks
if (majorTickCounter === maxNoOfMinorTicks) {
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.moveTo(OUTER_POINT, 0);
ctx.lineTo(MAJOR_INNER_POINT, 0);
ctx.closePath();
ctx.stroke();
ctx.save();
ctx.translate(TEXT_TRANSLATE_X, 0);
switch (tickLabelOrientation.type) {
case 'horizontal':
textRotationAngle = -alpha;
break;
case 'tangent':
textRotationAngle = (alpha <= HALF_PI + PI ? PI : 0);
break;
case 'normal':
/* falls through */
default:
textRotationAngle = HALF_PI;
break;
}
ctx.rotate(textRotationAngle);
switch (labelNumberFormat.format) {
case 'fractional':
ctx.fillText((valueCounter.toFixed(fractionalScaleDecimals)), 0, 0, TEXT_WIDTH);
break;
case 'scientific':
ctx.fillText((valueCounter.toPrecision(2)), 0, 0, TEXT_WIDTH);
break;
case 'standard':
/* falls through */
default:
ctx.fillText((valueCounter.toFixed(0)), 0, 0, TEXT_WIDTH);
break;
}
ctx.translate(-TEXT_TRANSLATE_X, 0);
ctx.restore();
valueCounter += majorTickSpacing;
majorTickCounter = 0;
ctx.rotate(rotationStep);
alpha += rotationStep;
continue;
}
// Draw tickmark every minor tickmark spacing
if (0 === maxNoOfMinorTicks % 2 && majorTickCounter === (HALF_MAX_NO_OF_MINOR_TICKS)) {
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(OUTER_POINT, 0);
ctx.lineTo(MED_INNER_POINT, 0);
ctx.closePath();
ctx.stroke();
} else {
ctx.lineWidth = 0.5;
ctx.beginPath();
ctx.moveTo(OUTER_POINT, 0);
ctx.lineTo(MINOR_INNER_POINT, 0);
ctx.closePath();
ctx.stroke();
}
ctx.rotate(rotationStep);
alpha += rotationStep;
}
/*
// Logarithmic scale
var tmp = 0.1;
var minValueLog10 = 0.1;
var maxValueLog10 = parseInt(Math.pow(10, Math.ceil(Math.log10(maxValue))));
var drawLabel = true;
angleStep = angleRange / (maxValueLog10 - minValueLog10)
for (var scaleFactor = minValueLog10 ; scaleFactor <= maxValueLog10 ; scaleFactor *= 10)
{
for (var i = parseFloat((1 * scaleFactor).toFixed(1)) ; i < parseFloat((10 * scaleFactor).toFixed(1)) ; i += scaleFactor)
{
textRotationAngle =+ rotationStep + HALF_PI;
if(drawLabel)
{
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.moveTo(imageWidth * 0.38,0);
ctx.lineTo(imageWidth * 0.35,0);
ctx.closePath();
ctx.stroke();
ctx.save();
ctx.translate(imageWidth * 0.31, 0);
ctx.rotate(textRotationAngle);
ctx.fillText(parseFloat((i).toFixed(1)), 0, 0, imageWidth * 0.0375);
ctx.translate(-imageWidth * 0.31, 0);
ctx.restore();
drawLabel = false;
}
else
{
ctx.lineWidth = 0.5;
ctx.beginPath();
ctx.moveTo(imageWidth * 0.38,0);
ctx.lineTo(imageWidth * 0.36,0);
ctx.closePath();
ctx.stroke();
}
//doc.write('log10 scale value: ' + parseFloat((i).toFixed(1)) + '<br>');
//Math.log10(parseFloat((i).toFixed(1)));
ctx.rotate(rotationStep);
}
tmp = 0.1;
drawLabel = true;
}
*/
ctx.translate(-centerX, -centerY);
ctx.restore();
};
// ************** Initialization ********************
// Draw all static painting code to background
var init = function (parameters) {
parameters = parameters || {};
var drawFrame = (undefined === parameters.frame ? false : parameters.frame);
var drawBackground = (undefined === parameters.background ? false : parameters.background);
var drawLed = (undefined === parameters.led ? false : parameters.led);
var drawUserLed = (undefined === parameters.userLed ? false : parameters.userLed);
var drawPointer = (undefined === parameters.pointer ? false : parameters.pointer);
var drawForeground = (undefined === parameters.foreground ? false : parameters.foreground);
var drawTrend = (undefined === parameters.trend ? false : parameters.trend);
var drawOdo = (undefined === parameters.odo ? false : parameters.odo);
initialized = true;
// Calculate the current min and max values and the range
calculate();
// Create frame in frame buffer (backgroundBuffer)
if (drawFrame && frameVisible) {
drawRadialFrameImage(frameContext, frameDesign, centerX, centerY, imageWidth, imageHeight);
}
// Create background in background buffer (backgroundBuffer)
if (drawBackground && backgroundVisible) {
drawRadialBackgroundImage(backgroundContext, backgroundColor, centerX, centerY, imageWidth, imageHeight);
// Create custom layer in background buffer (backgroundBuffer)
drawRadialCustomImage(backgroundContext, customLayer, centerX, centerY, imageWidth, imageHeight);
}
if (drawLed) {
// Draw LED ON in ledBuffer_ON
ledContextOn.drawImage(createLedImage(Math.ceil(size * 0.093457), 1, ledColor), 0, 0);
// Draw LED OFF in ledBuffer_OFF
ledContextOff.drawImage(createLedImage(Math.ceil(size * 0.093457), 0, ledColor), 0, 0);
}
if (drawUserLed) {
// Draw user LED ON in userLedBuffer_ON
userLedContextOn.drawImage(createLedImage(Math.ceil(size * 0.093457), 1, userLedColor), 0, 0);
// Draw user LED OFF in userLedBuffer_OFF
userLedContextOff.drawImage(createLedImage(Math.ceil(size * 0.093457), 0, userLedColor), 0, 0);
}
// Draw min measured value indicator in minMeasuredValueBuffer
if (minMeasuredValueVisible) {
minMeasuredValueCtx.drawImage(createMeasuredValueImage(Math.ceil(size * 0.028037), steelseries.ColorDef.BLUE.dark.getRgbaColor(), true, true), 0, 0);
}
// Draw max measured value indicator in maxMeasuredValueBuffer
if (maxMeasuredValueVisible) {
maxMeasuredValueCtx.drawImage(createMeasuredValueImage(Math.ceil(size * 0.028037), steelseries.ColorDef.RED.medium.getRgbaColor(), true), 0, 0);
}
// Create alignment posts in background buffer (backgroundBuffer)
if (drawBackground && backgroundVisible) {
drawPostsImage(backgroundContext);
// Create section in background buffer (backgroundBuffer)
if (null !== section && 0 < section.length) {
var sectionIndex = section.length;
do {
sectionIndex--;
drawAreaSectionImage(backgroundContext, section[sectionIndex].start, section[sectionIndex].stop, section[sectionIndex].color, false);
}
while (0 < sectionIndex);
}
// Create area in background buffer (backgroundBuffer)
if (null !== area && 0 < area.length) {
var areaIndex = area.length;
do {
areaIndex--;
drawAreaSectionImage(backgroundContext, area[areaIndex].start, area[areaIndex].stop, area[areaIndex].color, true);
}
while (0 < areaIndex);
}
// Create tickmarks in background buffer (backgroundBuffer)
drawTickmarksImage(backgroundContext, labelNumberFormat);
// Create title in background buffer (backgroundBuffer)
drawTitleImage(backgroundContext, imageWidth, imageHeight, titleString, unitString, backgroundColor, true, true);
}
// Draw threshold image to background context
if (drawBackground && thresholdVisible) {
backgroundContext.save();
backgroundContext.translate(centerX, centerY);
backgroundContext.rotate(rotationOffset + (threshold - minValue) * angleStep + HALF_PI);
backgroundContext.translate(-centerX, -centerY);
backgroundContext.drawImage(createThresholdImage(), imageWidth * 0.475, imageHeight * 0.13);
backgroundContext.translate(centerX, centerY);
backgroundContext.restore();
}
// Create lcd background if selected in background buffer (backgroundBuffer)
if (drawBackground && lcdVisible) {
if (useOdometer && drawOdo) {
odoGauge = new steelseries.Odometer('', {
_context: odoContext,
height: size * 0.075,
decimals: odometerParams.decimals,
digits: (odometerParams.digits === undefined ? 5 : odometerParams.digits),
valueForeColor: odometerParams.valueForeColor,
valueBackColor: odometerParams.valueBackColor,
decimalForeColor: odometerParams.decimalForeColor,
decimalBackColor: odometerParams.decimalBackColor,
font: odometerParams.font,
value: value
});
odoPosX = (imageWidth - odoBuffer.width) / 2;
} else if (!useOdometer) {
lcdBuffer = createLcdBackgroundImage(lcdWidth, lcdHeight, lcdColor);
backgroundContext.drawImage(lcdBuffer, lcdPosX, lcdPosY);
}
}
// Create pointer image in pointer buffer (contentBuffer)
if (drawPointer) {
drawPointerImage(pointerContext, imageWidth, pointerType, pointerColor, backgroundColor.labelColor);
}
// Create foreground in foreground buffer (foregroundBuffer)
if (drawForeground && foregroundVisible) {
var knobVisible = (pointerType.type === 'type15' || pointerType.type === 'type16' ? false : true);
drawRadialForegroundImage(foregroundContext, foregroundType, imageWidth, imageHeight, knobVisible, knobType, knobStyle, gaugeType);
}
// Create the trend indicator buffers
if (drawTrend && trendVisible) {
trendUpBuffer = createTrendIndicator(trendSize, steelseries.TrendState.UP, trendColors);
trendSteadyBuffer = createTrendIndicator(trendSize, steelseries.TrendState.STEADY, trendColors);
trendDownBuffer = createTrendIndicator(trendSize, steelseries.TrendState.DOWN, trendColors);
trendOffBuffer = createTrendIndicator(trendSize, steelseries.TrendState.OFF, trendColors);
}
};
var resetBuffers = function (buffers) {
buffers = buffers || {};
var resetFrame = (undefined === buffers.frame ? false : buffers.frame);
var resetBackground = (undefined === buffers.background ? false : buffers.background);
var resetLed = (undefined === buffers.led ? false : buffers.led);
var resetUserLed = (undefined === buffers.userLed ? false : buffers.userLed);
var resetPointer = (undefined === buffers.pointer ? false : buffers.pointer);
var resetForeground = (undefined === buffers.foreground ? false : buffers.foreground);
if (resetFrame) {
frameBuffer.width = size;
frameBuffer.height = size;
frameContext = frameBuffer.getContext('2d');
}
if (resetBackground) {
backgroundBuffer.width = size;
backgroundBuffer.height = size;
backgroundContext = backgroundBuffer.getContext('2d');
}
if (resetLed) {
ledBufferOn.width = Math.ceil(size * 0.093457);
ledBufferOn.height = Math.ceil(size * 0.093457);
ledContextOn = ledBufferOn.getContext('2d');
ledBufferOff.width = Math.ceil(size * 0.093457);
ledBufferOff.height = Math.ceil(size * 0.093457);
ledContextOff = ledBufferOff.getContext('2d');
// Buffer for current led painting code
ledBuffer = ledBufferOff;
}
if (resetUserLed) {
userLedBufferOn.width = Math.ceil(size * 0.093457);
userLedBufferOn.height = Math.ceil(size * 0.093457);
userLedContextOn = userLedBufferOn.getContext('2d');
userLedBufferOff.width = Math.ceil(size * 0.093457);
userLedBufferOff.height = Math.ceil(size * 0.093457);
userLedContextOff = userLedBufferOff.getContext('2d');
// Buffer for current user led painting code
userLedBuffer = userLedBufferOff;
}
if (resetPointer) {
pointerBuffer.width = size;
pointerBuffer.height = size;
pointerContext = pointerBuffer.getContext('2d');
}
if (resetForeground) {
foregroundBuffer.width = size;
foregroundBuffer.height = size;
foregroundContext = foregroundBuffer.getContext('2d');
}
};
var toggleAndRepaintLed = function () {
if (ledVisible) {
if (ledBuffer === ledBufferOn) {
ledBuffer = ledBufferOff;
} else {
ledBuffer = ledBufferOn;
}
if (!repainting) {
repainting = true;
requestAnimFrame(self.repaint);
}
}
};
var toggleAndRepaintUserLed = function () {
if (userLedVisible) {
if (userLedBuffer === userLedBufferOn) {
userLedBuffer = userLedBufferOff;
} else {
userLedBuffer = userLedBufferOn;
}
if (!repainting) {
repainting = true;
requestAnimFrame(self.repaint);
}
}
};
var blink = function (blinking) {
if (blinking) {
ledTimerId = setInterval(toggleAndRepaintLed, 1000);
} else {
clearInterval(ledTimerId);
ledBuffer = ledBufferOff;
}
};
var blinkUser = function (blinking) {
if (blinking) {
userLedTimerId = setInterval(toggleAndRepaintUserLed, 1000);
} else {
clearInterval(userLedTimerId);
userLedBuffer = userLedBufferOff;
}
};
//************************************ Public methods **************************************
this.setValue = function (newValue) {
newValue = parseFloat(newValue);
var targetValue = newValue < minValue ? minValue : (newValue > maxValue ? maxValue : newValue);
if (value !== targetValue) {
value = targetValue;
if (value > maxMeasuredValue) {
maxMeasuredValue = value;
}
if (value < minMeasuredValue) {
minMeasuredValue = value;
}
if ((value >= threshold && !ledBlinking && thresholdRising) ||
(value <= threshold && !ledBlinking && !thresholdRising)) {
ledBlinking = true;
blink(ledBlinking);
if (playAlarm) {
audioElement.play();
}
} else if ((value < threshold && ledBlinking && thresholdRising) ||
(value > threshold && ledBlinking && !thresholdRising)) {
ledBlinking = false;
blink(ledBlinking);
if (playAlarm) {
audioElement.pause();
}
}
this.repaint();
}
return this;
};
this.getValue = function () {
return value;
};
this.setOdoValue = function (newValue) {
newValue = parseFloat(newValue);
var targetValue = (newValue < 0 ? 0 : newValue);
if (odoValue !== targetValue) {
odoValue = targetValue;
this.repaint();
}
return this;
};
this.getOdoValue = function () {
return odoValue;
};
this.setValueAnimated = function (newValue, callback) {
newValue = parseFloat(newValue);
var targetValue = (newValue < minValue ? minValue : (newValue > maxValue ? maxValue : newValue)),
gauge = this,
time;
if (value !== targetValue) {
if (undefined !== tween && tween.isPlaying) {
tween.stop();
}
time = fullScaleDeflectionTime * Math.abs(targetValue - value) / (maxValue - minValue);
time = Math.max(time, fullScaleDeflectionTime / 5);
tween = new Tween({}, '', Tween.regularEaseInOut, value, targetValue, time);
//tween = new Tween({}, '', Tween.regularEaseInOut, value, targetValue, 1);
//tween = new Tween(new Object(), '', Tween.strongEaseInOut, value, targetValue, 1);
tween.onMotionChanged = function (event) {
value = event.target._pos;
if ((value >= threshold && !ledBlinking && thresholdRising) ||
(value <= threshold && !ledBlinking && !thresholdRising)) {
ledBlinking = true;
blink(ledBlinking);
if (playAlarm) {
audioElement.play();
}
} else if ((value < threshold && ledBlinking && thresholdRising) ||
(value > threshold && ledBlinking && !thresholdRising)) {
ledBlinking = false;
blink(ledBlinking);
if (playAlarm) {
audioElement.pause();
}
}
if (value > maxMeasuredValue) {
maxMeasuredValue = value;
}
if (value < minMeasuredValue) {
minMeasuredValue = value;
}
if (!repainting) {
repainting = true;
requestAnimFrame(gauge.repaint);
}
};
// do we have a callback function to process?
if (callback && typeof(callback) === "function") {
tween.onMotionFinished = callback;
}
tween.start();
}
return this;
};
this.resetMinMeasuredValue = function () {
minMeasuredValue = value;
this.repaint();
};
this.resetMaxMeasuredValue = function () {
maxMeasuredValue = value;
this.repaint();
return this;
};
this.setMinMeasuredValueVisible = function (visible) {
minMeasuredValueVisible = !!visible;
this.repaint();
return this;
};
this.setMaxMeasuredValueVisible = function (visible) {
maxMeasuredValueVisible = !!visible;
this.repaint();
return this;
};
this.setMaxMeasuredValue = function (newValue) {
newValue = parseFloat(newValue);
var targetValue = newValue < minValue ? minValue : (newValue > maxValue ? maxValue : newValue);
maxMeasuredValue = targetValue;
this.repaint();
return this;
};
this.setMinMeasuredValue = function (newValue) {
newValue = parseFloat(newValue);
var targetValue = newValue < minValue ? minValue : (newValue > maxValue ? maxValue : newValue);
minMeasuredValue = targetValue;
this.repaint();
return this;
};
this.setTitleString = function (title) {
titleString = title;
resetBuffers({background: true});
init({background: true});
this.repaint();
return this;
};
this.setUnitString = function (unit) {
unitString = unit;
resetBuffers({background: true});
init({background: true});
this.repaint();
return this;
};
this.setMinValue = function (newValue) {
minValue = parseFloat(newValue);
resetBuffers({background: true});
init({background: true});
this.repaint();
return this;
};
this.getMinValue = function () {
return minValue;