1;(
function ($, window, document, undefined) {
2 var pluginName =
"countdown360",
5 strokeStyle:
"#477050",
6 strokeWidth: undefined,
9 fontFamily:
"sans-serif",
14 label: [
"second",
"seconds"],
15 startOverAfterAdding:
true,
18 onComplete:
function () {}
21 function Plugin(element, options) {
22 this.element = element;
23 this.settings = $.extend({}, defaults, options);
24 if (!this.settings.fontSize) { this.settings.fontSize = this.settings.radius/1.2; }
25 if (!this.settings.strokeWidth) { this.settings.strokeWidth = this.settings.radius/4; }
26 this._defaults = defaults;
27 this._name = pluginName;
32 getTimeRemaining:
function()
35 var timeRemaining = this._secondsLeft(this.getElapsedTime());
38 getElapsedTime:
function()
40 return Math.round((
new Date().getTime() - this.startedAt.getTime())/1000);
42 extendTimer:
function (value) {
43 var seconds = parseInt(value),
44 secondsElapsed = Math.round((
new Date().getTime() - this.startedAt.getTime())/1000);
45 if ((this._secondsLeft(secondsElapsed) + seconds) <= this.settings.seconds) {
46 this.startedAt.setSeconds(this.startedAt.getSeconds() + parseInt(value));
50 addSeconds:
function (value) {
51 var secondsElapsed = Math.round((
new Date().getTime() - this.startedAt.getTime())/1000);
52 if (this.settings.startOverAfterAdding) {
53 this.settings.seconds = this._secondsLeft(secondsElapsed) + parseInt(value);
56 this.settings.seconds += parseInt(value);
61 this.startedAt =
new Date();
62 this._drawCountdownShape(Math.PI*3.5, !
this.settings.clockwise);
63 this._drawCountdownLabel(0);
64 var timerInterval = 1000;
65 if (this.settings.smooth) {
68 this.interval = setInterval(jQuery.proxy(
this._draw,
this), timerInterval);
72 clearInterval(this.interval);
77 this.settings.width = (this.settings.radius * 2) + (this.settings.strokeWidth * 2);
78 this.settings.height = this.settings.width;
79 this.settings.arcX = this.settings.radius + this.settings.strokeWidth;
80 this.settings.arcY = this.settings.arcX;
81 this._initPen(this._getCanvas());
82 if (this.settings.autostart) { this.start(); }
85 _getCanvas:
function () {
86 var $canvas = $(
"<canvas id=\"countdown360_" + $(this.element).attr(
"id") +
"\" width=\"" +
87 this.settings.width +
"\" height=\"" +
88 this.settings.height +
"\">" +
89 "<span id=\"countdown-text\" role=\"status\" aria-live=\"assertive\"></span></canvas>");
90 $(this.element).prepend($canvas[0]);
94 _initPen:
function (canvas) {
95 this.pen = canvas.getContext(
"2d");
96 this.pen.lineWidth = this.settings.strokeWidth;
97 this.pen.strokeStyle = this.settings.strokeStyle;
98 this.pen.fillStyle = this.settings.fillStyle;
99 this.pen.textAlign =
"center";
100 this.pen.textBaseline =
"middle";
101 this.ariaText = $(canvas).children(
"#countdown-text");
105 _clearRect:
function () {
106 this.pen.clearRect(0, 0, this.settings.width,
this.settings.height);
109 _secondsLeft:
function(secondsElapsed) {
110 return this.settings.seconds - secondsElapsed;
113 _drawCountdownLabel:
function (secondsElapsed) {
114 this.ariaText.text(secondsLeft);
115 this.pen.font = this.settings.fontWeight +
" " + this.settings.fontSize +
"px " + this.settings.fontFamily;
116 var secondsLeft = this._secondsLeft(secondsElapsed),
117 label = secondsLeft === 1 ? this.settings.label[0] : this.settings.label[1],
118 drawLabel = this.settings.label && this.settings.label.length === 2,
119 x = this.settings.width/2;
121 y = this.settings.height/2 - (this.settings.fontSize/6.2);
123 y = this.settings.height/2;
125 this.pen.fillStyle = this.settings.fillStyle;
126 this.pen.fillText(secondsLeft + 1, x, y);
127 this.pen.fillStyle = this.settings.fontColor;
128 this.pen.fillText(secondsLeft, x, y);
130 this.pen.font =
"normal small-caps " + (this.settings.fontSize/3) +
"px " + this.settings.fontFamily;
131 this.pen.fillText(label, this.settings.width/2,
this.settings.height/2 + (
this.settings.fontSize/2.2));
135 _drawCountdownShape:
function (endAngle, drawStroke) {
136 this.pen.fillStyle = this.settings.fillStyle;
137 this.pen.beginPath();
138 this.pen.arc(this.settings.arcX,
this.settings.arcY,
this.settings.radius, Math.PI*1.5, endAngle,
false);
140 if (drawStroke) { this.pen.stroke(); }
144 var millisElapsed, secondsElapsed;
145 millisElapsed =
new Date().getTime() - this.startedAt.getTime();
146 secondsElapsed = Math.floor((millisElapsed)/1000);
147 if (this.settings.clockwise) {
148 endAngle = (Math.PI*1.5) + (((Math.PI*2)/(this.settings.seconds * 1000)) * millisElapsed);
150 endAngle = (Math.PI*3.5) - (((Math.PI*2)/(this.settings.seconds * 1000)) * millisElapsed);
153 if (secondsElapsed < this.settings.seconds) {
154 this._drawCountdownShape(Math.PI*3.5,
false);
155 this._drawCountdownShape(endAngle,
true);
156 this._drawCountdownLabel(secondsElapsed);
158 this._drawCountdownShape(Math.PI * 3.5,
this.settings.clockwise);
159 this._drawCountdownLabel(this.settings.seconds);
161 this.settings.onComplete();
167 $.fn[pluginName] =
function (options) {
169 this.each(
function() {
170 plugin = $.data(
this,
"plugin_" + pluginName);
172 plugin =
new Plugin(
this, options);
173 $.data(
this,
"plugin_" + pluginName, plugin);
179})(jQuery, window, document);