-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeter_widget.js
55 lines (48 loc) · 1.17 KB
/
meter_widget.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
/*
* Copyright 2018 Peter Magnusson <kmpm@birchroad.net>
*/
export class MeterWidget {
constructor(element, options = {}) {
this._angle = 0;
this._value = 0;
this.element = element;
this.options = options;
this.arc = element.getElementById('arc');
this.image = this.element.getElementById('image');
this.goalValue = options.goalValue || 360;
this.onChange = options.onChange;
this._color = this.arc.style.fill;
}
setAngle(angle) {
if (angle === this._angle) {
return;
}
this._angle = angle;
if (this._angle > 360) {
this.setAngle(this._angle - 360);
}
this.arc.sweepAngle = this._angle;
if (typeof this.onChange === 'function') {
this.onChange(this, this._angle, this._value);
}
}
getAngle() {
return this._angle;
}
setValue(value) {
this._value = value;
let angle = Math.floor(360 * (value / this.goalValue));
if (angle > 360) {
angle = 360;
}
this.setAngle(angle);
}
setColor(color) {
if (color === this._color) {
return;
}
this._color = color;
this.arc.style.fill = color;
this.image.style.fill = color;
}
}