cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              .drum-pad{
  width: 150px;
  height: 50px;
  background-color: black;
  box-sizing: border-box;
  border: 2px solid white;
  color: white;
  text-align: center;
}

#drum-machine{
  width: 150px;
  margin: 0 auto;
}

#display {
  margin-top: 20px;
}
            
          
!
            
              const project_name = 'drum-machine';
var DrumMachine = React.createClass({
  getDefaultProps: function(){
    return {kit: [
            {id: 'chord-1',
             command: 'Q',
             sound: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3'},
            {id: 'chord-2',
              command: 'W',
              sound: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_2.mp3'}, 
            {id: 'chord-3', 
              command: 'E',
              sound: 'https://s3.amazonaws.com/freecodecamp/drums/Chord_3.mp3'},
            {id: 'heater-1',
             command: 'A',
             sound: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3'},
            {id: 'heater-2',
              command: 'S',
              sound: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3'},
            {id: 'heater-3',
              command: 'D',
              sound: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3' },
            {id: 'heater-6',
              command: 'Z',
              sound: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3'},
            {id: 'high-hat-kick',
             command: 'C',  // TO PASS TESTS, X NEEDS TO BE HERE OR TEST 6 FAILS
             sound: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3'},
            {id: 'heater-4',
             command: 'X', // TO PASS TESTS, C NEEDS TO BE HERE OR TEST 6 FAILS
             sound: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3'}
            ]
           }
  },
  getInitialState: function(){
    return {
      message: "Let's Play!"
    }
  },
  showMessage: function(message){
    this.setState({
      message: message
    })
  },
  render: function(){
    return <div>
              <DisplayContainer message={this.state.message}/>
              <DrumKit kit={this.props.kit} showMessage={this.showMessage}/>
           </div>
  }
})

var DrumKit = React.createClass({
  render: function(){
    var kit = this.props.kit;
    var self = this;
    return (
      <div className='drum-kit'>
        {kit.map(function(drum,index){
          return(
            <Drum key={drum.id}
                  id={drum.id}
                  sound={drum.sound}
                  command={drum.command}
                  keyCommand={drum.keyCommand}
                  showMessage={self.props.showMessage}/>
          )
        })
        }
      </div>
    )
  }
})

var Drum = React.createClass({
  componentDidMount: function(){
    window.addEventListener("keydown", this.handleKeyDown);
  },
  componentWillUnmount: function(){
    window.removeEventListener("keydown", this.handleKeyDown);
  },
  handleKeyDown: function(key){
    if(key.keyCode === 81 && this.props.command === 'Q') {
      this.playSound();  
    }
    else if (key.keyCode === 88 && this.props.command ==='X') {
      this.playSound();
    }
    else if (key.keyCode === 87 && this.props.command === 'W'){
      this.playSound();
    }
    else if (key.keyCode === 69 && this.props.command === 'E') {
      this.playSound();
    }
    else if (key.keyCode === 65 && this.props.command === 'A') {
      this.playSound();
    }
    else if (key.keyCode === 83 && this.props.command === 'S') {
      this.playSound();
    }
    else if (key.keyCode === 68 && this.props.command === 'D') {
      this.playSound();
    }
    else if (key.keyCode === 90 && this.props.command === 'Z') {
      this.playSound();
    }
    else if (key.keyCode === 67 && this.props.command === 'C') {
      this.playSound();
    }
  },  
  playSound: function(){
    var command = this.props.command;
    var message = this.props.id;
    var audioElement = document.getElementById(command);
    audioElement.currentTime=0;
    audioElement.play();
    this.props.showMessage(message);
  },
  render: function(){
    return (
      <div className={'drum-pad'}
           id={this.props.id}
           onClick={this.playSound}
           onKeyPress={this.handleKeyDown}>{this.props.command}
        <audio src={this.props.sound} 
              type='audio/mpeg'
              className='clip'
              id={this.props.command}></audio>
      </div>
    )
  }
})

var DisplayContainer = React.createClass({
  render: function(){
    return <div id='display'>{this.props.message}
           </div>
  }
})

ReactDOM.render(<DrumMachine />, document.getElementById('drum-machine'));
            
          
!
999px
Loading ..................

Console