Enable tap events

This commit is contained in:
Idrees Hassan
2025-01-02 12:09:40 -05:00
parent de4c6c72e2
commit ac6998dbfd

27
birb.js
View File

@@ -72,7 +72,7 @@ const styles = `
} }
.birb-window { .birb-window {
font-family: "Monocraft"; font-family: "Monocraft", "Courier New", monospace;
z-index: 999999999; z-index: 999999999;
position: fixed; position: fixed;
background-color: #ffecda; background-color: #ffecda;
@@ -819,14 +819,14 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
}); });
document.addEventListener("click", (e) => { onClick(document, (e) => {
timeOfLastAction = Date.now(); timeOfLastAction = Date.now();
if (e.target instanceof Node && document.querySelector("#" + MENU_EXIT_ID)?.contains(e.target)) { if (e.target instanceof Node && document.querySelector("#" + MENU_EXIT_ID)?.contains(e.target)) {
removeMenu(); removeMenu();
} }
}); });
canvas.addEventListener("click", () => { onClick(canvas, () => {
insertMenu(); insertMenu();
}); });
@@ -861,7 +861,7 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
} }
} }
if (visible && Math.random() < 1 / (60 * 3)) { if (visible && Math.random() < 1 / (60 * 3)) {
activateFeather(); // activateFeather();
} }
updateFeather(); updateFeather();
} }
@@ -980,7 +980,7 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
} }
FEATHER_ANIMATIONS.feather.draw(featherCtx, Directions.LEFT, Date.now(), theme); FEATHER_ANIMATIONS.feather.draw(featherCtx, Directions.LEFT, Date.now(), theme);
document.body.appendChild(featherCanvas); document.body.appendChild(featherCanvas);
featherCanvas.addEventListener("click", () => { onClick(featherCanvas, () => {
unlockBird(birdType); unlockBird(birdType);
removeFeather(); removeFeather();
if (document.querySelector("#" + FIELD_GUIDE_ID)) { if (document.querySelector("#" + FIELD_GUIDE_ID)) {
@@ -1119,7 +1119,7 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
themeElement.appendChild(themeCanvas); themeElement.appendChild(themeCanvas);
content.appendChild(themeElement); content.appendChild(themeElement);
if (unlocked) { if (unlocked) {
themeElement.addEventListener("click", () => { onClick(themeElement, () => {
switchTheme(id); switchTheme(id);
document.querySelectorAll(".birb-grid-item").forEach((element) => { document.querySelectorAll(".birb-grid-item").forEach((element) => {
element.classList.remove("birb-grid-item-selected"); element.classList.remove("birb-grid-item-selected");
@@ -1145,7 +1145,7 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
*/ */
function makeClosable(func, closeButton) { function makeClosable(func, closeButton) {
if (closeButton) { if (closeButton) {
closeButton.addEventListener("click", func); onClick(closeButton, func);
} }
document.addEventListener("keydown", (e) => { document.addEventListener("keydown", (e) => {
if (e.key === "Escape") { if (e.key === "Escape") {
@@ -1244,7 +1244,7 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
makeDraggable(document.querySelector(".birb-window-header")); makeDraggable(document.querySelector(".birb-window-header"));
let menuExit = makeElement("birb-window-exit", undefined, MENU_EXIT_ID); let menuExit = makeElement("birb-window-exit", undefined, MENU_EXIT_ID);
menuExit.addEventListener("click", () => { onClick(menuExit, () => {
removeMenu(); removeMenu();
}); });
document.body.appendChild(menuExit); document.body.appendChild(menuExit);
@@ -1308,7 +1308,7 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
return makeElement("birb-window-separator"); return makeElement("birb-window-separator");
} }
let menuItem = makeElement("birb-window-list-item", item.text); let menuItem = makeElement("birb-window-list-item", item.text);
menuItem.addEventListener("click", () => { onClick(menuItem, () => {
if (item.removeMenu) { if (item.removeMenu) {
removeMenu(); removeMenu();
} }
@@ -1317,6 +1317,15 @@ Promise.all([loadSpritesheetPixels(SPRITE_SHEET_URI), loadSpritesheetPixels(DECO
return menuItem; return menuItem;
} }
/**
* @param {Document|Element} element
* @param {(e: Event) => void} action
*/
function onClick(element, action) {
element.addEventListener("click", (e) => action(e));
element.addEventListener("touchstart", (e) => action(e));
}
/** /**
* Remove the menu from the page * Remove the menu from the page
*/ */