-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
91 lines (85 loc) · 2.9 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
82
83
84
85
86
87
88
89
90
91
// // if we put soundHandler() then on reloading the button will be in clicked state without user interface. So its better to use without ()
// document.querySelector(".w").addEventListener("click", function soundHandler(){
// new Audio('sounds/tom-1.mp3').play();
// this.style.color = "white";
// });
// document.querySelector(".a").addEventListener("click", function soundHandler(){
// new Audio('sounds/tom-2.mp3').play();
// this.style.color = "white";
// });
// document.querySelector(".s").addEventListener("click", function soundHandler(){
// new Audio('sounds/tom-3.mp3').play();
// this.style.color = "white";
// });
// document.querySelector(".d").addEventListener("click", function soundHandler(){
// new Audio('sounds/tom-4.mp3').play();
// this.style.color = "white";
// });
// document.querySelector(".j").addEventListener("click", function soundHandler(){
// new Audio('sounds/snare.mp3').play();
// this.style.color = "white";
// });
// document.querySelector(".k").addEventListener("click", function soundHandler(){
// new Audio('sounds/kick-bass.mp3').play();
// this.style.color = "white";
// });
// document.querySelector(".l").addEventListener("click", function soundHandler(){
// new Audio('sounds/crash.mp3').play();
// this.style.color = "white";
// });
ndrums = document.querySelectorAll(".drum").length;
for (var i = 0; i < ndrums; i++) {
document.querySelectorAll(".drum")[i].addEventListener("click", function (e) {
// this.style.color = "cyan";
console.log(e)
var clickedButton = this.textContent;
console.log(clickedButton)
selectedButton(clickedButton)
animateButton(clickedButton)
});
}
// detecting keys pressed
document.addEventListener("keydown", function(event) {
var keyPressed = event.key.toLowerCase();
console.log(keyPressed);
selectedButton(keyPressed);
animateButton(keyPressed)
})
function selectedButton(buttonX)
{
switch (buttonX) {
case 'w':
new Audio('sounds/tom-1.mp3').play();
break;
case 'a':
new Audio('sounds/tom-2.mp3').play();
break
case 's':
new Audio('sounds/tom-3.mp3').play();
break
case 'd':
new Audio('sounds/tom-4.mp3').play();
break
case 'j':
new Audio('sounds/snare.mp3').play();
break
case 'k':
new Audio('sounds/kick-bass.mp3').play();
break
case 'l':
new Audio('sounds/crash.mp3').play();
break
default:
console.log(this)
}
}
function animateButton(key)
{
var activeKey = document.querySelector("." + key);
activeKey.classList.add("pressed");
activeKey.classList.add("color");
setTimeout(function (){
activeKey.classList.remove("pressed")
activeKey.classList.remove("color")
}, 500);
}