-
Notifications
You must be signed in to change notification settings - Fork 3
/
to-the-king.html
178 lines (149 loc) · 5.1 KB
/
to-the-king.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To The King Clock</title>
<style>
@font-face {
font-family: "to-the-king";
src: url("to-the-king.ttf");
}
body {
overflow: hidden;
text-align: center;
vertical-align: middle;
height: 100vh;
position:relative;
background: rgba(0, 0, 0, 0);
margin: 0 auto;
}
#to-the-king-clock {
font-family: "to-the-king";
color: #00ff00;
font-size: 27.5vw;
position: relative;
top: 50%;
transform: translateY(-50%);
}
#menu {
display: none;
position: absolute;
}
#menu div {
background: gray;
color: white;
text-align: left;
padding-left: 5px;
}
#menu div:hover {
background: white;
color: black;
}
#menu div a {
background: gray;
color: white;
text-decoration: none;
}
#menu div a:hover {
background: white;
color: black;
}
</style>
</head>
<body>
<div id="to-the-king-clock"></div>
<div id="menu">
<div id="pause">Pause</div>
<div id="reset">Reset</div>
<div id="setting-custom-time">Setting time</div>
<div id="project" style="border-top: 1px solid black; margin-top: 1px;"><a href="https://github.com/kyunooh/to-the-king-clock" target="_blank">To the King Clock / by Kyunooh</a></div>
</div>
<script>
var startTime = new Date();
var clock = document.getElementById("to-the-king-clock");
var isPause = false;
var menu = document.getElementById("menu");
var pauseTime = 0;
var formTime = function(time) {
if (time < 10) {
return "0" + time;
}
return time;
};
var reset = function() {
startTime = new Date();
menu.style.display = "none";
};
var setTime = function() {
var time = Math.floor((new Date() - startTime)/1000);
var sec = time % 60;
var minute = Math.floor(time / 60 % (60));
var hour = Math.floor(time / (60 * 60));
clock.innerText = formTime(hour) + ":" + formTime(minute) + ":" + formTime(sec);
};
setTime();
var interval = setInterval(setTime, 500);
var settingCustomTime = function(customTime) {
menu.style.display = "none";
if(!customTime) {
customTime = prompt("Time format is 'hh:mm:ss' ex)12:34:56");
}
var reg = "([0-9]{1,3}):([0-9]{1,2}):([0-9]{1,2})";
var timeChecker = new RegExp(reg, "gi");
if(!timeChecker.test(customTime)) {
alert("Time format is wrong");
return false;
}
timeChecker = timeChecker = new RegExp(reg, "gi");
var timeGroups = timeChecker.exec(customTime);
var hours = parseInt(timeGroups[1]);
var minutes = parseInt(timeGroups[2]);
var seconds = parseInt(timeGroups[3]);
startTime = new Date(new Date() - ((hours * 3600) + (minutes * 60) + seconds) * 1000);
};
var url = new URL(location.href);
var queries = new URLSearchParams(url.search);
if(queries.has("time") && queries.get("time")) {
settingCustomTime(queries.get("time"));
}
var pauseAndStart = function() {
var pauseEl = document.getElementById("pause");
if(isPause) {
isPause = false;
var term = new Date() - pauseTime;
startTime = new Date(startTime.getTime() + term);
pauseEl.innerText = "Pause";
interval = setInterval(setTime, 500);
} else {
isPause = true;
pauseTime = new Date();
pauseEl.innerText = "Start";
clearInterval(interval);
}
menu.style.display = "none";
};
document.body.addEventListener("contextmenu", function(e) {
e.preventDefault();
menu.style.display = "block";
menu.style.top = e.clientY + "px";
menu.style.left = e.clientX + "px";
});
document.getElementById("pause").onclick = function(e) {
e.stopPropagation();
pauseAndStart();
};
document.getElementById("reset").onclick = function(e) {
e.stopPropagation();
reset();
};
document.body.addEventListener("click", function(e) {
menu.style.display = "none";
pauseAndStart();
});
document.getElementById("setting-custom-time").onclick = function(e) {
e.stopPropagation();
settingCustomTime();
};
</script>
</body>
</html>