Skip to content

Instantly share code, notes, and snippets.

@PaulCampbell
Last active March 16, 2023 08:13
Show Gist options
  • Save PaulCampbell/cab870461fd2e455970df1c6bc173080 to your computer and use it in GitHub Desktop.
Save PaulCampbell/cab870461fd2e455970df1c6bc173080 to your computer and use it in GitHub Desktop.

Use HTML, CSS and javascript to create a web page with a piano keyboard. When you press the keys, the corresponding notes should play

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Piano Keyboard</title>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <div class="piano">
      <div class="white-keys">
        <div class="key" id="c"></div>
        <div class="key" id="d"></div>
        <div class="key" id="e"></div>
        <div class="key" id="f"></div>
        <div class="key" id="g"></div>
        <div class="key" id="a"></div>
        <div class="key" id="b"></div>
      </div>
      <div class="black-keys">
        <div class="key sharp" id="c-sharp"></div>
        <div class="key sharp" id="d-sharp"></div>
        <div class="key sharp" id="f-sharp"></div>
        <div class="key sharp" id="g-sharp"></div>
        <div class="key sharp" id="a-sharp"></div>
      </div>
    </div>
    <script src="script.js"></script>
  </body>
</html>
body {
  background-color: #f1f1f1;
}

.piano {
  display: flex;
  justify-content: center;
  margin-top: 50px;
}

.white-keys {
  display: flex;
  flex-direction: row;
}

.key {
  box-sizing: border-box;
  border: 1px solid #999;
  border-radius: 2px;
  width: 50px;
  height: 200px;
  margin-right: -1px;
  background-color: #fff;
  cursor: pointer;
}

.key:hover {
  background-color: #eee;
}

.sharp {
  position: relative;
  top: -120px;
  left: -25px;
  width: 30px;
  height: 120px;
  background-color: #000;
}

.sharp:hover {
  background-color: #444;
  cursor: pointer;
}

.black-keys {
  position: relative;
  margin-left: -25px;
}

#c-sharp {
  left: 60px;
}

#d-sharp {
  left: 150px;
}

#f-sharp {
  left: 330px;
}

#g-sharp {
  left: 420px;
}

#a-sharp {
  left: 510px;
}
const context = new AudioContext();
const keys = document.querySelectorAll('.key');

keys.forEach(key => {
  key.addEventListener('mousedown', () => {
    const note = key.id;
    const oscillator = context.createOscillator();
    oscillator.type = 'sine'; // you can try different oscillator types here
    oscillator.frequency.value = getFrequency(note);
    oscillator.connect(context.destination);
    oscillator.start();
    key.classList.add('active');
  });

  key.addEventListener('mouseup', () => {
    const oscillator = context.createOscillator();
    oscillator.type = 'sine'; // you can try different oscillator types here
    oscillator.frequency.value = getFrequency(note);
    oscillator.connect(context.destination);
    oscillator.stop();
    oscillator.disconnect();
    key.classList.remove('active');
  });
});

function getFrequency(note) {
  const frequencies = {
    'c': 261.63,
    'c-sharp': 277.18,
    'd': 293.66,
    'd-sharp': 311.13,
    'e': 329.63,
    'f': 349.23,
    'f-sharp': 369.99,
    'g': 392.00,
    'g-sharp': 415.30,
    'a': 440.00,
    'a-sharp': 466.16,
    'b': 493.88
  };
  return frequencies[note];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment