Skip to content

Commit 1196da7

Browse files
committed
⚡️ Drum > Script implementation
1 parent 10d4aeb commit 1196da7

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

medium/Drum-Kit/index.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Button Press
2+
var n = document.querySelectorAll(".drum").length;
3+
4+
//This for loop will add event listner to all elements
5+
for (var i = 0; i < n; i++) {
6+
//After event listner is added to all elements, then the eventListner gets activated by click, and then "this" finds out which button activated the eventListner or function inside eventListner
7+
//sound stores the value of this.innerHTML and sends it to makesound function.
8+
document.querySelectorAll(".drum")[i].addEventListener("click", function () {
9+
var sound = this.innerHTML;
10+
11+
makeSound(sound);
12+
13+
buttonAnimation(sound);
14+
});
15+
}
16+
17+
// Keyboard press
18+
19+
document.addEventListener("keypress", function (even) {
20+
makeSound(even.key);
21+
22+
buttonAnimation(even.key);
23+
});
24+
25+
function makeSound(key) {
26+
switch (key) {
27+
case "w":
28+
var tom1 = new Audio("sounds/tom-1.mp3");
29+
tom1.play();
30+
break;
31+
32+
case "a":
33+
var tom2 = new Audio("sounds/tom-2.mp3");
34+
tom2.play();
35+
break;
36+
37+
case "s":
38+
var tom3 = new Audio("sounds/tom-3.mp3");
39+
tom3.play();
40+
break;
41+
42+
case "d":
43+
var tom4 = new Audio("sounds/tom-4.mp3");
44+
tom4.play();
45+
break;
46+
47+
case "j":
48+
var snare = new Audio("sounds/snare.mp3");
49+
snare.play();
50+
break;
51+
52+
case "k":
53+
var crash = new Audio("sounds/crash.mp3");
54+
crash.play();
55+
break;
56+
57+
case "i":
58+
var kickBass = new Audio("sounds/kick-bass.mp3");
59+
kickBass.play();
60+
break;
61+
62+
default:
63+
alert("somthing wrong");
64+
}
65+
}
66+
67+
function buttonAnimation(currentKey) {
68+
var activeButton = document.querySelector("." + currentKey);
69+
70+
activeButton.classList.add("pressed");
71+
72+
setTimeout(function () {
73+
activeButton.classList.remove("pressed");
74+
}, 100);
75+
}

0 commit comments

Comments
 (0)