-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
98 lines (81 loc) · 1.69 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
/**
* Module dependencies.
*/
var style = require('style');
/**
* Expose `Pie()`.
*/
module.exports = Pie;
/**
* Initialize a new `Pie` with
* an optional css `selector`,
* defaulting to ".pie".
*
* @param {String} selector
* @api public
*/
function Pie(selector) {
if (!(this instanceof Pie)) return new Pie(selector);
selector = selector || '.pie';
this.background = style(selector, 'background-color');
this.borderWidth = parseInt(style(selector, 'border-width'), 10);
this.borderColor = style(selector, 'border-color');
this.color = style(selector, 'color');
this.size(16);
}
/**
* Update percentage to `n`.
*
* @param {Number} n
* @return {Pie}
* @api public
*/
Pie.prototype.update = function(n){
this.percent = n;
return this;
};
/**
* Set size to `n`.
*
* @param {Number} n
* @return {Pie}
* @api public
*/
Pie.prototype.size = function(n){
this._size = n;
return this;
};
/**
* Draw on to `ctx`.
*
* @param {CanvasContext2d} ctx
* @return {Pie}
* @api public
*/
Pie.prototype.draw = function(ctx){
var size = this._size;
var half = size / 2;
var n = this.percent / 100;
var pi = Math.PI * 2;
// clear
ctx.clearRect(0, 0, size, size);
// border
ctx.beginPath();
ctx.moveTo(half, half);
ctx.arc(half, half, half, 0, pi, false);
ctx.fillStyle = this.borderColor;
ctx.fill();
// background
ctx.beginPath();
ctx.moveTo(half, half);
ctx.arc(half, half, half - this.borderWidth, 0, pi, false);
ctx.fillStyle = this.background;
ctx.fill();
// pie
ctx.beginPath();
ctx.moveTo(half, half);
ctx.arc(half, half, half - this.borderWidth, 0, pi * n, false);
ctx.fillStyle = this.color;
ctx.fill();
return this;
};