forked from vincentlws/canvasProgress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.js
122 lines (101 loc) · 2.39 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
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
var Progress = function(opt) {
this.option = this.extend({
radius: 20,
//文字样式
text: {
font: '10px Arial',
style: '#fff',
alpha: 1
},
//外圈样式
outSideCircle: {
style: '#fff',
alpha: 1,
lineWidth: 2.5
},
//内圈样式
inSideCircle:{
style: '#fff',
alpha: 0.3,
lineWidth: 2
}
}, opt)
this.el = this.option.element;
this.context = this.el.getContext('2d');
this.elWidth = this.el.width;
this.elHeight = this.el.height;
this.hElWidth = this.elWidth / 2;
this.hElHeight = this.elHeight / 2;
}
Progress.prototype = {
constructor: Progress,
extend: function(tar, sub) {
for (var x in sub) {
tar[x] = sub[x];
}
return tar;
},
/**
* 绘制圆形
* @参数 n: 当前进度
*
*/
_drawCircle: function(n) {
var option = this.option,
context = this.context,
pi = Math.PI,
inSideCircle = option.inSideCircle,
outSideCircle = option.outSideCircle;
context.beginPath();
//设置透明度,样式与线条宽度
this.extend(context, {
globalAlpha: inSideCircle.alpha,
strokeStyle: inSideCircle.style,
lineWidth: inSideCircle.lineWidth
})
//绘制圆
context.arc(this.hElWidth, this.hElHeight, option.radius + outSideCircle.lineWidth - inSideCircle.lineWidth, 0, pi * 2, false);
//画出圆形
context.stroke();
context.beginPath();
//设置透明度,样式与线条宽度
this.extend(context, {
globalAlpha: outSideCircle.alpha,
strokeStyle: outSideCircle.style,
lineWidth: outSideCircle.lineWidth
})
//根据当前进度绘制圆
context.arc(this.hElWidth, this.hElHeight, option.radius, -(pi / 2), pi * 2 / 100 * (n - 25), false);
//画出圆形
context.stroke();
},
/**
* 绘制进度
* @参数 n: 当前进度
*/
_drawText: function(n) {
var context = this.context,
number = n + '%',
numberWidth = 0,
textObj = this.option.text;
context.beginPath();
this.extend(context, {
font: textObj.font,
fillStyle: textObj.style,
globalAlpha: textObj.alpha
})
//得出数值宽度
numberWidth = context.measureText(number).width;
context.fillText(number, this.hElWidth - numberWidth / 2, this.hElHeight + 10 / 3)
},
/*
* 绘制进度
* @参数 n: 进度
*/
setProgress: function(n) {
//清除canvas内容
this.context.clearRect(0, 0, this.elWidth, this.elHeight);
this._drawCircle(n);
this._drawText(n);
}
}