1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | module.exports = Progress;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function Progress () {
|
13 | this.percent = 0;
|
14 | this.size(0);
|
15 | this.fontSize(11);
|
16 | this.font('helvetica, arial, sans-serif');
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | Progress.prototype.size = function (size) {
|
27 | this._size = size;
|
28 | return this;
|
29 | };
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | Progress.prototype.text = function (text) {
|
39 | this._text = text;
|
40 | return this;
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | Progress.prototype.fontSize = function (size) {
|
51 | this._fontSize = size;
|
52 | return this;
|
53 | };
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | Progress.prototype.font = function (family) {
|
62 | this._font = family;
|
63 | return this;
|
64 | };
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | Progress.prototype.update = function (n) {
|
73 | this.percent = n;
|
74 | return this;
|
75 | };
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | Progress.prototype.draw = function (ctx) {
|
84 | try {
|
85 | var percent = Math.min(this.percent, 100);
|
86 | var size = this._size;
|
87 | var half = size / 2;
|
88 | var x = half;
|
89 | var y = half;
|
90 | var rad = half - 1;
|
91 | var fontSize = this._fontSize;
|
92 |
|
93 | ctx.font = fontSize + 'px ' + this._font;
|
94 |
|
95 | var angle = Math.PI * 2 * (percent / 100);
|
96 | ctx.clearRect(0, 0, size, size);
|
97 |
|
98 |
|
99 | ctx.strokeStyle = '#9f9f9f';
|
100 | ctx.beginPath();
|
101 | ctx.arc(x, y, rad, 0, angle, false);
|
102 | ctx.stroke();
|
103 |
|
104 |
|
105 | ctx.strokeStyle = '#eee';
|
106 | ctx.beginPath();
|
107 | ctx.arc(x, y, rad - 1, 0, angle, true);
|
108 | ctx.stroke();
|
109 |
|
110 |
|
111 | var text = this._text || (percent | 0) + '%';
|
112 | var w = ctx.measureText(text).width;
|
113 |
|
114 | ctx.fillText(text, x - w / 2 + 1, y + fontSize / 2 - 1);
|
115 | } catch (err) {
|
116 |
|
117 | }
|
118 | return this;
|
119 | };
|