forked from qiao/ProgressCircle.js
/
ProgressCircle.js
305 lines (255 loc) · 10.2 KB
/
ProgressCircle.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
(function(window, document, undefined) {
/**
* Find the absolute position of an element
*/
var absPos = function(element) {
var offsetLeft, offsetTop;
offsetLeft = offsetTop = 0;
if (element.offsetParent) {
do {
offsetLeft += element.offsetLeft;
offsetTop += element.offsetTop;
} while (element = element.offsetParent);
}
return [offsetLeft, offsetTop];
};
/**
* @constructor Progress Circle class
* @param params.canvas Canvas on which the circles will be drawn.
* @param params.minRadius Inner radius of the innermost circle, in px.
* @param params.arcWidth Width of each circle(to be more accurate, ring).
* @param params.gapWidth Space between each circle.
* @param params.centerX X coordinate of the center of circles.
* @param params.centerY Y coordinate of the center of circles.
* @param params.infoLineBaseAngle Base angle of the info line.
* @param params.infoLineAngleInterval Angles between the info lines.
*/
var ProgressCircle = function(params) {
this.canvas = params.canvas;
this.minRadius = params.minRadius || 15;
this.arcWidth = params.arcWidth || 5;
this.gapWidth = params.gapWidth || 3;
this.centerX = params.centerX || this.canvas.width / 2;
this.centerY = params.centerY || this.canvas.height / 2;
this.infoLineLength = params.infoLineLength || 60;
this.horizLineLength = params.horizLineLength || 10;
this.infoLineAngleInterval = params.infoLineAngleInterval || Math.PI / 8;
this.infoLineBaseAngle = params.infoLineBaseAngle || Math.PI / 6;
this.context = this.canvas.getContext('2d');
this.width = this.canvas.width;
this.height = this.canvas.height;
this.circles = [];
this.runningCount = 0;
};
ProgressCircle.prototype = {
constructor: ProgressCircle,
/**
* @method Adds an progress monitor entry.
* @param params.fillColor Color to fill in the circle.
* @param params.outlineColor Color to outline the circle.
* @param params.progressListener Callback function to fetch the progress.
* @param params.infoListener Callback function to fetch the info.
* @returns this
*/
addEntry: function(params) {
this.circles.push(new Circle({
canvas: this.canvas,
context: this.context,
centerX: this.centerX,
centerY: this.centerY,
innerRadius: this.minRadius + this.circles.length *
(this.gapWidth + this.arcWidth),
arcWidth: this.arcWidth,
infoLineLength: this.infoLineLength,
horizLineLength: this.horizLineLength,
id: this.circles.length,
fillColor: params.fillColor,
outlineColor: params.outlineColor,
progressListener: params.progressListener,
infoListener: params.infoListener,
infoLineAngle: this.infoLineBaseAngle +
this.circles.length * this.infoLineAngleInterval,
}));
return this;
},
/**
* @method Starts the monitor and updates with the given interval.
* @param interval Interval between updates, in millisecond.
* @returns this
*/
start: function(interval) {
var self = this;
this.timer = setInterval(function() {
self._update();
}, interval || 33);
return this;
},
/**
* @method Manually update all circles
*/
update: function(value) {
this._update(value);
},
/**
* @method Stop the animation.
*/
stop: function() {
clearTimeout(this.timer);
},
/**
* @private
* @method Call update on each circle and redraw them.
* @param value Value to be passed to circle update method (in case of manual update)
* @returns this
*/
_update: function(value) {
this._clear();
this.circles.forEach(function(circle, idx, array) {
circle.update(value);
});
return this;
},
/**
* @private
* @method Clear the canvas.
* @returns this
*/
_clear: function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
return this;
},
};
/**
* @private
* @class Individual progress circle.
* @param params.canvas Canvas on which the circle will be drawn.
* @param params.context Context of the canvas.
* @param params.innerRadius Inner radius of the circle, in px.
* @param params.arcWidth Width of each arc(circle).
* @param params.gapWidth Distance between each arc.
* @param params.centerX X coordinate of the center of circles.
* @param params.centerY Y coordinate of the center of circles.
* @param params.fillColor Color to fill in the circle.
* @param params.outlineColor Color to outline the circle.
* @param params.progressListener Callback function to fetch the progress.
* @param params.infoListener Callback function to fetch the info.
* @param params.infoLineAngle Angle of info line.
*/
var Circle = function(params) {
this.id = params.id;
this.canvas = params.canvas;
this.context = params.context;
this.centerX = params.centerX;
this.centerY = params.centerY;
this.arcWidth = params.arcWidth;
this.innerRadius = params.innerRadius || 0;
this.fillColor = params.fillColor || '#fff';
this.outlineColor = params.outlineColor || this.fillColor;
this.progressListener = params.progressListener;
this.infoLineLength = params.infoLineLength || 250;
this.horizLineLength = params.horizLineLength || 50;
this.infoListener = params.infoListener;
this.infoLineAngle = params.infoLineAngle;
this.outerRadius = this.innerRadius + this.arcWidth;
// If the info listener is not registered, then don't calculate
// the related coordinates
if (!this.infoListener) return;
// calculate the info-line turning points
var angle = this.infoLineAngle,
arcDistance = (this.innerRadius + this.outerRadius) / 2,
sinA = Math.sin(angle),
cosA = Math.cos(angle);
this.infoLineStartX = this.centerX + sinA * arcDistance;
this.infoLineStartY = this.centerY - cosA * arcDistance;
this.infoLineMidX = this.centerX + sinA * this.infoLineLength;
this.infoLineMidY = this.centerY - cosA * this.infoLineLength;
this.infoLineEndX = this.infoLineMidX +
(sinA < 0 ? -this.horizLineLength : this.horizLineLength);
this.infoLineEndY = this.infoLineMidY;
var infoText = document.createElement('div'),
style = infoText.style;
style.color = this.fillColor;
style.position = 'absolute';
style.left = this.infoLineEndX + absPos(this.canvas)[0] + 'px';
// style.top will be calculated in the `drawInfo` method. Since
// user may want to change the size of the font, so the top offset
// must be updated in each loop.
infoText.className = 'ProgressCircleInfo'; // For css styling
infoText.id = 'progress_circle_info_' + this.id;
document.body.appendChild(infoText);
this.infoText = infoText;
};
Circle.prototype = {
constructor: Circle,
update: function(value) {
this.progress = value || this.progressListener();
this._draw();
if (this.infoListener) {
this.info = this.infoListener();
this._drawInfo();
}
},
/**
* @private
* @method Draw the circle on the canvas.
* @returns this
*/
_draw: function() {
var ctx = this.context,
ANGLE_OFFSET = -Math.PI / 2,
startAngle = 0 + ANGLE_OFFSET,
endAngle= startAngle + this.progress * Math.PI * 2,
x = this.centerX,
y = this.centerY,
innerRadius = this.innerRadius,
outerRadius = this.outerRadius;
ctx.fillStyle = this.fillColor;
ctx.strokeStyle = this.outlineColor;
ctx.beginPath();
ctx.arc(x, y, innerRadius, startAngle, endAngle, false);
ctx.arc(x, y, outerRadius, endAngle, startAngle, true);
ctx.closePath();
ctx.stroke();
ctx.fill();
return this;
},
/**
* @private
* @method Draw the info lines and info text.
* @returns this
*/
_drawInfo: function() {
var pointList, lineHeight;
pointList = [
[this.infoLineStartX, this.infoLineStartY],
[this.infoLineMidX, this.infoLineMidY],
[this.infoLineEndX, this.infoLineEndY],
];
this._drawSegments(pointList, false);
this.infoText.innerHTML = this.info;
lineHeight = this.infoText.offsetHeight;
this.infoText.style.top = this.infoLineEndY +
absPos(this.canvas)[1] - lineHeight / 2 + 'px';
return this;
},
/**
* @private
* @method Helper function to draw the segments
* @param pointList An array of points in the form of [x, y].
* @param close Whether to connect the first and last point.
*/
_drawSegments: function(pointList, close) {
var ctx = this.context;
ctx.beginPath();
ctx.moveTo(pointList[0][0], pointList[0][1]);
for (var i = 1; i < pointList.length; ++i) {
ctx.lineTo(pointList[i][0], pointList[i][1]);
}
if (close) {
ctx.closePath();
}
ctx.stroke();
},
};
window.ProgressCircle = ProgressCircle;
})(window, document);