-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.html
129 lines (127 loc) 路 3.94 KB
/
clock.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<style>
#cas {
display: block;
border: 1px solid;
margin: auto;
}
</style>
<title>鏃堕挓</title>
</head>
<body>
<canvas id="cas" width="500" height="500">娴忚鍣ㄤ笉鏀寔canvas</canvas>
<script>
const canvasElement = document.getElementById("cas");
const context = canvasElement.getContext("2d");
const RADIUS = 100;
</script>
<script>
function Clock(context, x, y, r) {
this.context = context;
this.x = x;
this.y = y;
this.r = r;
this.draw();
}
Clock.prototype = {
draw: function() {
this.drawCircle();
this.drawNumber();
const animate = () => {
this.context.clearRect(
0,
0,
canvasElement.width,
canvasElement.height
);
this.context.putImageData(this.imgData, 0, 0);
this.drawPoints();
window.requestAnimationFrame(animate);
};
this.imgData = this.context.getImageData(
0,
0,
canvasElement.width,
canvasElement.height
);
animate();
},
drawCircle: function() {
this.context.beginPath();
this.context.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
this.context.stroke();
this.context.save();
this.context.beginPath();
this.context.arc(this.x, this.y, 5, 0, 2 * Math.PI);
this.context.fill();
this.context.restore();
},
drawNumber: function() {
this.context.font = "18px sans-serif";
this.context.textAlign = "center";
this.context.textBaseline = "middle";
for (let i = 0; i < 12; i++) {
const num = i <= 9 ? i + 3 : i - 9;
this.context.fillText(
num,
this.x + Math.cos((Math.PI / 6) * i) * (this.r - 15),
this.y + Math.sin((Math.PI / 6) * i) * (this.r - 15)
);
}
},
drawPoints: function() {
const { second, minute, hour } = this.getAngles();
const secondEndPoint = {
x: this.x + Math.cos(second) * 0.9 * this.r,
y: this.y + Math.sin(second) * 0.9 * this.r
};
const minuteEndPoint = {
x: this.x + Math.cos(minute) * 0.7 * this.r,
y: this.y + Math.sin(minute) * 0.7 * this.r
};
const hourEndPoint = {
x: this.x + Math.cos(hour) * 0.5 * this.r,
y: this.y + Math.sin(hour) * 0.5 * this.r
};
this.drawLine(secondEndPoint.x, secondEndPoint.y);
this.drawLine(minuteEndPoint.x, minuteEndPoint.y);
this.drawLine(hourEndPoint.x, hourEndPoint.y);
},
getAngles: function() {
const date = new Date();
const second = date.getSeconds();
const minute = date.getMinutes();
const hour =
date.getHours() + 1 > 12 ? date.getHours() - 12 : date.getHours();
const secondAngle = (second / 30) * Math.PI - Math.PI / 2;
const minuteAngle =
(minute / 30) * Math.PI - Math.PI / 2 + (second / 1800) * Math.PI;
const hourAngle =
(hour / 6) * Math.PI - Math.PI / 2 + (minute / 360) * Math.PI;
return {
second: secondAngle,
minute: minuteAngle,
hour: hourAngle
};
},
drawLine: function(x, y) {
this.context.beginPath();
this.context.moveTo(this.x, this.y);
this.context.lineTo(x, y);
this.context.stroke();
}
};
</script>
<script>
new Clock(
context,
canvasElement.width / 2,
canvasElement.height / 2,
RADIUS
);
</script>
</body>
</html>