-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
180 lines (163 loc) · 5.93 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
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
179
const currentUrl = new URL(window.location.href);
let startingTime = currentUrl.searchParams.get("startTime"),
endingTime = currentUrl.searchParams.get("finishTime");
let ststr = parseTimeString(startingTime),
etstr = parseTimeString(endingTime);
document.getElementById("time-start").innerHTML =
window.sl["start-time"].format(ststr.slice(0, ststr.indexOf(":", ststr.indexOf(":") + 1)));
document.getElementById("time-end").innerHTML =
window.sl["end-time"].format(etstr.slice(0, etstr.indexOf(":", etstr.indexOf(":") + 1)));
startingTime = new Date(+startingTime);
endingTime = new Date(+endingTime);
const difference = endingTime - startingTime;
const a = {
hour: endingTime.getHours(),
min: endingTime.getMinutes(),
sec: endingTime.getSeconds(),
milsec: endingTime.getMilliseconds()
};
let now;
let confettiDone = false;
const nowDisplay = document.getElementById("now");
const timeLeftDisplay = document.getElementById("time-left");
const clock = document.getElementById("clock");
const ctx = clock.getContext("2d");
//clock.width = clock.height = Math.min(window.innerWidth, window.innerHeight) / 2;
clock.width = clock.height = 300;
requestAnimationFrame(function f() {
now = new Date();
setTime(now);
let difference = startingTime - now;
if (difference < 0) {
difference = endingTime - now;
setDifference(difference, true);
} else {
setDifference(difference, false);
}
requestAnimationFrame(f);
})
function setTime(time) {
let hour = time.getHours(),
min = time.getMinutes(),
sec = time.getSeconds(),
milsec = time.getMilliseconds();
nowDisplay.innerHTML = window.sl["time-now"].format(parseTime(hour, min, sec, milsec));
ctx.clearRect(0, 0, 300, 300);
ctx.lineWidth = 1;
ctx.lineCap = "square";
// draw clock border
ctx.beginPath();
ctx.arc(150, 150, 150, 0, Math.PI * 2);
// draw clock ticks
for (let i = 0; i < 60; i++) {
let angle = Math.PI / 30 * i;
let vector = [Math.cos(angle), Math.sin(angle)];
let length = 10;
if (i % 5 == 0) length = 20;
ctx.moveTo(vector[0] * 140 + 150, vector[1] * 140 + 150);
ctx.lineTo(vector[0] * (140 - length) + 150, vector[1] * (140 - length) + 150);
}
ctx.stroke();
// draw clock hands
function drawHand(portion, length, thickness = 1) {
let angle = 2 * Math.PI * portion / 60 - Math.PI / 2;
let vec = [Math.cos(angle), Math.sin(angle)]
ctx.lineWidth = thickness;
ctx.lineCap = "round";
ctx.beginPath();
ctx.moveTo(150, 150);
ctx.lineTo(vec[0] * length + 150, vec[1] * length + 150)
ctx.stroke();
}
// ending time
ctx.strokeStyle = "#f0f0f0";
//drawHand(a.sec + a.milsec / 1000, 115);
//drawHand(a.min + a.sec / 60 + milsec / 60000, 90, 3);
drawHand((a.hour + (a.min + a.sec / 60 + a.milsec / 60000) / 60) * 5, 75, 5);
// now
ctx.strokeStyle = "black";
drawHand(sec + milsec / 1000, 115);
drawHand(min + sec / 60 + milsec / 60000, 90, 3);
drawHand((hour + (min + sec / 60 + milsec / 60000) / 60) * 5, 75, 5);
// there's 12 hours each round
}
function setDifference(d, inTest) {
document.querySelector(".valuebar").style.height =
Math.min(100 - d / difference * 100, 100) + '%';
let showMs = false;
let radius = 5;
if (d < 600000 && d > 0 && inTest) {
timeLeftDisplay.style.color = "red";
let thing = document.getElementsByClassName('wrap')[0];
let top = document.querySelector('.top');
let clock = document.querySelector('.clock');
// document.getElementsByClassName("bottom")[0].style.animation = "0.2s linear 0s infinite normal none running vibrate";
let angle = Math.random() * 2 * Math.PI;
let x = radius * Math.cos(angle);
let y = radius * Math.sin(angle);
let rotate = Math.random() * 2 - 1;
thing.style.transform = thing.style.mozTransform =
`translate(${x}px, ${y}px) rotate(${rotate}deg)`;
top.style.transform = top.style.mozTransform =
`translate(${x}px, ${y}px) rotate(${rotate}deg)`;
clock.style.transform = clock.style.mozTransform =
`translate(${x}px, ${y}px) rotate(${rotate}deg)`;
showMs = true;
document.querySelector(".smoke-img").style.opacity = -d / 1e6 + 1;
}
if (d < 0) {
timeLeftDisplay.innerHTML = window.sl["test-end"];
timeLeftDisplay.style.color = "red";
if (!confettiDone) {
confettiDone = true;
confetti();
setInterval(confetti, 5000);
}
let thing = document.getElementsByClassName('wrap')[0];
thing.style.transform = thing.style.mozTransform = null;
showMs = true;
document.querySelector(".smoke-img").style.opacity = 0;
} else {
if (inTest) {
timeLeftDisplay.innerHTML = window.sl["time-remaining"].format(parseTimeFromNumber(d, showMs));
} else {
timeLeftDisplay.innerHTML = window.sl["test-not-start"];
}
}
}
function parseTimeString(d, showMs = false) {
let date = new Date(+d);
let hour = date.getHours(),
min = date.getMinutes(),
sec = date.getSeconds(),
milsec = date.getMilliseconds();
return parseTime(hour, min, sec, milsec, showMs);
}
function parseTimeFromNumber(n, showMs = false) {
let d = new Date(n);
let hour = Math.floor((n % 86400000) / 3600000),
min = Math.floor((n % 3600000) / 60000),
sec = Math.floor((n % 60000) / 1000),
milsec = n % 1000;
/* let hour = d.getHours(),
min = d.getMinutes(),
sec = d.getSeconds(),
milsec = d.getMilliseconds(); */
return parseTime(hour, min, sec, milsec, showMs);
}
function parseTime(h, m, s, ms, showMs = false) {
let hstring = h > 0 ? h + ":" : "";
//let mstring = m > 0 ? m < 10 && hstring != "" ? "0" + m + ":" : m + ":" : "";
let mstring = "";
if (hstring && m < 10) mstring += "0";
if (m > 0 || mstring != '') {
mstring += m;
mstring += ':';
}
let sstring = s < 10 && mstring != "" ? "0" + s : s;
let msstring = "." + (ms < 10 ? "00" + ms : ms < 100 ? "0" + ms : ms);
return hstring + mstring + sstring + (showMs ? msstring : "");
}
function r(a, b) {
return Math.floor(Math.random() * (b - a + 1)) + a;
}