TiledViz
Loading...
Searching...
No Matches
jquery.countdown360.js
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 label: ["second", "seconds"], // the label to use or false if none
15 startOverAfterAdding: true, // Start the timer over after time is added with addSeconds
16 smooth: false, // should the timer be smooth or stepping
17 clockwise: false, // should the timer be counterclockwise or clockwise
18 onComplete: function () {}
19 };
20
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;
28 this._init();
29 }
30
31 Plugin.prototype = {
32 getTimeRemaining: function()
33 {
34
35 var timeRemaining = this._secondsLeft(this.getElapsedTime());
36 return timeRemaining;
37 },
38 getElapsedTime: function()
39 {
40 return Math.round((new Date().getTime() - this.startedAt.getTime())/1000);
41 },
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));
47 }
48 },
49
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);
54 this.start();
55 } else {
56 this.settings.seconds += parseInt(value);
57 }
58 },
59
60 start: function () {
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) {
66 timerInterval = 16;
67 }
68 this.interval = setInterval(jQuery.proxy(this._draw, this), timerInterval);
69 },
70
71 stop: function (cb) {
72 clearInterval(this.interval);
73 if (cb) { cb(); }
74 },
75
76 _init: function () {
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(); }
83 },
84
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]);
91 return $canvas[0];
92 },
93
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");
102 this._clearRect();
103 },
104
105 _clearRect: function () {
106 this.pen.clearRect(0, 0, this.settings.width, this.settings.height);
107 },
108
109 _secondsLeft: function(secondsElapsed) {
110 return this.settings.seconds - secondsElapsed;
111 },
112
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;
120 if (drawLabel) {
121 y = this.settings.height/2 - (this.settings.fontSize/6.2);
122 } else {
123 y = this.settings.height/2;
124 }
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);
129 if (drawLabel) {
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));
132 }
133 },
134
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);
139 this.pen.fill();
140 if (drawStroke) { this.pen.stroke(); }
141 },
142
143 _draw: function () {
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);
149 } else {
150 endAngle = (Math.PI*3.5) - (((Math.PI*2)/(this.settings.seconds * 1000)) * millisElapsed);
151 }
152 this._clearRect();
153 if (secondsElapsed < this.settings.seconds) {
154 this._drawCountdownShape(Math.PI*3.5, false);
155 this._drawCountdownShape(endAngle, true);
156 this._drawCountdownLabel(secondsElapsed);
157 } else {
158 this._drawCountdownShape(Math.PI * 3.5, this.settings.clockwise);
159 this._drawCountdownLabel(this.settings.seconds);
160 this.stop();
161 this.settings.onComplete();
162 }
163 }
164
165 };
166
167 $.fn[pluginName] = function (options) {
168 var plugin;
169 this.each(function() {
170 plugin = $.data(this, "plugin_" + pluginName);
171 if (!plugin) {
172 plugin = new Plugin(this, options);
173 $.data(this, "plugin_" + pluginName, plugin);
174 }
175 });
176 return plugin;
177 };
178
179})(jQuery, window, document);