-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.html
62 lines (56 loc) · 2.19 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<canvas id="clock" width="500" height="500">
Please upgrade your browser.
</canvas>
<script>
function setTime() {
var canvas = document.getElementById("clock");
var context = canvas.getContext("2d");
var clockRadius = canvas.width/2;
context.fillStyle = "black";
context.beginPath();
context.arc(clockRadius, clockRadius, clockRadius, 0, 2*Math.PI);
context.fill();
context.fillStyle = "white";
context.beginPath();
context.arc(clockRadius, clockRadius, 10, 0, 2*Math.PI);
context.fill();
context.font = clockRadius / 10 + "px arial";
context.fillStyle = "white";
context.textBaseline = "middle";
context.textAlign = "center";
for (var i = 1; i <= 12; i++) {
context.fillText(i, clockRadius + clockRadius * 0.9 * Math.sin(i * 2 * Math.PI / 12), clockRadius - (clockRadius * 0.9 * Math.cos(i * 2 * Math.PI / 12)));
}
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var fullHours = hours % 12 + minutes / 60 + seconds / 3600;
var hoursAngle = fullHours * 2 * Math.PI / 12;
var minutesAngle = minutes * 2 * Math.PI / 60;
var secondsAngle = seconds * 2 * Math.PI / 60;
context.strokeStyle = "white";
context.lineWidth = 5;
context.moveTo(clockRadius, clockRadius);
context.lineTo(clockRadius + clockRadius * 0.6 * Math.sin(hoursAngle), clockRadius - (clockRadius * 0.6 * Math.cos(hoursAngle)));
context.stroke();
context.lineWidth = 3;
context.moveTo(clockRadius, clockRadius);
context.lineTo(clockRadius + clockRadius * 0.75 * Math.sin(minutesAngle), clockRadius - (clockRadius * 0.75 * Math.cos(minutesAngle)));
context.stroke();
context.lineWidth = 1;
context.moveTo(clockRadius, clockRadius);
context.lineTo(clockRadius + clockRadius * 0.9 * Math.sin(secondsAngle), clockRadius - (clockRadius * 0.9 * Math.cos(secondsAngle)));
context.stroke();
}
setInterval(setTime, 1000);
</script>
</body>
</html>