-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (66 loc) · 2.18 KB
/
script.js
File metadata and controls
76 lines (66 loc) · 2.18 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
const countEl = document.getElementById("count");
const clickArea = document.getElementById("clickArea");
const sandal = document.getElementById("sandal");
const mouse = document.getElementById("mouse");
const stars = document.getElementById("stars");
const floatplus = document.getElementById("floatplus");
const resetBtn = document.getElementById("resetBtn");
let busy = false;
let count = parseInt(localStorage.getItem("faarCount")) || 45;
countEl.textContent = count;
const AudioCtx = window.AudioContext || window.webkitAudioContext;
const audioCtx = AudioCtx ? new AudioCtx() : null;
function playHitSound() {
if (!audioCtx) return;
const now = audioCtx.currentTime;
const thud = audioCtx.createOscillator();
const thudGain = audioCtx.createGain();
thud.type = "sine";
thud.frequency.setValueAtTime(130, now);
thudGain.gain.setValueAtTime(0.0001, now);
thudGain.gain.exponentialRampToValueAtTime(0.6, now + 0.002);
thudGain.gain.exponentialRampToValueAtTime(0.0001, now + 0.14);
thud.connect(thudGain).connect(audioCtx.destination);
thud.start(now);
thud.stop(now + 0.16);
}
function showStars() {
stars.style.display = "block";
stars.style.opacity = 1;
stars.style.transition = "opacity 0.3s ease";
setTimeout(() => {
stars.style.opacity = 0;
setTimeout(() => {
stars.style.display = "none";
}, 300);
}, 800);
}
function doHit() {
if (busy) return;
busy = true;
sandal.classList.add("swing");
mouse.classList.add("mouse-shake");
playHitSound();
showStars();
floatplus.style.opacity = 1;
floatplus.style.transform = "translateX(-50%) translateY(-90px)";
floatplus.style.transition = "transform .7s,opacity .7s";
setTimeout(() => (floatplus.style.opacity = 0), 200);
count++;
countEl.textContent = count;
localStorage.setItem("faarCount", count);
setTimeout(() => {
sandal.classList.remove("swing");
mouse.classList.remove("mouse-shake");
busy = false;
}, 400);
}
clickArea.addEventListener("click", () => {
if (audioCtx && audioCtx.state === "suspended") audioCtx.resume();
doHit();
});
resetBtn.addEventListener("click", () => {
count = 45;
localStorage.setItem("faarCount", count);
countEl.textContent = count;
});