-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (106 loc) · 3.42 KB
/
script.js
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
130
131
132
133
134
135
// the time
var time;
// hand rotations and radii
var secondsHandAngle;
var secondsHandRadius;
var minutesHandAngle;
var minutesHandRadius;
var hoursHandAngle;
var hoursHandRadius;
// unit for scaled sizing
var unitSize;
// width and height of window
const WIDTH = window.innerWidth,
HEIGHT = window.innerHeight;
window.onload = function(e) {
// canvas setup
var canvas = document.getElementById("canvas");
var c = canvas.getContext("2d");
canvas.width = WIDTH;
canvas.height = HEIGHT;
// initial setup
init(c);
// main logic loop
var loop = function() {
update();
render(c);
window.requestAnimationFrame(loop, canvas);
}
window.requestAnimationFrame(loop, canvas);
}
function init(c) {
// set size unit based on screen size
unitSize = (HEIGHT > WIDTH ? WIDTH : HEIGHT) * 0.001;
// set stroke values
c.strokeStyle = "white";
c.lineCap = "round";
}
function update() {
// get current time
var time = theTime();
// calculate hand rotations
secondsHandAngle = (time.seconds / 60 * 2 * Math.PI) - Math.PI / 2;
minutesHandAngle = ((time.minutes + time.seconds / 60) / 60 * 2 * Math.PI) - Math.PI / 2;
hoursHandAngle = (((time.hours + time.minutes / 60) % 12) / 12 * 2 * Math.PI) - Math.PI / 2;
}
function render(c) {
// determine appropriate radius
var radius = (HEIGHT > WIDTH ? WIDTH : HEIGHT) / 2 * 0.9;
// calculate hand radii
secondsHandRadius = radius * 0.9;
minutesHandRadius = radius * 0.85;
hoursHandRadius = radius * 0.5;
// clear previous frame
c.clearRect(0, 0, WIDTH, HEIGHT);
// draw clock border
c.lineWidth = unitSize * 6;
c.beginPath();
c.arc(WIDTH / 2, HEIGHT / 2, radius, 0, 2 * Math.PI);
c.stroke();
c.closePath();
// draw seconds hand
drawLine(c, WIDTH / 2, HEIGHT / 2,
WIDTH / 2 + Math.cos(secondsHandAngle) * secondsHandRadius,
HEIGHT / 2 + Math.sin(secondsHandAngle) * secondsHandRadius,
unitSize * 1);
// draw minutes hand
drawLine(c, WIDTH / 2, HEIGHT / 2,
WIDTH / 2 + Math.cos(minutesHandAngle) * minutesHandRadius,
HEIGHT / 2 + Math.sin(minutesHandAngle) * minutesHandRadius,
unitSize * 7);
// draw hours hand
drawLine(c, WIDTH / 2, HEIGHT / 2,
WIDTH / 2 + Math.cos(hoursHandAngle) * hoursHandRadius,
HEIGHT / 2 + Math.sin(hoursHandAngle) * hoursHandRadius,
unitSize * 7);
// draw spokes
for (var i = 0; i < 60; i++) {
// determine angle and length of spoke
var spokeAngle = i / 60 * 2 * Math.PI;
var spokeLength = i % 5 == 0 ? radius * 0.1 : radius * 0.05;
// draw single spoke
drawLine(c, WIDTH / 2 + Math.cos(spokeAngle) * (radius - spokeLength),
HEIGHT / 2 + Math.sin(spokeAngle) * (radius - spokeLength),
WIDTH / 2 + Math.cos(spokeAngle) * radius,
HEIGHT / 2 + Math.sin(spokeAngle) * radius,
unitSize * (i % 5 == 0 ? 3 : 1));
}
}
// helper for drawing lines
function drawLine(c, x1, y1, x2, y2, width) {
c.lineWidth = width;
c.beginPath();
c.moveTo(x1, y1);
c.lineTo(x2, y2);
c.stroke();
c.closePath();
}
// returns the current time as a JSON object
function theTime() {
var date = new Date();
return {
hours: date.getHours(),
minutes: date.getMinutes(),
seconds: date.getSeconds()
}
}