-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock1.html
68 lines (64 loc) · 1.59 KB
/
clock1.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>时钟</title>
<style>
.clock {
width: 200px;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="clock">
<canvas height="200" width="200"></canvas>
</div>
<script>
var obj = document.getElementsByTagName('canvas')[0],
ctx = obj.getContext('2d'),
width = ctx.canvas.width,
height = ctx.canvas.height,
r = width/2,
rem = width/200;
// 画圆
function drawBackground() {
ctx.save();
ctx.translate(r,r);
ctx.beginPath();
ctx.lineWidth = 10*rem;
//以0,0为原点,r为半径,0为起始角,2*Math.PI为结束角,顺时针画圆
ctx.arc(0,0,r-ctx.lineWidth / 2, 0 , 2*Math.PI, false);
ctx.stroke();
var hourNumber = [3,4,5,6,7,8,9,10,11,12,1,2];
ctx.font = 18*rem +'px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
// 画出1-12的数字
hourNumber.forEach(function (number,i){
var rad = 2*Math.PI / 12* i;
var x = Math.cos(rad) * (r-30*rem);
var y = Math.sin(rad) * (r-30*rem);
ctx.fillText(number,x,y);
});
// 画出秒针走动的60个点
for (var i = 0; i < 60; i++) {
var rad = 2*Math.PI / 60*i;
var x = Math.cos(rad)*(r-18*rem);
var y = Math.sin(rad)*(r-18*rem);
ctx.beginPath();
if (i%5 === 0) {
ctx.fillStyle = '#000';
ctx.arc(x,y,2*rem,0,2,2*Math.PI,false);
} else {
ctx.fillStyle = '#ccc';
ctx.arc(x,y,2*rem,0,2,2*Math.PI,false);
}
ctx.fill();
}
}
drawBackground();
console.log(obj);
</script>
</body>
</html>