Add visual and vibration feedback to buttons

This commit is contained in:
Unrud 2023-04-25 01:38:51 +02:00
parent 20cea84056
commit 4285f6aa1b
3 changed files with 23 additions and 0 deletions

View file

@ -74,3 +74,9 @@ export const addPointerlockchangeEventListener = (listener) => {
document.addEventListener("pointerlockchange", listener); document.addEventListener("pointerlockchange", listener);
} }
}; };
export const vibrate = (pattern) => {
if (navigator.vibrate) {
navigator.vibrate(pattern);
}
};

View file

@ -23,7 +23,9 @@ import Touchpad from "./touchpad.mjs";
import * as compat from "./compat.mjs"; import * as compat from "./compat.mjs";
const IGNORE_CLICK_AFTER_TOUCH_DURATION = 1000; // milliseconds const IGNORE_CLICK_AFTER_TOUCH_DURATION = 1000; // milliseconds
const CLICK_VIBRATION_PATTERN = [10];
const buttons = document.querySelectorAll("button");
const scenes = document.querySelectorAll("body > .scene"); const scenes = document.querySelectorAll("body > .scene");
const openingScene = document.getElementById("opening"); const openingScene = document.getElementById("opening");
const closedScene = document.getElementById("closed"); const closedScene = document.getElementById("closed");
@ -60,6 +62,9 @@ export default class UI {
window.addEventListener("popstate", () => { this.#update(); }); window.addEventListener("popstate", () => { this.#update(); });
compat.addFullscreenchangeEventListener(() => { this.#update(); }); compat.addFullscreenchangeEventListener(() => { this.#update(); });
compat.addPointerlockchangeEventListener(() => { this.#update(); }); compat.addPointerlockchangeEventListener(() => { this.#update(); });
for (const button of buttons) {
button.addEventListener("click", this.#handleButtonClick.bind(this));
}
this.#update(); this.#update();
} }
@ -95,6 +100,12 @@ export default class UI {
sessionStorage.setItem("text-input", textInput.value); sessionStorage.setItem("text-input", textInput.value);
} }
#handleButtonClick(event) {
event.target.classList.add("click");
setTimeout(() => event.target.classList.remove("click"), 0);
compat.vibrate(CLICK_VIBRATION_PATTERN);
}
#handleSendText() { #handleSendText() {
if (textInput.value) { if (textInput.value) {
// normalize line endings // normalize line endings

View file

@ -86,12 +86,18 @@ button {
font-family: "icon"; font-family: "icon";
overflow: hidden; overflow: hidden;
cursor: pointer; cursor: pointer;
transition: box-shadow 0.2s ease-out;
} }
button:focus { button:focus {
outline: none; outline: none;
} }
button.click {
box-shadow: 0 0 1rem white inset;
transition: none;
}
button.top, button.top,
button.bottom, button.bottom,
button.left, button.left,