UNPKG

1.28 kBJavaScriptView Raw
1var drawHand = function(value, length) {
2 ctx.beginPath();
3
4 var angle = Math.PI * 2 * value / 60;
5 var x = Math.sin(angle) * length;
6 var y = Math.cos(angle) * length;
7
8 ctx.moveTo(100, 100);
9 ctx.lineTo(100 + x, 100 - y);
10 ctx.stroke();
11}
12
13var drawClock = function(time) {
14 if (!ctx) {
15 var h = time.getHours();
16 var m = time.getMinutes();
17 var s = time.getSeconds();
18
19 var text =
20 ((h >= 10) ? h : "0" + h) + ":" +
21 ((h >= 10) ? m : "0" + m) + ":" +
22 ((h >= 10) ? s : "0" + s);
23
24 document.getElementById("clockText").innerHTML = text;
25 return;
26 }
27
28 ctx.clearRect(0, 0, 200, 200);
29
30 ctx.beginPath();
31 ctx.arc(100, 100, 90, 0, Math.PI * 2, false);
32 for (var i = 0; i < 60; i += 5) {
33 var angle = Math.PI * 2 * i / 60;
34 var x = Math.sin(angle);
35 var y = Math.cos(angle);
36 ctx.moveTo(100 + x * 85, 100 - y * 85);
37 ctx.lineTo(100 + x * 90, 100 - y * 90);
38 }
39 ctx.stroke();
40
41 drawHand(time.getSeconds(), 80);
42 drawHand(time.getMinutes() + time.getSeconds() * 1.0 / 60, 60);
43 drawHand(time.getHours() % 12 * 5 + time.getMinutes() * 1.0 / 12, 40);
44}
45
46var drawClockAsync = function(interval) {
47 while (true) {
48 drawClock(new Date());
49 setTimeout(cont(), interval);
50 }
51};
\No newline at end of file