-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathindex.html
150 lines (150 loc) · 3.79 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>时钟-豪情</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<style>
.c{background-color:#f1f1f1;margin:0 auto;}
</style>
</head>
<body>
<canvas id="canvas" class="c" width="500" height="500"></canvas>
<script>
(function(){
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var h,m,s;
function now(){
var d = new Date();
h = d.getHours();
m = d.getMinutes();
s = d.getSeconds();
h += m/60;
h = h > 12 ? h - 12 : h;
}
// 表盘
function drawBase(){
ctx.beginPath();
ctx.lineWidth = 10;
ctx.strokeStyle = '#99ccff';
ctx.arc(250, 250, 200, 0, 360, false);
ctx.closePath();
ctx.stroke();
}
function drawNumbers(){
var angle = 0,
nWidth = 0;
ctx.save();
ctx.translate(250, 250);
ctx.font="19px Arial";
for(var i = 1; i <= 12; i++){
if(!(i % 3)){
angle = Math.PI / 6 * (i - 3);
nWidth = ctx.measureText(i).width;
ctx.fillText(i, Math.cos(angle) * 150 - nWidth/2, Math.sin(angle) * 150 + nWidth/2);
}
}
ctx.restore();
}
// 时针刻度
function drawHourDegree(){
for(var i = 0; i < 12; i++){
ctx.save();
ctx.lineWidth = 5;
ctx.translate(250, 250);
ctx.rotate(i * 30 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, -190);
ctx.lineTo(0, -170);
ctx.strokeStyle = '#333';
ctx.closePath();
ctx.stroke();
ctx.restore();
}
}
// 分针盘
function drawMinDegree(){
for(var i = 0; i < 60; i++){
ctx.save();
ctx.translate(250, 250);
ctx.rotate(i * 6 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, -190);
ctx.lineWidth = 5;
ctx.lineTo(0, -180);
ctx.strokeStyle = '#999';
ctx.closePath();
ctx.stroke();
ctx.restore();
}
}
// 时针
function drawHour(){
ctx.save();
ctx.translate(250, 250);
ctx.rotate(h * 30 * Math.PI / 180); // 每小时转过12度
ctx.beginPath();
ctx.moveTo(0, -110);
ctx.lineTo(0, 30);
ctx.lineWidth = 9;
ctx.strokeStyle = '#333';
ctx.closePath();
ctx.stroke();
ctx.restore();
}
// 分针
function drawMin(){
ctx.save();
ctx.translate(250, 250);
ctx.rotate(m * 6 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, -130);
ctx.lineWidth = 7;
ctx.lineTo(0, 25);
ctx.strokeStyle = 'green';
ctx.closePath();
ctx.stroke();
ctx.restore();
}
// 中间圆点
function drawMiddle(){
ctx.beginPath();
ctx.arc(250, 250, 10, 0, 360, false);
ctx.closePath();
ctx.fill();
}
// 秒钟
function drawSecond(){
ctx.save();
ctx.translate(250, 250);
ctx.rotate(s * 6 * Math.PI / 180); // 6 每秒转过的角度
ctx.beginPath();
ctx.moveTo(0, -150);
ctx.lineWidth = 3;
ctx.strokeStyle = 'red';
ctx.lineTo(0, 25);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
function clock(){
ctx.clearRect(0, 0, 500, 500);
now();
drawBase();
drawNumbers();
drawMinDegree();
drawHourDegree();
drawHour();
drawMin();
drawSecond();
drawMiddle();
}
clock();
setInterval(function(){
clock();
}, 1000);
}());
</script>
</body>
</html>