forked from lasverg/bandClock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbandClock.js
119 lines (113 loc) · 4.11 KB
/
bandClock.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
/*
Band Clock is a jquery plugin to display a dynamic band clock.
Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
Built on top of the jQuery library (http://jquery.com)
@source: http://github.com/zaniitiin/band-clock/
@autor: Nitin Jha
@version: 1.0
*/
(function() {
'use strict';
(function($) {
$.bandClock = function(el, options) {
var degToRed, renderTime;
this.el = el;
this.$el = $(el);
this.$el.data('bandClock', this);
this.init = (function(_this) {
return function() {
var _x, scaleBy;
_this.options = $.extend({}, $.bandClock.defaultOptions, options);
_this.canvas = $("<canvas width='" + _this.options.size + "' height='" + _this.options.size + "' ></canvas>").get(0);
_this.$el.append(_this.canvas);
_this.ctx = _this.canvas.getContext('2d');
if (window.devicePixelRatio > 1) {
scaleBy = window.devicePixelRatio;
$(_this.canvas).css({
width: _this.options.size,
height: _this.options.size
});
_this.canvas.width *= scaleBy;
_this.canvas.height *= scaleBy;
_this.ctx.scale(scaleBy, scaleBy);
}
_this.$el.addClass('bandClock');
_this.$el.css({
width: _this.options.size,
height: _this.options.size,
lineHeight: _this.options.size + "px"
});
_x = _this.options.size / 2;
_this.ctx.translate(_x, _x);
_this.ctx.shadowBlur = 2;
_this.ctx.shadowColor = _this.options.color;
return _this;
};
})(this);
degToRed = function(degree) {
var factor;
factor = Math.PI / 180;
return degree * factor;
};
renderTime = (function(_this) {
return function() {
var _g, _r, _r1, _r2, _x, hours, milliseconds, minutes, newSeconds, now, seconds, time;
_x = _this.options.size / 2;
_g = _this.options.gap;
_r = _x - (_this.options.lineWidth + 2);
_r1 = _r - (_g + _this.options.lineWidth);
_r2 = _r1 - (_g + _this.options.lineWidth);
_this.ctx.fillStyle = _this.options.bgColor;
_this.ctx.fillRect(-_x, -_x, _this.options.size, _this.options.size);
_this.ctx.strokeStyle = _this.options.color;
_this.ctx.lineWidth = _this.options.lineWidth;
_this.ctx.lineCap = _this.options.lineCap;
now = new Date();
hours = now.getHours();
minutes = now.getMinutes();
seconds = now.getSeconds();
milliseconds = now.getMilliseconds();
newSeconds = seconds + (milliseconds / 1000);
time = now.toLocaleTimeString();
_this.ctx.beginPath();
_this.ctx.arc(0, 0, _r, degToRed(270), degToRed((hours * 30) - 90));
_this.ctx.stroke();
_this.ctx.beginPath();
_this.ctx.arc(0, 0, _r1, degToRed(270), degToRed((minutes * 6) - 90));
_this.ctx.stroke();
_this.ctx.beginPath();
_this.ctx.arc(0, 0, _r2, degToRed(270), degToRed((newSeconds * 6) - 90));
_this.ctx.stroke();
_this.ctx.font = _this.options.fontStyle;
_this.ctx.fillStyle = _this.options.color;
_this.ctx.textAlign = "center";
_this.ctx.fillText(time, 0, 0);
return _this;
};
})(this);
setInterval(renderTime, 40);
return this.init();
};
$.bandClock.defaultOptions = {
size: 300,
color: '#18FFFF',
bgColor: '#212121',
lineWidth: 10,
lineCap: 'butt',
gap: 5,
fontStyle: '20px Verdana'
};
return $.fn.bandClock = function(options) {
$.each(this, function(i, el) {
var $el, instanceOptions;
$el = $(el);
if (!$el.data('bandClock')) {
instanceOptions = $.extend({}, options, $el.data());
return $el.data('bandClock', new $.bandClock(el, instanceOptions));
}
});
return void 0;
};
})(jQuery);
}).call(this);