<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #0F2027;
/* fallback for old browsers */
background: -webkit-linear-gradient(to bottom, #2C5364, #203A43, #0F2027);
/* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to bottom, #2C5364, #203A43, #0F2027);
/* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}
.piano {
height: 100vh;
width: 100%;
display: flex;
justify-content: center;
}
.key {
display: flex;
justify-content: center;
align-items: flex-end;
font-size: 1.2rem;
font-weight: bold;
padding-bottom: 20px;
}
.white {
width: 9%;
min-width: 60px;
height: 70%;
background-color: white;
border: 1px solid #ccc;
color: #000;
}
.black {
width: 8%;
height: 45%;
margin-left: -4%;
margin-right: -4%;
z-index: 2;
background-color: black;
color: #fff;
}
.white.active {
background-color: #ccc;
}
.black.active {
background-color: #333;
}
</style>
<title>Piano</title>
</head>
<body>
<div class="piano">
<div class="key white" data-note="C">A</div>
<div class="key black" data-note="Cs">W</div>
<div class="key white" data-note="D">S</div>
<div class="key black" data-note="Ds">E</div>
<div class="key white" data-note="E">D</div>
<div class="key white" data-note="F">F</div>
<div class="key black" data-note="Fs">R</div>
<div class="key white" data-note="G">G</div>
<div class="key black" data-note="Gs">T</div>
<div class="key white" data-note="A">H</div>
<div class="key black" data-note="As">Y</div>
<div class="key white" data-note="B">J</div>
</div>
<audio src="https://nemo0.github.io/js-piano/notes/C4.mp3" id="C"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/Cs4.mp3" id="Cs"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/D4.mp3" id="D"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/Ds4.mp3" id="Ds"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/E4.mp3" id="E"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/F4.mp3" id="F"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/Fs4.mp3" id="Fs"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/G4.mp3" id="G"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/Gs4.mp3" id="Gs"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/A4.mp3" id="A"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/As4.mp3" id="As"></audio>
<audio src="https://nemo0.github.io/js-piano/notes/B4.mp3" id="B"></audio>
<script>
const keys = document.querySelectorAll('.key');
const regulars = document.querySelectorAll('.key.white');
const sharps = document.querySelectorAll('.key.black');
const whites = ['a', 's', 'd', 'f', 'g', 'h', 'j'];
const blacks = ['w', 'e', 'r', 't', 'y']
keys.forEach(key => {
key.addEventListener('click', () => playNote(key))
})
document.addEventListener('keydown', e => {
if (e.repeat) return
const key = e.key
const whiteKeyIndex = whites.indexOf(key)
const blackKeyIndex = blacks.indexOf(key)
if (whiteKeyIndex > -1) playNote(regulars[whiteKeyIndex])
if (blackKeyIndex > -1) playNote(sharps[blackKeyIndex])
})
let playNote = (key) => {
const noteSound = document.getElementById(key.dataset.note)
noteSound.currentTime = 0
noteSound.play()
key.classList.add('active')
noteSound.addEventListener('ended', () => {
key.classList.remove('active')
})
}
</script>
</body>
</html>