This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
81 lines (65 loc) · 2.15 KB
/
index.js
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
77
78
79
80
81
// this is for detecting button which is pressed
var numberOfDrumButtons = document.querySelectorAll(".drum").length; // length of class with .Drum
function setUpListeners() {
for (var i = 0; i < numberOfDrumButtons; i++) {
document.querySelectorAll(".drum")[i].addEventListener("click", function() {
var buttonInnerHtml = this.innerHTML;
makeSound(buttonInnerHtml);
buttonAnimation(buttonInnerHtml);
document.getElementsByTagName('Body')[0].classList.add('shake');
setTimeout(function() {
document.getElementsByTagName('Body')[0].classList.remove('shake');
}, 200);
});
}
// if key is presseed then also it will play
document.addEventListener("keydown", function(event) {
makeSound(event.key);
buttonAnimation(event.key);
document.getElementsByTagName('Body')[0].classList.add('shake');
setTimeout(function() {
document.getElementsByTagName('Body')[0].classList.remove('shake');
}, 200);
});
}
const promises = [];
const getAudioObj = (name) => {
const aud = new Audio(`sounds/${name}.mp3`);
aud.preload = "auto";
promises.push(
new Promise((resolve, reject) => {
aud.onloadeddata = resolve;
aud.onerror = reject;
})
);
return aud;
}
const sounds = {
w: getAudioObj("tom1"),
a: getAudioObj("tom2"),
s: getAudioObj("tom3"),
d: getAudioObj("tom4"),
j: getAudioObj("snare"),
k: getAudioObj("crash"),
l: getAudioObj("kick-bass"),
};
function makeSound(key) {
if (Object.keys(sounds).includes(key)) sounds[key].cloneNode().play();
}
function buttonAnimation(currentkey) {
var activeButton = document.querySelector("." + currentkey);
activeButton.classList.add("pressed");
setTimeout(function() {
activeButton.classList.remove("pressed");
}, 100);
}
(async () => {
const loader = document.getElementById('popup');
try {
await Promise.all(promises);
setUpListeners();
} catch (e) {
alert("Unable to load audio files");
}
loader.style.display = "none";
})();