forked from Mikhus/canvas-gauges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-drawings.html
184 lines (159 loc) · 5.47 KB
/
custom-drawings.html
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
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Gauge Test</title>
<link rel="stylesheet" href="../fonts/fonts.css">
<script src="../gauge.min.js"></script>
</head>
<body style="background: #222">
<button onclick="animateGauges()">Animate</button>
<button onclick="stopGaugesAnimation()">Stop animation</button>
<input type="text" id="gauge-size" value="400">
<button onclick="resize()">Resize</button>
<button onclick="updateUnits()">Change units</button>
<hr>
<canvas data-type="radial-gauge"
data-width="400"
data-height="400"
data-units="°C"
data-title="Temperature"
data-min-value="-50"
data-max-value="50"
data-major-ticks="[-50,-40,-30,-20,-10,0,10,20,30,40,50]"
data-minor-ticks="2"
data-stroke-ticks="true"
data-highlights='[
{"from": -50, "to": 0, "color": "rgba(0,0, 255, .3)"},
{"from": 0, "to": 50, "color": "rgba(255, 0, 0, .3)"}
]'
data-ticks-angle="225"
data-start-angle="67.5"
data-color-major-ticks="#ddd"
data-color-minor-ticks="#ddd"
data-color-title="#eee"
data-color-units="#ccc"
data-color-numbers="#eee"
data-color-plate="#222"
data-border-shadow-width="0"
data-borders="true"
data-needle-type="arrow"
data-needle-width="2"
data-needle-circle-size="7"
data-needle-circle-outer="true"
data-needle-circle-inner="false"
data-animation-duration="1500"
data-animation-rule="linear"
data-color-border-outer="#333"
data-color-border-outer-end="#111"
data-color-border-middle="#222"
data-color-border-middle-end="#111"
data-color-border-inner="#111"
data-color-border-inner-end="#333"
data-color-needle-shadow-down="#333"
data-color-needle-circle-outer="#333"
data-color-needle-circle-outer-end="#111"
data-color-needle-circle-inner="#111"
data-color-needle-circle-inner-end="#222"
data-value-box-border-radius="0"
data-color-value-box-rect="#222"
data-color-value-box-rect-end="#333"
data-font-value="Led"
data-font-numbers="Led"
data-font-title="Led"
data-font-units="Led"
></canvas>
<script>
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(cb) {
var i = 0, s = this.length;
for (; i < s; i++) {
cb && cb(this[i], i, this);
}
}
}
document.fonts && document.fonts.forEach(function(font) {
font.loaded.then(function() {
if (font.family.match(/Led/)) {
document.gauges.forEach(function(gauge) {
gauge.update();
gauge.options.renderTo.style.visibility = 'visible';
});
}
});
});
var timers = [];
function animateGauges() {
document.gauges.forEach(function(gauge) {
timers.push(setInterval(function() {
gauge.value = Math.random() *
(gauge.options.maxValue - gauge.options.minValue) +
gauge.options.minValue;
}, gauge.animation.duration + 50));
});
}
function stopGaugesAnimation() {
timers.forEach(function(timer) {
clearInterval(timer);
});
}
function resize() {
var size = parseFloat(document.getElementById('gauge-size').value) || 400;
document.gauges.forEach(function (gauge) {
gauge.update({ width: size, height: size });
});
}
function updateUnits() {
document.gauges.forEach(function (gauge) {
gauge.update({ units: 'Miles' });
});
}
function setText() {
var text = document.getElementById('gauge-text').value;
document.gauges.forEach(function (gauge) {
gauge.update({ valueText: text });
});
}
window.onload = function() {
// refer gauge
var gauge = document.gauges[0];
// this will draw red or blue circle on a gauge plate depending on
// current value
gauge.on('beforeNeedle', function () {
// getting canvas 2d drawing context
var context = this.canvas.context;
// we can use gauge context special 'max' property which represents
// gauge radius in a real pixels and calculate size of relative pixel
// for our drawing needs
var pixel = context.max / 100;
// step out our circle center coordinate by 30% of its radius from
// gauge center
var centerX = 30 * pixel;
// stay in center by Y-coordinate
var centerY = 0;
// use circle radius equal to 5%
var radius = 5 * pixel;
// save previous context state
context.save();
// draw circle using canvas JS API
context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
var gradient = context.createRadialGradient(
centerX, centerY, 0,
centerX, centerY, radius);
gradient.addColorStop(0, this.options.value <= 0 ? '#aaf' : '#faa');
gradient.addColorStop(0.82, this.options.value <= 0 ? '#00f' : '#f00');
gradient.addColorStop(1, this.options.value <= 0 ? '#88a' : '#a88');
context.fillStyle = gradient;
context.fill();
context.closePath();
// restore previous context state to prevent break of
// further drawings
context.restore();
});
// redraw the gauge if it has been already drawn
gauge.draw();
}
</script>
</body>
</html>