Make working piano using HTML + JS (one file)
- User
Make working piano using HTML + JS (one file)
- Assistant
Make working piano using HTML + JS (one file)
Here's a simple virtual piano implemented using HTML and JavaScript in a single file. Save the following code as an .html file and open it in a modern web browser to try it out:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Virtual Piano</title>
<style>
body {
background-color: #222;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.piano {
position: relative;
width: 336px; /* 8 white keys * 42px per key */
}
.key {
width: 40px;
height: 200px;
margin: 1px;
border: 1px solid black;
background: white;
position: relative;
z-index: 1;
float: left;
box-sizing: border-box;
}
.key.black {
width: 30px;
height: 120px;
background: black;
position: absolute;
z-index: 2;
margin: 0;
}
.key:active,
.key.active {
background: #ccc;
}
.key.black:active,
.key.black.active {
background: #555;
}
</style>
</head>
<body>
<div class="piano">
<!-- White keys -->
<div class="key" data-note="C4" style="left: 0px;"></div>
<div class="key" data-note="D4" style="left: 42px;"></div>
<div class="key" data-note="E4" style="left: 84px;"></div>
<div class="key" data-note="F4" style="left: 126px;"></div>
<div class="key" data-note="G4" style="left: 168px;"></div>
<div class="key" data-note="A4" style="left: 210px;"></div>
<div class="key" data-note="B4" style="left: 252px;"></div>
<div class="key" data-note="C5" style="left: 294px;"></div>
<!-- Black keys -->
<div class="key black" data-note="C#4" style="left: 27px;"></div>
<div class="key black" data-note="D#4" style="left: 69px;"></div>
<!-- No black key between E and F -->
<div class="key black" data-note="F#4" style="left: 153px;"></div>
<div class="key black" data-note="G#4" style="left: 195px;"></div>
<div class="key black" data-note="A#4" style="left: 237px;"></div>
</div>
<script>
// Function to map note names to frequencies
function getFrequency(note) {
const noteFreq = {
'C4': 261.63,
'C#4': 277.18,
'D4': 293.66,
'D#4': 311.13,
'E4': 329.63,
'F4': 349.23,
'F#4': 369.99,
'G4': 392.00,
'G#4': 415.30,
'A4': 440.00,
'A#4': 466.16,
'B4': 493.88,
'C5': 523.25
};
return noteFreq[note];
}
// Initialize audio context
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContext();
// Function to play a note
function playNote(freq) {
const oscillator = audioCtx.createOscillator();
const gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
oscillator.type = 'sine';
oscillator.frequency.value = freq;
gainNode.gain.setValueAtTime(1, audioCtx.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 1);
oscillator.start(audioCtx.currentTime);
oscillator.stop(audioCtx.currentTime + 1);
}
// Add event listeners to keys
const keys = document.querySelectorAll('.key');
keys.forEach(function(key) {
function startNote() {
const note = this.dataset.note;
const freq = getFrequency(note);
playNote(freq);
this.classList.add('active');
}
function endNote() {
this.classList.remove('active');
}
key.addEventListener('mousedown', startNote);
key.addEventListener('mouseup', endNote);
key.addEventListener('mouseleave', endNote);
// Touch events for mobile devices
key.addEventListener('touchstart', startNote);
key.addEventListener('touchend', endNote);
key.addEventListener('touchcancel', endNote);
});
</script>
</body>
</html>
Instructions:
Save the Code: Copy the code above into a text editor and save the file with an .html extension (e.g., piano.html).
Open in Browser: Double-click the saved file or open it using a modern web browser (such as Chrome, Firefox, or Edge).
Play the Piano: Click on the piano keys with your mouse or tap them if you're on a touchscreen device. The keys will produce sounds corresponding to their musical notes.
Features:
Interactive Keys: Both mouse and touch events are supported, allowing interaction on desktops and mobile devices.
Visual Feedback: Keys change color when pressed to provide visual feedback.
Web Audio API: Uses the Web Audio API to generate sound frequencies for each note without external audio files.
Notes:
Feel free to experiment with the code by adding more keys, changing the waveform type ('sine', 'square', 'sawtooth', 'triangle'), or adjusting the styling to suit your preferences.
Not yet included in a published release.