-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclocks.coffee
92 lines (79 loc) · 2.04 KB
/
clocks.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
shuffle = (array) ->
m = array.length
while (m)
i = Math.floor(Math.random() * m--)
t = array[m]
array[m] = array[i]
array[i] = t
return array
makeClock = (sel, size) ->
c = size / 2
r = c - 3
rm = r - 3
rh = r - 6
sel.append('circle')
.attr('class', 'outter')
.attr('cx', c)
.attr('cy', c)
.attr('r', r)
.style('fill', 'none')
sel.append('line')
.attr('class', 'hour')
.attr('x1', c)
.attr('y1', c)
.attr('x2', (d) -> c + rh * Math.sin(Math.floor(d / 60)/6*Math.PI))
.attr('y2', (d) -> c - rh * Math.cos(Math.floor(d / 60)/6*Math.PI))
.style('fill', 'none')
sel.append('line')
.attr('class', 'minute')
.attr('x1', c)
.attr('y1', c)
.attr('x2', (d) -> c + rm * Math.sin((d % 60)/30*Math.PI))
.attr('y2', (d) -> c - rm * Math.cos((d % 60)/30*Math.PI))
.style('fill', 'none')
sel.append('circle')
.attr('class', 'inner')
.attr('cx', c)
.attr('cy', c)
.attr('r', 2)
.style('stroke', 'none')
return
size = 38
svg = d3.select(document.body).append('svg')
.attr('width', size * 36)
.attr('height', size * 20)
.style('margin-left', -size * 36 / 2)
.style('margin-top', -size * 20 / 2)
numbers = shuffle(d3.range(720))
sel = svg.selectAll('g.clock').data(numbers)
sel.enter()
.append('g')
.attr('class', 'clock')
.attr('transform', (d, i) ->
x = (i % 36) * size
y = Math.floor(i / 36) * size
return "translate(#{x},#{y})"
)
.style('fill', 'white')
.style('stroke', 'white')
.call(makeClock, size)
shownTime = null
setTime = ->
duration = 1500
now = new Date(Date.now() + duration / 2)
time = (now.getHours() % 12) * 60 + now.getMinutes()
return if time is shownTime
shownTime = time
col = (d) -> if d is shownTime then 'red' else 'white'
svg.selectAll('g.clock')
.transition()
.duration(duration)
.style('fill', col)
.style('stroke', col)
return
setTime()
setInterval(setTime, 200)
d3.select(window)
.on 'resize', ->
#console.log window.innerWidth, window.innerHeight
return