-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (83 loc) · 2.26 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
92
93
94
95
96
97
// detecting button press
var numberOfPianoButtons = (document.querySelectorAll(".piano")).length;
for (var i = 0; i < numberOfPianoButtons; i++) {
document.querySelectorAll(".piano")[i].addEventListener("click", function() {
var buttonInnerHTML = this.innerHTML;
makeSound(buttonInnerHTML);
buttonAnimation(buttonInnerHTML);
});
}
// detecting keyboard press
document.addEventListener("keydown", function(event) {
makeSound(event.key);
buttonAnimation(event.key);
});
function makeSound(key) {
switch (key) {
case "a":
var pianoA = new Audio("sounds/pianoA.mp3");
pianoA.play();
break;
case "s":
var pianoB = new Audio("sounds/pianoB.mp3");
pianoB.play();
break;
case "d":
var pianoC = new Audio("sounds/pianoC.mp3");
pianoC.play();
break;
case "f":
var pianoD = new Audio("sounds/pianoD.mp3");
pianoD.play();
break;
case "g":
var pianoE = new Audio("sounds/pianoE.mp3");
pianoE.play();
break;
case "h":
var pianoF = new Audio("sounds/pianoF.mp3");
pianoF.play();
break;
case "j":
var pianoG = new Audio("sounds/pianoG.mp3");
pianoG.play();
break;
case "k":
var pianoH = new Audio("sounds/pianoH.mp3");
pianoH.play();
break;
case "l":
var pianoI = new Audio("sounds/pianoI.mp3");
pianoI.play();
break;
case "q":
var pianoJ = new Audio("sounds/pianoJ.mp3");
pianoJ.play();
break;
case "w":
var pianoK = new Audio("sounds/pianoK.mp3");
pianoK.play();
break;
case "e":
var pianoL = new Audio("sounds/pianoL.mp3");
pianoL.play();
break;
case "r":
var pianoM = new Audio("sounds/pianoM.mp3");
pianoM.play();
break;
case "t":
var pianoN = new Audio("sounds/pianoN.mp3");
pianoN.play();
break;
default:
console.log(buttonInnerHTML);
}
}
function buttonAnimation(currentKey) {
var activeButton = document.querySelector("." + currentKey);
activeButton.classList.add("pressed");
setTimeout(function() {
activeButton.classList.remove("pressed");
}, 100);
}