-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprogress.js
93 lines (75 loc) · 1.58 KB
/
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
/**
* Module dependencies.
*/
var autoscale = require('autoscale-canvas');
/**
* Expose `Progress`.
*/
module.exports = Progress;
/**
* Initialize a new `Progress` indicator.
*/
function Progress() {
this.percent = 0;
this.el = document.createElement('canvas');
this.ctx = this.el.getContext('2d');
this.color = '#00bbff';
this.shadowColor = 'rgba(0, 187, 255, 0.3)';
this.fontSize = 12;
this.font = 'helvetica, arial, sans-serif';
this.size(52);
}
/**
* Set progress size to `n`.
*
* @param {Number} n
* @return {Progress}
* @api public
*/
Progress.prototype.size = function(n){
this.el.width = n;
this.el.height = n;
autoscale(this.el);
return this;
};
/**
* Update percentage to `n`.
*
* @param {Number} n
* @return {Progress}
* @api public
*/
Progress.prototype.update = function(n){
this.percent = n;
this.draw(this.ctx);
return this;
};
/**
* Draw on `ctx`.
*
* @param {CanvasRenderingContext2d} ctx
* @return {Progress}
* @api private
*/
Progress.prototype.draw = function(ctx){
var percent = Math.min(this.percent, 100)
, ratio = window.devicePixelRatio || 1
, size = this.el.width / ratio
, half = size / 2
, x = half
, y = half
, rad = half - 1
, fontSize = this.fontSize;
ctx.font = fontSize + 'px ' + this.font;
var angle = Math.PI * 2 * (percent / 100);
ctx.clearRect(0, 0, size, size);
// shadow
ctx.shadowColor = this.shadowColor;
ctx.shadowBlur = 10;
// outer circle
ctx.strokeStyle = this.color;
ctx.beginPath();
ctx.arc(x, y, rad, 0, angle, false);
ctx.stroke();
return this;
};