-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbandClock.coffee
122 lines (99 loc) · 3.54 KB
/
bandClock.coffee
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
###
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
###
'use strict'
(($) ->
$.bandClock = (el, options) ->
@el = el
@$el = $ el
@$el.data 'bandClock', @
@init = =>
@options = $.extend {}, $.bandClock.defaultOptions, options
#creating canvas element
@canvas = $("<canvas width='#{@options.size}' height='#{@options.size}'
></canvas>").get(0)
@$el.append @canvas
@ctx = @canvas.getContext('2d')
if window.devicePixelRatio > 1
scaleBy = window.devicePixelRatio
$(@canvas).css({
width: @options.size
height: @options.size
})
@canvas.width *= scaleBy
@canvas.height *= scaleBy
@ctx.scale scaleBy, scaleBy
@$el.addClass 'bandClock'
@$el.css({
width: @options.size
height: @options.size
lineHeight: "#{@options.size}px"
})
_x = @options.size/2
@ctx.translate _x, _x
@ctx.shadowBlur = 2;
@ctx.shadowColor = @options.color
@
degToRed = (degree)->
factor = Math.PI / 180
degree * factor
renderTime = =>
_x = @options.size/2
_g = @options.gap
_r = _x - (@options.lineWidth + 2)
_r1 = _r - (_g + @options.lineWidth)
_r2 = _r1 - (_g + @options.lineWidth)
@ctx.fillStyle = @options.bgColor
@ctx.fillRect -_x, -_x, @options.size, @options.size
@ctx.strokeStyle = @options.color
@ctx.lineWidth = @options.lineWidth
@ctx.lineCap = @options.lineCap
now = new Date()
hours = now.getHours();
minutes = now.getMinutes();
seconds = now.getSeconds();
milliseconds = now.getMilliseconds();
newSeconds = seconds + (milliseconds / 1000);
time = now.toLocaleTimeString();
#Hours
@ctx.beginPath()
@ctx.arc(0, 0, _r, degToRed(270), degToRed((hours * 30) - 90))
@ctx.stroke()
#Minutes
@ctx.beginPath()
@ctx.arc(0, 0, _r1, degToRed(270), degToRed((minutes * 6) - 90))
@ctx.stroke()
#Seconds
@ctx.beginPath()
@ctx.arc(0, 0, _r2, degToRed(270), degToRed((newSeconds * 6) - 90))
@ctx.stroke()
#Time
@ctx.font = _this.options.fontStyle
@ctx.fillStyle = _this.options.color
@ctx.textAlign = "center"
@ctx.fillText(time, 0, 0)
@
setInterval renderTime, 40
@init()
$.bandClock.defaultOptions =
size : 300
color: '#18FFFF'
bgColor: '#212121'
lineWidth: 10
lineCap: 'butt'
gap: 5
fontStyle: '20px Verdana'
$.fn.bandClock = (options) ->
$.each @, (i, el) ->
$el = ($ el)
unless $el.data 'bandClock'
instanceOptions = $.extend {}, options , $el.data()
$el.data 'bandClock', new $.bandClock el, instanceOptions
undefined
)(jQuery)