-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
94 lines (84 loc) · 3.1 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>varma01 - drums</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🥁</text></svg>">
</head>
<body>
<img class="bg-img" src="bg.jpg" alt="Background image: people in a club partying">
<div class="keys">
<button data-key="65" class="key" onclick="playSoundOnClick(this)">
<kbd>A</kbd>
<span class="sound">clap</span>
</button>
<button data-key="83" class="key" onclick="playSoundOnClick(this)">
<kbd>S</kbd>
<span class="sound">hihat</span>
</button>
<button data-key="68" class="key" onclick="playSoundOnClick(this)">
<kbd>D</kbd>
<span class="sound">kick</span>
</button>
<button data-key="70" class="key" onclick="playSoundOnClick(this)">
<kbd>F</kbd>
<span class="sound">openhat</span>
</button>
<button data-key="71" class="key" onclick="playSoundOnClick(this)">
<kbd>G</kbd>
<span class="sound">boom</span>
</button>
<button data-key="72" class="key" onclick="playSoundOnClick(this)">
<kbd>H</kbd>
<span class="sound">ride</span>
</button>
<button data-key="74" class="key" onclick="playSoundOnClick(this)">
<kbd>J</kbd>
<span class="sound">snare</span>
</button>
<button data-key="75" class="key" onclick="playSoundOnClick(this)">
<kbd>K</kbd>
<span class="sound">tom</span>
</button>
<button data-key="76" class="key" onclick="playSoundOnClick(this)">
<kbd>L</kbd>
<span class="sound">tink</span>
</button>
</div>
<audio data-key="65" src="sounds/clap.wav"></audio>
<audio data-key="83" src="sounds/hihat.wav"></audio>
<audio data-key="68" src="sounds/kick.wav"></audio>
<audio data-key="70" src="sounds/openhat.wav"></audio>
<audio data-key="71" src="sounds/boom.wav"></audio>
<audio data-key="72" src="sounds/ride.wav"></audio>
<audio data-key="74" src="sounds/snare.wav"></audio>
<audio data-key="75" src="sounds/tom.wav"></audio>
<audio data-key="76" src="sounds/tink.wav"></audio>
<script>
const bgimg = document.querySelector(".bg-img")
let timer;
function removeTransition(e) {
if (e.propertyName !== 'transform') return;
e.target.classList.remove('playing');
}
function playSound(e) {
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
const key = document.querySelector(`button[data-key="${e.keyCode}"]`);
if (!audio) return;
key.classList.add('playing');
audio.currentTime = 0;
audio.play();
bgimg.style.animationPlayState = "running"
if (timer) clearTimeout(timer)
timer = setTimeout(()=>bgimg.style.animationPlayState = "paused", 1000)
}
function playSoundOnClick(e) {
playSound({keyCode: e.dataset.key})
}
const keys = Array.from(document.querySelectorAll('.key'));
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
document.addEventListener('keydown', playSound);
</script>
</body>
</html>