-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery-pie-loader.js
150 lines (122 loc) · 4.4 KB
/
jquery-pie-loader.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
/**
* @author Antonin Cezard <anc@ecedi.fr>
* @license http://www.opensource.org/licenses/MIT MIT
* jQuery Pie-Loader Plugin 0.1
*
* Based on jQuery Boilerplate by Zeno Rocha with the help of Addy Osmani
* http://jqueryboilerplate.com
*
* Licensed under the MIT license:
*
*/
/* global jQuery */
(function($) {
'use strict';
// Create the defaults once
var pluginName = 'svgPie',
defaults = {
easing: 'easeOutCubic',
dimension: 200,
percentage: 50,
duration: 2000,
onStart: function() {},
onComplete: function() {}
};
// The actual plugin constructor
function Plugin(element, options) {
this.element = element;
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
// Custom easing function borrowed from jQuery-UI
$.extend($.easing, {
easeOutCubic: function(x, t, b, c, d) {
return c * ((t = t / d - 1) * t * t + 1) + b;
}
});
// Avoid Plugin.prototype conflicts
$.extend(Plugin.prototype, {
// Initialization logic
init: function() {
$(this.element).css({
'width': this.settings.dimension + 'px',
'height': this.settings.dimension + 'px'
});
this.createSvg();
this.animateNumber();
this.animateStrokeDasharray();
$(this.element).addClass('rendered');
},
// SVG pie markup rendering
createSvg: function() {
var half = this.settings.dimension / 2;
var quarter = this.settings.dimension / 4;
var area = Math.PI * 2 * quarter;
var svg =
'<svg xmlns:svg="http://www.w3.org/2000/svg"' +
'xmlns="http://www.w3.org/2000/svg"' +
'>' +
'<circle r="' + half +
'" cx="' + half +
'" cy="' + half +
'"/>' +
'<circle r="' + (quarter + 0.5) + // +0.5 to debug non-webkit based browsers
'" cx="' + half +
'" cy="' + half + '"' +
'style="stroke-width:' + half + 'px;' +
'stroke-dasharray:' + '0px' + ' ' + area + ';' +
'"/>' +
'</svg>' +
'<div class="percentage"' +
'></div>';
$(this.element).prepend(svg);
},
// Number animation
animateNumber: function() {
var $target = $(this.element).find('.percentage');
$({
percentageValue: 0
}).animate({
percentageValue: this.settings.percentage
}, {
duration: this.settings.duration,
easing: this.settings.easing,
start: this.settings.onStart,
step: function() {
// Update the element's text with rounded-up value:
$target.text(Math.round(this.percentageValue) + '%');
},
complete: this.settings.onComplete
});
},
// Pie animation
animateStrokeDasharray: function() {
var debug = this.settings.percentage >= 100 ? 1 : 0; // to debug non webkit browsers
var area = 2 * Math.PI * ((this.settings.dimension / 4) + 0.4); // +0.4 to debug non webkit browsers
var strokeEndValue = (this.settings.percentage + debug) * area / 100;
var $target = $(this.element).find('svg circle:nth-child(2)');
$({
strokeValue: 0
}).animate({
strokeValue: strokeEndValue
}, {
duration: this.settings.duration,
easing: this.settings.easing,
step: function() {
$target.css('stroke-dasharray', this.strokeValue + 'px' + ' ' + area + 'px');
}
});
}
});
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function(options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
});
};
})(jQuery);