-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcircularProgress.js
78 lines (63 loc) · 2.09 KB
/
circularProgress.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
const { setSVGAttributes } = require('./../../helper');
class ProgressCircle extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({
mode: 'open'
});
const radius = ProgressCircle.__radius;
const svg = document.createElementNS(ProgressCircle.__svgNameSpace, "svg");
setSVGAttributes(svg, {
style: 'width: 100%; height: 100%;',
viewBox: -50 + " " + -50 + " " + (100) + " " + (100)
});
this.pathElement = document.createElementNS(ProgressCircle.__svgNameSpace, "path");
setSVGAttributes(this.pathElement, {
style: "fill: #555;"
});
svg.append(this.pathElement);
const circleBorder = document.createElementNS(ProgressCircle.__svgNameSpace, "circle");
setSVGAttributes(circleBorder, {
style: "fill: none; stroke: #555; stroke-width: 5px;",
cx: 0,
cy: 0,
r: radius
});
svg.append(circleBorder);
shadow.appendChild(svg);
}
attributeChangedCallback(attribute, oldValue, newValue) {
const attributeHandlers = {
value: value => this.__visualCalc(value),
};
if (attribute in attributeHandlers) {
attributeHandlers[attribute](newValue);
}
}
get value() {
return parseFloat(this.getAttribute("value"));
}
__visualCalc(value) {
const progress = Math.PI * 2 * (value === 1 ? 0.99999999 : value);
this.coords = {
x: Math.sin(progress) * ProgressCircle.__radius,
y: Math.cos(progress) * ProgressCircle.__radius
};
}
set value(value) {
this.__visualCalc(value);
this.setAttribute("value", value);
}
set coords(coords) {
const radius = ProgressCircle.__radius;
this.path = "M 0,0 l 0," + -radius + " A " + radius + " " + radius + " 0 " + (coords.x < 0 & 1) + " 1 " + coords.x + "," + -coords.y;
}
set path(value) {
this.pathElement.setAttributeNS(null, "d", value);
}
}
ProgressCircle.observedAttributes = ["value"];
ProgressCircle.__svgNameSpace = "http://www.w3.org/2000/svg";
ProgressCircle.__radius = 50;
customElements.define("progress-circle", ProgressCircle);
module.exports = ProgressCircle;