| 12990 |
anikendra |
1 |
;(function ($, window, document, undefined) {
|
|
|
2 |
var pluginName = "countdown360",
|
|
|
3 |
defaults = {
|
|
|
4 |
radius: 15.5, // radius of arc
|
|
|
5 |
strokeStyle: "#477050", // the color of the stroke
|
|
|
6 |
strokeWidth: undefined, // the stroke width, dynamically calulated if omitted in options
|
|
|
7 |
fillStyle: "#8ac575", // the fill color
|
|
|
8 |
fontColor: "#477050", // the font color
|
|
|
9 |
fontFamily: "sans-serif", // the font family
|
|
|
10 |
fontSize: undefined, // the font size, dynamically calulated if omitted in options
|
|
|
11 |
fontWeight: 700, // the font weight
|
|
|
12 |
autostart: true, // start the countdown automatically
|
|
|
13 |
seconds: 10, // the number of seconds to count down
|
|
|
14 |
onComplete: undefined
|
|
|
15 |
};
|
|
|
16 |
|
|
|
17 |
function Plugin(element, options) {
|
|
|
18 |
this.element = element;
|
|
|
19 |
this.settings = $.extend({}, defaults, options);
|
|
|
20 |
if (!this.settings.fontSize) { this.settings.fontSize = this.settings.radius/1.2; }
|
|
|
21 |
if (!this.settings.strokeWidth) { this.settings.strokeWidth = this.settings.radius/4; }
|
|
|
22 |
this._defaults = defaults;
|
|
|
23 |
this._name = pluginName;
|
|
|
24 |
this._init();
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
Plugin.prototype = {
|
|
|
28 |
|
|
|
29 |
start: function () {
|
|
|
30 |
this.startedAt = new Date();
|
|
|
31 |
this._drawCountdownShape(Math.PI*3.5, true);
|
|
|
32 |
this._drawCountdownLabel(0);
|
|
|
33 |
this.interval = setInterval(jQuery.proxy(this._draw, this), 1000);
|
|
|
34 |
},
|
|
|
35 |
|
|
|
36 |
stop: function (cb) {
|
|
|
37 |
clearInterval(this.interval);
|
|
|
38 |
if (cb) { cb(); }
|
|
|
39 |
},
|
|
|
40 |
|
|
|
41 |
_init: function () {
|
|
|
42 |
this.settings.width = (this.settings.radius * 2) + (this.settings.strokeWidth * 2);
|
|
|
43 |
this.settings.height = this.settings.width;
|
|
|
44 |
this.settings.arcX = this.settings.radius + this.settings.strokeWidth;
|
|
|
45 |
this.settings.arcY = this.settings.arcX;
|
|
|
46 |
this._initPen(this._getCanvas());
|
|
|
47 |
if (this.settings.autostart) { this.start(); }
|
|
|
48 |
},
|
|
|
49 |
|
|
|
50 |
_getCanvas: function () {
|
|
|
51 |
var $canvas = $("<canvas id=\"countdown360_" + $(this.element).attr("id") + "\" width=\"" +
|
|
|
52 |
this.settings.width + "\" height=\"" +
|
|
|
53 |
this.settings.height + "\">" +
|
|
|
54 |
"<span id=\"countdown-text\" role=\"status\" aria-live=\"assertive\"></span></canvas>");
|
|
|
55 |
$(this.element).prepend($canvas[0]);
|
|
|
56 |
return $canvas[0];
|
|
|
57 |
},
|
|
|
58 |
|
|
|
59 |
_initPen: function (canvas) {
|
|
|
60 |
this.pen = canvas.getContext("2d");
|
|
|
61 |
this.pen.lineWidth = this.settings.strokeWidth;
|
|
|
62 |
this.pen.strokeStyle = this.settings.strokeStyle;
|
|
|
63 |
this.pen.fillStyle = this.settings.fillStyle;
|
|
|
64 |
this.pen.textAlign = "center";
|
|
|
65 |
this.pen.textBaseline = "middle";
|
|
|
66 |
this.ariaText = $(canvas).children("#countdown-text");
|
|
|
67 |
this._clearRect();
|
|
|
68 |
},
|
|
|
69 |
|
|
|
70 |
_clearRect: function () {
|
|
|
71 |
this.pen.clearRect(0, 0, this.settings.width, this.settings.height);
|
|
|
72 |
},
|
|
|
73 |
|
|
|
74 |
_drawCountdownLabel: function (secondsElapsed) {
|
|
|
75 |
var secondsLeft = this.settings.seconds - secondsElapsed,
|
|
|
76 |
label = "Please Wait";
|
|
|
77 |
this.ariaText.text(secondsLeft);
|
|
|
78 |
this.pen.fillStyle = this.settings.fontColor;
|
|
|
79 |
this.pen.font = this.settings.fontWeight + " " + this.settings.fontSize + "px " + this.settings.fontFamily;
|
|
|
80 |
this.pen.fillText(secondsLeft, this.settings.width/2, this.settings.height/2 - (this.settings.fontSize/6.2) );
|
|
|
81 |
this.pen.font = "normal small-caps " + (this.settings.fontSize/3) + "px " + this.settings.fontFamily;
|
|
|
82 |
this.pen.fillText(label, this.settings.width/2, this.settings.height/2 + (this.settings.fontSize/2.2));
|
|
|
83 |
},
|
|
|
84 |
|
|
|
85 |
_drawCountdownShape: function (endAngle, drawStroke) {
|
|
|
86 |
this.pen.fillStyle = this.settings.fillStyle;
|
|
|
87 |
this.pen.beginPath();
|
|
|
88 |
this.pen.arc(this.settings.arcX, this.settings.arcY, this.settings.radius, Math.PI*1.5, endAngle, false);
|
|
|
89 |
this.pen.fill();
|
|
|
90 |
if (drawStroke) { this.pen.stroke(); }
|
|
|
91 |
},
|
|
|
92 |
|
|
|
93 |
_draw: function () {
|
|
|
94 |
var secondsElapsed = Math.round((new Date().getTime() - this.startedAt.getTime())/1000),
|
|
|
95 |
endAngle = (Math.PI*3.5) - (((Math.PI*2)/this.settings.seconds) * secondsElapsed);
|
|
|
96 |
this._clearRect();
|
|
|
97 |
this._drawCountdownShape(Math.PI*3.5, false);
|
|
|
98 |
if (secondsElapsed < this.settings.seconds) {
|
|
|
99 |
this._drawCountdownShape(endAngle, true);
|
|
|
100 |
this._drawCountdownLabel(secondsElapsed);
|
|
|
101 |
} else {
|
|
|
102 |
this._drawCountdownLabel(this.settings.seconds);
|
|
|
103 |
this.stop();
|
|
|
104 |
this.settings.onComplete();
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
};
|
|
|
109 |
|
|
|
110 |
$.fn[pluginName] = function (options) {
|
|
|
111 |
var plugin;
|
|
|
112 |
this.each(function() {
|
|
|
113 |
plugin = $.data(this, "plugin_" + pluginName);
|
|
|
114 |
if (!plugin) {
|
|
|
115 |
plugin = new Plugin(this, options);
|
|
|
116 |
$.data(this, "plugin_" + pluginName, plugin);
|
|
|
117 |
}
|
|
|
118 |
});
|
|
|
119 |
return plugin;
|
|
|
120 |
};
|
|
|
121 |
|
|
|
122 |
})(jQuery, window, document);
|