-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclock.html
107 lines (107 loc) · 4.37 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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
h1{text-align:center;color:red;text-shadow:2px 2px 8px olive;}
</style>
</head>
<body>
<h1>Real Clock</h1>
<canvas id="myCanvas" width="400" class="img-responsive" height="300" style="background-image:url('images/clock_n.jpg');background-repeat:no-repeat;background-position:center;">
</canvas><br /><br />
<div class="text-center">
<input type="checkbox" value="Roman" id="chkdial" /><span> Roman Numerals </span>
<input type="button" value="Start the Clock" onclick="window.setInterval('timing()',1000)" />
</div>
<p> </p>
<script type="text/javascript" >
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var k;
staticDial(2);
function timing() {
//get date
var tme = new Date();
//get seconds
var s = tme.getSeconds();
//get minutes
var m = tme.getMinutes();
//get hours
var h = tme.getHours();
//coordinates for 'seconds' hand
var x1 = 190 + 120 * Math.cos((270 + 6 * s) * Math.PI / 180);
var y1 = 150 + 120 * Math.sin((270 + 6 * s) * Math.PI / 180);
//coordinates for minute hand
var x2 = 200 + 100 * Math.cos((270 + 6 * m) * Math.PI / 180);
var y2 = 150 + 100 * Math.sin((270 + 6 * m) * Math.PI / 180);
// adjustment for the hour hand
if (h >= 12) {
h = h + 12;
}
if (m > 30) { h = h + 0.5; }
//coordinates for hour hand
var x3 = 200 + 80 * Math.cos((270 + 30 * h) * Math.PI / 180);
var y3 = 150 + 80 * Math.sin((270 + 30 * h) * Math.PI / 180);
// clear the canvas in after every 1000 ms
context.clearRect(0, 0, 400, 300);
// condition for testing for dial-type
if (document.getElementById('chkdial').checked)
{ staticDial(1); }
else
{ staticDial(2); }
//seconds
context.beginPath();
context.moveTo(200, 150);
context.lineTo(x1, y1);
//context.closePath();
context.strokeStyle = 'red';
context.lineWidth = 3;
context.stroke();
//minutes
context.beginPath();
context.moveTo(200, 150);
context.lineTo(x2, y2);
context.strokeStyle = 'blue';
context.lineWidth = 6;
context.stroke();
//hours
context.beginPath();
context.moveTo(200, 150);
context.lineTo(x3, y3);
context.strokeStyle = 'magenta';
context.lineWidth = 8;
context.stroke();
//dial
context.beginPath();
context.arc(200, 150, 140, 0, 2 * Math.PI, false);
context.strokeStyle = 'lightblue';
context.lineWidth = 3;
context.stroke();
context.closePath();
//button at the centre to cover the terminals of hands
context.beginPath();
context.arc(200, 150, 10, 0, 2 * Math.PI, false);
context.fillStyle = 'lightblue';
context.fill();
context.closePath();
}
// code for static dial
function staticDial(k) {
// background image of the canvas is determined by this - Arab numerals or Roman numerals
if (k == 1)
{ document.getElementById('myCanvas').style.backgroundImage = "url(images/clock_r.jpg)"; }
else {
document.getElementById('myCanvas').style.backgroundImage = "url(images/clock_n.jpg)";
//dial
context.beginPath();
context.arc(200, 150, 140, 0, 2 * Math.PI, false);
context.strokeStyle = 'lightblue';
context.lineWidth = 3;
context.stroke();
context.closePath();
}
}
</script>
</body>
</html>