Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const regularFart = new Audio("fart-83471-fixed-regular.flac");
const critFart = new Audio("fart-4-228244-fixed-crit.flac");
const STORAGE_KEY = "@button:"

let canPlaySound = true;

const farts = [
regularFart,
critFart,
];

function playFart(fart) {
if (!canPlaySound) return;
fart.currentTime = 0;
fart.playbackRate = randomPlaybackRate();
fart.preservesPitch = false;
Expand Down Expand Up @@ -125,8 +129,23 @@ function fireEvents() {
}
}

function initializeButton() {
canPlaySound = false;
for (const event of eventsTable.reverse()) {
if (event.onCount > counter) {
break;
}

event.action();
}

eventsTable.reverse();
canPlaySound = true;
}


let shaking = false;
let counter = 0; // TODO: DONT FORGET TO SET TO 0 ON RELEASE!!!
let counter = Number(localStorage.getItem(`${STORAGE_KEY}counter`)) || 0;

function finishFart() {
shaking = false;
Expand All @@ -136,11 +155,16 @@ for (let fart of farts) {
fart.onended = finishFart;
}

function updatePopup() {
popupText.innerText = counter + "🍑💨";
}

// TODO: change it to onmousedown (it stopped working after separating button and label)
clickMe.onclick = () => {
counter += 1;
popupText.innerText = counter + "🍑💨";
localStorage.setItem(`${STORAGE_KEY}counter`, counter);
fireEvents();
updatePopup();
};

let prevTimestamp = 0;
Expand All @@ -163,4 +187,5 @@ window.requestAnimationFrame((timestamp) => {
window.requestAnimationFrame(frame);
});

fireEvents();
initializeButton();
updatePopup();