forked from component/progress
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
157 lines (130 loc) · 2.72 KB
/
index.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
/**
* Module dependencies.
*/
var autoscale = require('autoscale-canvas');
var raf = require('raf');
/**
* Expose `Spinner`.
*/
module.exports = Spinner;
/**
* Initialize a new `Spinner`.
*/
function Spinner() {
this.percent = 0;
this.el = document.createElement('canvas');
this.ctx = this.el.getContext('2d');
this.size(50);
this.fontSize(11);
this.speed(60);
this.font('helvetica, arial, sans-serif');
var self = this;
(function animate() {
raf(animate);
self.percent = (self.percent + self._speed / 36) % 100;
self.draw(self.ctx);
})();
}
/**
* Set spinner size to `n`.
*
* @param {Number} n
* @return {Spinner}
* @api public
*/
Spinner.prototype.size = function(n){
this.el.width = n;
this.el.height = n;
autoscale(this.el);
return this;
};
/**
* Set text to `str`.
*
* @param {String} str
* @return {Spinner}
* @api public
*/
Spinner.prototype.text = function(str){
this._text = str;
return this;
};
/**
* Set font size to `n`.
*
* @param {Number} n
* @return {Spinner}
* @api public
*/
Spinner.prototype.fontSize = function(n){
this._fontSize = n;
return this;
};
/**
* Set font `family`.
*
* @param {String} family
* @return {Spinner}
* @api public
*/
Spinner.prototype.font = function(family){
this._font = family;
return this;
};
/**
* Set speed to `n` rpm.
*
* @param {Number} n
* @return {Spinner}
* @api public
*/
Spinner.prototype.speed = function(n) {
this._speed = n;
return this;
}
/**
* Draw on `ctx`.
*
* @param {CanvasRenderingContext2d} ctx
* @return {Spinner}
* @api private
*/
Spinner.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);
// outer circle
var grad = ctx.createLinearGradient(
half + Math.sin(Math.PI * 1.5 - angle) * half,
half + Math.cos(Math.PI * 1.5 - angle) * half,
half + Math.sin(Math.PI * 0.5 - angle) * half,
half + Math.cos(Math.PI * 0.5 - angle) * half
);
grad.addColorStop(0, "rgba(0, 0, 0, 0)");
grad.addColorStop(1, 'rgba(0, 0, 0, 1)');
ctx.strokeStyle = grad;
ctx.beginPath();
ctx.arc(x, y, rad, angle - Math.PI, angle, false);
ctx.stroke();
// inner circle
ctx.strokeStyle = '#eee';
ctx.beginPath();
ctx.arc(x, y, rad - 1, 0, Math.PI * 2, true);
ctx.stroke();
// text
var text = this._text || ''
, w = ctx.measureText(text).width;
ctx.fillText(
text
, x - w / 2 + 1
, y + fontSize / 2 - 1);
return this;
};