-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
28 lines (21 loc) · 825 Bytes
/
Copy pathscript.js
File metadata and controls
28 lines (21 loc) · 825 Bytes
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
function playSound(event) {
const key = document.querySelector(`div[data-key='${event.keyCode}']`);
const audio = document.querySelector(`audio[data-key='${event.keyCode}']`);
if (!audio) return;
const root = document.querySelector(":root");
const red = Math.round(Math.random() * 255);
const blue = Math.round(Math.random() * 255);
const green = Math.round(Math.random() * 255);
root.style.setProperty("--color", `rgb(${red}, ${green}, ${blue})`);
audio.currentTime = 0;
key.classList.add("playing");
audio.play();
}
function removeClass(event) {
event.target.classList.remove("playing");
}
const keys = document.querySelectorAll(".key");
keys.forEach((key) => {
key.addEventListener("transitionend", removeClass);
});
window.addEventListener("keydown", playSound);