Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="root"></div>

<!-- <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script> -->
            
          
!
            
              * {
  box-sizing: border-box;
}

body {
  background-image: url("https://raw.githubusercontent.com/paul-duvall/website_images/master/white-waves.png");
  background-size: repeat;
}

#drum-machine {
  background-image: linear-gradient(to right, rgba(250, 112, 154, 0.6) 0%, rgba(254, 225, 64, 0.6) 100%);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  width: 600px;
  height: 500px;
  border-radius: 5px;
  flex-direction: row;
}

.drum-machineOff {
  background-image: linear-gradient(to right, rgba(213, 213, 213, 0.8) 0%, rgba(213, 213, 213, 0.8) 100%);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  width: 600px;
  height: 500px;
  border-radius: 5px;
}

#drum-keys {
  display: flex;
  flex-wrap: wrap;
  padding: 10px;
}

.drum-pad {
  width: 90px;
  margin: 10px;
  border-radius: 5px;
  border: none;
  font-size: 25px;
  background-color: #F7F5DD;
  color: grey;
  transition: all 0.1s ease;
}

.playing {
  transform: scale(1.1);
  border-color: #744267;
  background: #6dc06644;
  box-shadow: 0 0 1rem #744267;
  color: white;
}

#display {
  margin-top: 20px;
  margin-right: 20px;
  text-align: center;
  position: relative;
}

h1 {
  font-family: 'Overpass Mono', monospace;
  position: absolute;
  bottom: 15px;
  right: 10px;
  text-align: right;
  font-size: 45px;
  margin: 0;
  color: rgba(250, 112, 154, 0.6);
  text-shadow: 0 0 12px rgba(250, 112, 154, 0.6);
  -webkit-text-fill-color: rgba(254, 225, 64, 0.6);
  -webkit-text-stroke-width: 2px;
  -webkit-text-stroke-color: rgba(250, 112, 154, 0.6);
}

/* Screen displaying the current sound being played */
.displayScreen {
  border-radius: 5px;
  width: 150px;
  height: 75px;
  margin: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  text-transform: uppercase;
  color: grey;
  font-size: 20px;
}

.displayScreenOn {
  box-shadow: 0 0 10px 2px #F3F3F3;
  background: #F7F7F7;
}

/* Applied when drum machine is off to dull screen */
.displayScreenOff {
  background: #E5E5E5;
  box-shadow: none;
}

/* On / off button */
#onButton {
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding-right: 20px;
  margin-top: 40px;
  color: grey;
}

/* Box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(250, 112, 154, 0.6);
  transition: .4s;
  border-radius: 5px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: #F7F5DD;
  transition: .4s;
  border-radius: 5px;
}

input:checked + .slider {
  background-color: rgba(133, 154, 183, 0.6);
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  transform: translateX(26px);
}

/* Tablet view */
@media only screen and (max-width: 630px) {
  #drum-machine {
    height: auto; 
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 90%;
  }
  
  .drum-machineOff {
    height: 100vh; 
    width: 100%;
  }
  
  #drum-keys {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    padding: 10px;
  }
  
  .drum-pad {
    width: 90px;
    margin: 10px;
    padding-top: 20px;
    padding-bottom: 20px;
    font-size: 20px;
  }
  
  #display {
    margin-top: 0;
    margin-right: 0;
    text-align: center;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  
  #onButton {
    width: 50%;
    padding-right: 0;
    margin-top: 10px;
    margin-bottom: 25px;
  }
  
  h1 {
    position: static;
  }
}

/* Mobile view */
@media only screen and (max-width: 400px) {  
  #drum-keys {
    padding: 5px;
  }
  
  .drum-pad {
    width: 70px;
    margin: 8px;
    padding-top: 18px;
    padding-bottom: 18px;
    font-size: 18px;
  }
}
            
          
!
            
              const sounds = [
  {
    name: 'Heater-1',
    location: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3',
    letter: 'Q',
    keycode: 113
  },
  {
    name: 'Heater-2',
    location: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3',
    letter: 'W',
    keycode: 119
  },
  {
    name: 'Heater-3',
    location: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3',
    letter: 'E',
    keycode: 101
  },
  {
    name: 'Heater-4',
    location: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3',
    letter: 'A',
    keycode: 97
  },
  {
    name: 'Clap',
    location: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3',
    letter: 'S',
    keycode: 115
  },
  {
    name: 'Open-HH',
    location: 'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3',
    letter: 'D',
    keycode: 100
  },
  {
    name: 'Kick-n-Hat',
    location: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3',
    letter: 'Z',
    keycode: 122
  },
  {
    name: 'Kick',
    location: 'https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3',
    letter: 'X',
    keycode: 120
  },
  {
    name: 'Closed-HH',
    location: 'https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3',
    letter: 'C',
    keycode: 99
  },
];

class App extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      switchOff: false,
      currentLetter: '',
      keyCode: undefined
    };  
    this.handleSound = this.handleSound.bind(this);
    this.handleKeyPress = this.handleKeyPress.bind(this);
    this.playSound = this.playSound.bind(this);
    this.switchOff = this.switchOff.bind(this);
  }
  
  componentDidMount(){
    window.addEventListener('keypress', this.handleKeyPress);
  }
  
  handleKeyPress = async (event) => {
    let keyCode = event.keyCode;
    await this.setState({ keyCode: keyCode });
    this.playSound();
  }
  
  handleSound = async (e) => {
    // Set current letter in state to match the button pressed
    const currentLetter = e.target.id;
    // Set currentLetter in state
    await this.setState({ currentLetter: currentLetter }); 
    this.playSound();
  }
  
  playSound() {
    // Loop over sounds array, finding matching sound object and play associated sound 
    if(this.state.switchOff === false) {
      sounds.forEach((sound) => {
        if(sound.letter === this.state.currentLetter || sound.keycode === this.state.keyCode){
        let drumSound = new Audio(sound.location); 
        document.getElementById(sound.letter).classList.add('playing');
        setTimeout(() => {
          document.getElementById(sound.letter).classList.remove('playing');
        }, 200);
        drumSound.play(); 
        document.querySelector('.displayScreen').innerHTML=`${sound.name}`;
      }
    });   
   }
  }

switchOff() {
  this.setState({ switchOff: !this.state.switchOff });
  
  if(this.state.switchOff === false){
    console.log('its off');
    document.querySelector('.displayScreen').classList.remove('displayScreenOn');
    document.querySelector('.displayScreen').classList.add('displayScreenOff');
    document.getElementById('drum-machineOff').classList.add('drum-machineOff');
    document.querySelector('.displayScreen').innerHTML='';
  } else {
    document.querySelector('.displayScreen').classList.add('displayScreenOn');
    document.querySelector('.displayScreen').classList.remove('displayScreenOff');
    document.getElementById('drum-machineOff').classList.remove('drum-machineOff');
  }
}
    
  render() {
    return (
      <div>
        <div id="drum-machineOff"></div>
        <div id="drum-machine">
          <div id="drum-keys">
            {sounds.map((sound) => (
            // <audio src={sound.location} className="clip" id={sound.letter}></audio> was within button
              <button 
               onClick={this.handleSound} 
               id={sound.letter}
               className="drum-pad"
             >
             {sound.letter}
            </button>
            ))}
          </div>
          <div id="display">
            <div class="displayScreen displayScreenOn"></div>
            <div id="onButton">
              <span>On</span>
              <label class="switch">  
                <input type="checkbox" onClick={this.switchOff}></input>
              <span class="slider"></span>
              </label>
            Off</div>
            <h1>Drum Machine</h1>
          </div>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<App/>, document.getElementById("root"));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console