-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
85 lines (76 loc) · 1.82 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Analog Clock</title>
</head>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
function clock(){
// clear timer
clearTimeout(timer);
// remove svg repeat
d3.select('svg').remove();
// set svg
var width = 400;
var height = 400;
var svg = d3.select('body')
.append('svg')
.attr('width', width)
.attr('height', height);
// now
var now = new Date();
var hh = now.getHours() % 12;
var mm = now.getMinutes();
var ss = now.getSeconds();
console.log(hh+':'+mm+':'+ss);
// hourHand
svg.append('line')
.attr('x1', 200)
.attr('y1', 200)
.attr('x2', 200)
.attr('y2', 150)
.style('stroke', 'black')
.style('stroke-width', 3)
.attr('transform', 'rotate('+ (hh*30+mm/2+ss/120) +' 200 200)');
// minuteHand
svg.append('line')
.attr('x1', 200)
.attr('y1', 200)
.attr('x2', 200)
.attr('y2', 120)
.style('stroke', 'black')
.style('stroke-width', 2)
.attr('transform', 'rotate('+ (mm*6+ss/10) +' 200 200)');
// secondHand
svg.append('line')
.attr('x1', 200)
.attr('y1', 200)
.attr('x2', 200)
.attr('y2', 106)
.style('stroke', 'red')
.style('stroke-width', 1)
.attr('transform', 'rotate('+ ss*6 +' 200 200)');
// center circle
svg.append('circle')
.attr('cx', '200px')
.attr('cy', '200px')
.attr('r', '3px')
.attr('fill', 'black');
// 12 clock notation
for(var i=0; i<12; i++){
svg.append('circle')
.attr('cx', '200px')
.attr('cy', '100px')
.attr('r', '2px')
.attr('fill', 'black')
.attr('transform', 'rotate(' +30*(i+1)+ ' 200 200)');
}
var timer = setTimeout(clock, 1000);
}
clock();
</script>
</body>
</html>