circle.js
4.7 KB
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
// var $ = require('jquery');
// require(['jquery'], function($){
var Circle = {
initialize: function() {
this.render();
},
render: function() {
var _this = this;
$('span[data-circle]').each(function() {
var c = new _circle($(this));
c.initialize();
});
}
};
function _circle(selector) {
// var opt = {
// countdown: $.trim(selector.attr('data-circle')) == 'countdown',
// min: +$.trim(selector.attr('data-min')),
// max: +$.trim(selector.attr('data-max')),
// init: +$.trim(selector.attr('data-init')),
// r: +$.trim(selector.attr('data-r')),
// lineWidth: +$.trim(selector.attr('data-lineWidth')),
// hColor: $.trim(selector.attr('data-hColor')),
// percent: $.trim(selector.attr('data-percent')),
// };
var opt = selector.data();
console.log(opt);
var defaults = {
countdown: false,
min: 0,
r: 45, //半径
linewidth: 6,
color: '#e2e2e2',
fontsize: 26,
hcolor: '#69b8dc',
max: 100,
init: 0,
current: 0,
percent: 0
};
this.selector = selector;
this.settings = $.extend(true, defaults, opt);
this.settings.current = this.settings.init;
this.settings.countdown = this.settings.circle > 0 ? true: false;
}
_circle.prototype = {
initialize: function() {
var canvas = document.createElement('canvas');
canvas.setAttribute('width', this.settings.r * 2);
canvas.setAttribute('height', this.settings.r * 2);
var ctx = canvas.getContext('2d');
this.canvas = canvas;
this.show();
this.ctx = ctx;
var _this = this;
if (this.settings.countdown) {
this.timer = setInterval(function() {
_this.countdown();
}, 1000);
}
this.bindEvents();
this.render();
},
bindEvents: function(){
var _this = this;
if(this.settings.input){
$(_this.settings.input).on('input', function(e){
var number = $.trim($(this).val());
number = number.replace(/^\.*$/, '');
number = number.replace(/[^0-9\.]/g, '');
number = number.replace(/(([1-9]\d*(\.\d{0,2})?)|0(\.\d{0,2})?).*/, "$1");
if(+number > _this.settings.max){
number = _this.settings.max;
}
$(this).val(number);
_this.settings.current = +number;
_this.render();
});
}
},
show: function() {
this.selector.append(this.canvas);
},
render: function() {
this.update();
},
countdown: function() {
if (this.settings.current <= 0) {
clearInterval(this.timer);
this.selector.trigger('circleFinish');
return;
}
this.settings.current -= 1;
this.render();
},
update: function() {
var ctx = this.ctx;
ctx.clearRect(0, 0, this.settings.r * 2, this.settings.r * 2);
ctx.beginPath();
ctx.strokeStyle = this.settings.color;
ctx.lineWidth = this.settings.linewidth;
ctx.arc(this.settings.r, this.settings.r, this.settings.r - this.settings.linewidth, 0, Math.PI * 2, true);
ctx.stroke();
// 设置字体
ctx.font = "normal "+ this.settings.fontsize+"px Arial";
// 设置对齐方式
ctx.textAlign = "center";
// 设置填充颜色
ctx.fillStyle = this.settings.hcolor;
var text = Math.floor(this.settings.current * 100 / this.settings.max);
var y = this.settings.r + 11;
if(this.settings.percent > 0){
text += '%';
y = this.settings.r + 7;
}
// 设置字体内容,以及在画布上的位置
ctx.fillText(text, this.settings.r, y);
// 绘制空心字
// ctx.strokeText(this.settings.init, this.settings.r, this.settings.r + 8);
var ctx = this.ctx;
ctx.beginPath();
ctx.strokeStyle = this.settings.hcolor;
var end = -this.settings.current / this.settings.max * Math.PI * 2 - Math.PI / 180 * 90;
ctx.arc(this.settings.r, this.settings.r, this.settings.r - this.settings.linewidth, -Math.PI / 180 * 90, end, true);
ctx.stroke();
}
};
// component
if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
// AMD. Register as an anonymous module.
define(function() {
return Circle;
});
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = Circle;
} else {
window.Circle = Circle;
}