-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
126 lines (103 loc) · 3.46 KB
/
client.js
File metadata and controls
126 lines (103 loc) · 3.46 KB
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
document.addEventListener('DOMContentLoaded', function() {
const lights = Array.from(document.querySelectorAll('.light-strip'));
const timeDisplay = document.querySelector('.time');
const bestTimeDisplay = document.querySelector('.best span');
let bestTime = Number(localStorage.getItem('best')) || Infinity;
let started = false;
let lightsOutTime = 0;
let raf, timeout;
let reduceTime = false; // New variable to track if time should be reduced
if (bestTime !== Infinity) {
bestTimeDisplay.textContent = formatTime(bestTime);
}
function formatTime(time) {
time = Math.round(time) / 1000;
return time.toFixed(3).padStart(6, '0');
}
function start() {
lights.forEach(light => light.classList.remove('on'));
timeDisplay.textContent = '00.000';
timeDisplay.classList.remove('anim');
lightsOutTime = 0;
reduceTime = false; // Reset the time reduction flag
const lightsStart = performance.now();
let lightsOn = 0;
function frame(now) {
const toLight = Math.floor((now - lightsStart) / 1000) + 1;
if (toLight > lightsOn) {
lights.slice(0, toLight).forEach(light => light.classList.add('on'));
}
if (toLight < 5) {
raf = requestAnimationFrame(frame);
} else {
const delay = Math.random() * 4000 + 1000;
timeout = setTimeout(() => {
lights.forEach(light => light.classList.remove('on'));
lightsOutTime = performance.now();
}, delay);
}
}
raf = requestAnimationFrame(frame);
}
function end(timeStamp) {
cancelAnimationFrame(raf);
clearTimeout(timeout);
if (!lightsOutTime) {
timeDisplay.textContent = "Jump Start!";
timeDisplay.classList.add('anim');
return;
}
let thisTime = timeStamp - lightsOutTime;
if (reduceTime) {
thisTime -= 222; // Reduce the time by 200ms if 'a' was pressed
}
// Ensure time is not negative or zero
if (thisTime < 0.00001) {
thisTime = 0.001;
}
timeDisplay.textContent = formatTime(thisTime);
if (thisTime < bestTime) {
bestTime = thisTime;
bestTimeDisplay.textContent = timeDisplay.textContent;
localStorage.setItem('best', thisTime);
}
timeDisplay.classList.add('anim');
}
function tap(event) {
const timeStamp = performance.now();
if (!started && event.target.closest && event.target.closest('a')) return;
event.preventDefault();
if (started) {
end(timeStamp);
started = false;
} else {
start();
started = true;
}
}
function handleKeyPress(event) {
if (event.key === ' ') {
tap(event);
} else if (event.key === 'a' && started) {
reduceTime = true; // Set the flag to reduce time
}
}
addEventListener('touchstart', tap, { passive: false });
addEventListener('mousedown', event => {
if (event.button === 0) tap(event);
}, { passive: false });
addEventListener('keydown', handleKeyPress, { passive: false });
document.getElementById('floating-badge').addEventListener('click', function() {
window.open('https://www.youtube.com/watch?v=xvFZjo5PgG0', '_blank');
});
function updateBadgeColor() {
const badge = document.getElementById('floating-badge');
badge.style.transition = 'transform 0.5s ease, opacity 0.5s ease';
badge.classList.add('size-change');
setTimeout(() => {
badge.classList.remove('size-change');
}, 500);
}
updateBadgeColor();
setInterval(updateBadgeColor, 3000);
});