This repository has been archived by the owner on Jul 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HPgauge.js
194 lines (159 loc) · 5.64 KB
/
HPgauge.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
function HPgauge(canvas, options) {
this.options = Util.extend({}, HPgauge.defaultOptions, options);
this.canvas = canvas;
this.value = 1.0;
this.curvalue = 0.0
this.upincrementer = .1;
this.dnincrementer = .1;
//this.redraw(canvas)
this.animate();
};
HPgauge.defaultOptions = {
range_high: 250,
range_low: 0,
warn_high: 200,
warn_low: 100,
norm_high: 100,
norm_low: 0,
ideal_high: 75,
ideal_low: 35,
col_warn: "#6c6c6c",
col_norm: "#FFF",
col_ideal: "#00ffd1",
col_marker: "#000",
col_alarm: "#F00",
};
HPgauge.prototype.animate = function() {
//requestAnimationFrame(this.animate.bind(this));
requestAnimationFrame(() => { this.animate() } );
this.redraw(this.canvas);
}
HPgauge.prototype.update = function(value) {
this.value= value;
//this.redraw(this.canvas)
};
HPgauge.prototype.redraw = function(canvas) {
var ctx = this.canvas.getContext("2d");
ctx.fillStyle = "#000";
ctx.clearRect(0, 0, canvas.width, canvas.height);
startwidth = Math.round((this.canvas.width/2))
startheight = Math.round((this.canvas.height/10)*1)
endheight = Math.round((this.canvas.height/10)*9)
barwidth = this.canvas.width * 0.3;
barheight = endheight - startheight;
bar_bottom = startheight + barheight;
range = this.options.range_high - this.options.range_low;
pixelsperinc = barheight / (this.options.range_high - this.options.range_low);
warn_start = bar_bottom - (this.options.warn_high * pixelsperinc);
warn_height = (bar_bottom - (this.options.warn_low * pixelsperinc)) - warn_start
normal_start = bar_bottom - (this.options.norm_high * pixelsperinc);
normal_height = (bar_bottom - (this.options.norm_low * pixelsperinc)) - normal_start
ideal_start = bar_bottom - (this.options.ideal_high * pixelsperinc);
ideal_height = (bar_bottom - (this.options.ideal_low * pixelsperinc)) - ideal_start
marker_pos = bar_bottom - (this.curvalue) * pixelsperinc;
markerwidth = Math.round((this.canvas.width/5)*2)
markerheight = Math.round((this.canvas.height/10))
//Draw Basic Bar
ctx.fillRect(startwidth,startheight-1,barwidth,barheight+2);
//Draw Warning Range
ctx.fillStyle=this.options.col_warn;
ctx.fillRect(startwidth+1,warn_start,barwidth-2,warn_height);
//Draw Normal Range
ctx.fillStyle=this.options.col_norm;
ctx.fillRect(startwidth+1,normal_start,barwidth-2,normal_height);
//Draw Ideal Range
ctx.fillStyle=this.options.col_ideal;
ctx.fillRect(startwidth+1,ideal_start,barwidth-2,ideal_height);
//Draw Triangle
this.drawmarker(marker_pos)
//drawtext
this.drawtext(this.curvalue, startwidth,startheight * 9.8)
//drawalarm
if(this.value > this.options.norm_high || this.value < this.options.norm_low){
this.drawalarm(1);
}
if(this.curvalue < this.value){
dist = this.value - this.curvalue
this.curvalue += dist / 10
}
if(this.curvalue > this.value){
dist = this.curvalue - this.value
this.curvalue -= dist / 10
}
if (dist < 0.1 ){
this.curvalue = this.value
}
};
HPgauge.prototype.drawmarker = function(marker_pos) {
var ctx = this.canvas.getContext("2d");
ctx.fillStyle=this.options.col_marker;
startpoint_h = startwidth - 2
startpoint_v = marker_pos
ctx.beginPath();
ctx.moveTo(startpoint_h, startpoint_v);
ctx.lineTo(startpoint_h - markerwidth, startpoint_v + (markerheight/2));
ctx.lineTo(startpoint_h - markerwidth, startpoint_v - (markerheight/2));
ctx.lineTo(startpoint_h, startpoint_v);
ctx.fill();
};
HPgauge.prototype.drawalarm = function(level) {
var ctx = this.canvas.getContext("2d");
alarmwidth = Math.round(barwidth*1.4);
alarmheight = Math.round(barwidth*1.4);
startpoint_h = Math.round(startwidth + (barwidth/2) - (alarmwidth/2))
startpoint_v = Math.round(startheight*0.2)
ctx.fillStyle=this.options.col_alarm;
ctx.strokeStyle="#000";
var fontsize = this.canvas.width * 0.25
ctx.font = (fontsize|0)+ "px Arial";
ctx.textAlign = "center";
console.log(startpoint_h, startpoint_v)
ctx.beginPath();
ctx.moveTo(startpoint_h, startpoint_v+alarmheight);
ctx.lineTo(startpoint_h + alarmwidth, startpoint_v+alarmheight);
ctx.lineTo(startpoint_h + (alarmwidth/2), startpoint_v);
ctx.lineTo(startpoint_h , startpoint_v+alarmheight);
//ctx.lineTo(startpoint_h + alarmwidth, startpoint_v);
//ctx.lineTo(startpoint_h + alarmwidth, startpoint_v + alarmheight);
//ctx.lineTo(startpoint_h , startpoint_v + alarmheight);
//ctx.lineTo(startpoint_h , startpoint_v);
ctx.strokeStyle="#000";
ctx.fill();
ctx.stroke();
ctx.lineWidth=2;
ctx.textAlign = "center";
ctx.fillStyle="#000";
ctx.fillText("!",startwidth + (barwidth/2)+1,startheight*0.75);
};
HPgauge.prototype.drawtext = function(value, x,y) {
var ctx = this.canvas.getContext("2d");
var fontsize = this.canvas.width * 0.3
ctx.font = (fontsize|0)+"px Arial";
ctx.textAlign = "center";
ctx.fillStyle="#0012ff";
ctx.fillText(value.toFixed(2),x,y);
};
var Util = {
extend: function() {
arguments[0] = arguments[0] || {};
for (var i = 1; i < arguments.length; i++)
{
for (var key in arguments[i])
{
if (arguments[i].hasOwnProperty(key))
{
if (typeof(arguments[i][key]) === 'object') {
if (arguments[i][key] instanceof Array) {
arguments[0][key] = arguments[i][key];
} else {
arguments[0][key] = Util.extend(arguments[0][key], arguments[i][key]);
}
} else {
arguments[0][key] = arguments[i][key];
}
}
}
}
return arguments[0];
},
};