-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.js
132 lines (116 loc) · 3.76 KB
/
clock.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
let canvas, ctx;
if (document.addEventListener) {
document.addEventListener('contextmenu', function (e) {
e.preventDefault();
}, false);
} else {
document.attachEvent('oncontextmenu', function () {
window.event.returnValue = false;
});
}
function draw () {
let time = (function () {
let midnight = new Date();
midnight.setHours(0);
midnight.setMinutes(0);
midnight.setSeconds(0);
midnight.setMilliseconds(0);
return Date.now() - midnight.getTime();
})(),
hours = time / (60 * 60 * 1000),
minutes = hours * 60 % 60,
seconds = minutes * 60 % 60,
digitalHours = Math.trunc(hours);
digitalMinutes = Math.trunc(minutes);
digitalSeconds = Math.trunc(seconds);
digitalHours = ("0" + digitalHours).slice(-2);
digitalMinutes = ("0" + digitalMinutes).slice(-2);
digitalSeconds = ("0" + digitalSeconds).slice(-2);
digitalClock = (digitalHours + ":" + digitalMinutes + ":" + digitalSeconds)
ctx.clearRect(0, 0, size, size);
ctx.lineCap = 'round';
c = {x: size / 2, y: size / 2};
face();
digital();
logo();
secondHand();
function face () {
// Background
ctx.beginPath();
ctx.arc(c.x, c.y, size / 2, 0, Math.PI * 2);
ctx.fillStyle = "#1a1a1a";
ctx.fill();
// Dashes
ctx.lineWidth = 12;
for (let i = 0; i < 60; i++) {
let r = (size -20) / 2,
l = 0;
ctx.strokeStyle = 'rgb(100, 35, 35)';
let v = new Vector(r, Math.PI * 2 * (i / 60) - Math.PI / 2);
ctx.beginPath();
ctx.moveTo(v.getX() + c.x, v.getY() + c.y);
v.setMag(r + l);
ctx.lineTo(v.getX() + c.x, v.getY() + c.y);
ctx.stroke();
}
// 5 second dashes
ctx.lineWidth = 12;
for (let i = 0; i < 12; i++) {
let r = ((size -20) / 2) -20,
l = 0;
ctx.strokeStyle = '#ff0000';
let v = new Vector(r, Math.PI * 2 * (i / 12) - Math.PI / 2);
ctx.beginPath();
ctx.moveTo(v.getX() + c.x, v.getY() + c.y);
v.setMag(r + l);
ctx.lineTo(v.getX() + c.x, v.getY() + c.y);
ctx.stroke();
}
}
function digital () {
ctx.font = "800 5rem Helvetica";
ctx.fillStyle = "red"
ctx.textAlign = "center";
ctx.fillText(digitalClock, size/2, (size/2)+25);
}
function logo () {
ctx.font = "800 2rem serif";
ctx.fillStyle = "#004d1a"
ctx.textAlign = "center";
ctx.fillText("ROLÔI", size/2, (size/4)*3);
}
function secondHand () {
ctx.lineWidth = 12;
for (let i = 0; i < seconds; i++) {
let r = (size -20) / 2,
l = 0;
ctx.strokeStyle = '#7fff00';
let v = new Vector(r, Math.PI * 2 * (i / 60) - Math.PI / 2);
ctx.beginPath();
ctx.moveTo(v.getX() + c.x, v.getY() + c.y);
v.setMag(r + l);
ctx.lineTo(v.getX() + c.x, v.getY() + c.y);
ctx.stroke();
}
}
}
function init () {
canvas = document.getElementById('clock');
width = window.innerWidth / 5 * 4.65;
height = window.innerHeight / 5 * 4.65;
if (width > height) {
size = height
}
else {
size = width
}
//size = height
console.log(width +"*"+ height)
console.log("size: " + size)
canvas.width = canvas.height = size;
console.log("canvas.width: " + canvas.width)
console.log("canvas.height: " + canvas.height)
ctx = canvas.getContext('2d');
setInterval(draw, 10);
}
init();