forked from StartPolymer/s-circle-progress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths-circle-progress.js
170 lines (135 loc) · 3.43 KB
/
s-circle-progress.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
/**
`s-circle-progress`
Polymer-based web component displaying a circular progress bar.
@demo demo/index.html
*/
/*
FIXME(polymer-modulizer): the above comments were extracted
from HTML and may be out of place here. Review them and
then delete this comment!
*/
import '@polymer/polymer/polymer-legacy.js';
import '@polymer/iron-flex-layout/iron-flex-layout.js';
import { IronResizableBehavior } from '@polymer/iron-resizable-behavior/iron-resizable-behavior.js';
import { Polymer } from '@polymer/polymer/lib/legacy/polymer-fn.js';
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
Polymer({
_template: html`
<style>
:host {
@apply(--layout-vertical);
@apply(--layout-center-center);
position: relative;
width: 64px;
height: 64px;
margin: 24px;
border-radius: 50%;
}
svg {
position: absolute;
top: 0;
left: 0;
display: none;
}
.Circle-background {
stroke: var(--s-circle-progress-bg-stroke-color, var(--paper-grey-100));
}
.Circle-foreground {
transition: stroke-dashoffset 150ms;
stroke: var(--s-circle-progress-stroke-color, var(--accent-color));
stroke-linecap: var(--s-circle-progress-stroke-linecap, round);
}
slot::slotted(*) {
text-align: center;
}
</style>
<svg id="circle" width="100%" height="100%">
<circle class="Circle-background" r\$="[[_radius]]" cx\$="[[_cx]]" cy\$="[[_cy]]" fill="transparent" stroke-width\$="[[strokeWidth]]"></circle>
<circle class="Circle-foreground" r\$="[[_radius]]" cx\$="[[_cx]]" cy\$="[[_cy]]" fill="transparent" stroke-width\$="[[strokeWidth]]" stroke-dasharray\$="[[_dasharray]]" stroke-dashoffset\$="[[_dashoffset]]" transform\$="[[_transform]]"></circle>
</svg>
<slot></slot>
`,
is: 's-circle-progress',
behaviors: [
IronResizableBehavior
],
properties: {
/**
* Value of circular progress bar.
*/
value: {
type: Number,
value: 0
},
/**
* Maximum of value.
*/
max: {
type: Number,
value: 100
},
/**
* Stroke width of circle.
*/
strokeWidth: {
type: Number,
value: 4
},
/**
* Starting angle of the progress.
*/
angle: {
type: Number,
value: -90
},
_cx: {
type: Number,
value: null
},
_cy: {
type: Number,
value: null
},
_radius: {
type: Number,
computed: '_computeRadius(_cx, _cy, strokeWidth)'
},
_transform: {
type: String,
computed: '_computeTransform(angle, _cx, _cy)'
},
_dasharray: {
type: Number,
computed: '_computeDashArray(_radius)'
},
_dashoffset: {
type: Number,
computed: '_computeDashOffset(value, max, _dasharray)'
}
},
listeners: {
"iron-resize": "_onIronResize"
},
attached() {
this.async(this.notifyResize, 1);
},
_computeDashArray: function(radius) {
return 2 * Math.PI * radius;
},
_computeDashOffset: function(value, max, dasharray) {
return (1 - value / max) * dasharray;
},
_computeRadius: function(cx, cy, strokeWidth) {
return cx && cy ? Math.max(0, Math.min(cx, cy) - strokeWidth / 2) : 0;
},
_computeTransform: function(angle, cx, cy) {
return cx && cy ? 'rotate(' + angle + ', ' + cx + ', ' + cy + ')' : '';
},
_onIronResize: function() {
if (this.offsetWidth && this.offsetHeight) {
this._cx = this.offsetWidth / 2;
this._cy = this.offsetHeight / 2;
this.$.circle.style.display = 'block';
}
}
});