Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

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

CSS

              
                body {
  margin: 0px;
  background-color: #3a3838;
}

#root {
  height: 100vh;
  display: grid;
  grid-template-columns: 1fr;
  justify-items: center;
  align-items: center;
}

.container {
  border: 10px solid black;
  padding: 15px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background-color: #c2ffd7;
  /* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#ffffff+0,f3f3f3+50,ededed+51,ffffff+100;White+Gloss+%232 */
background: #ffffff; /* Old browsers */
background: -moz-linear-gradient(top, #ffffff 0%, #f3f3f3 50%, #ededed 51%, #ffffff 100%); /* FF3.6-15 */
background: -webkit-linear-gradient(top, #ffffff 0%,#f3f3f3 50%,#ededed 51%,#ffffff 100%); /* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to bottom, #ffffff 0%,#f3f3f3 50%,#ededed 51%,#ffffff 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ffffff',GradientType=0 ); /* IE6-9 */
}

.logo {
  align-self: flex-end;
  font-weight: bold;
}

.board {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.keyboard {
  min-width: 290px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

.drum-pad {
  
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 15px;
  border-radius: 10px;
  font-size: 1.5em;
  font-weight: bold;
  margin: 5px;
  background-color: #f7f2f2;
  -webkit-box-shadow: 0px -1px 11px 2px rgba(10,9,9,1);
  -moz-box-shadow: 0px -1px 11px 2px rgba(10,9,9,1);
  box-shadow: 0px -1px 11px 2px rgba(10,9,9,1);
  cursor: pointer;
}

.drum-pad:active {
  transform: scale(0.9);
  transition: .3s;
}

.key:hover {
  -webkit-box-shadow: -2px 2px 19px 2px rgba(214,26,26,1);
  -moz-box-shadow: -2px 2px 19px 2px rgba(214,26,26,1);
  box-shadow: -2px 2px 19px 2px rgba(214,26,26,1);
  background-color: #ffe8e8;
  border: none;
  cursor: pointer;
}

.pane {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  margin: 10px;
  padding: 10px;
  border: 3px solid b;
  border-radius: 10px;
  border: 2px solid black;
  -webkit-box-shadow: 0px -1px 4px 1px rgba(3,3,3,1);
  -moz-box-shadow: 0px -1px 4px 1px rgba(3,3,3,1);
  box-shadow: 0px -1px 4px 1px rgba(3,3,3,1);
}

.power {
  margin: 5px;
  font-size: 2em;
  color: green;
  font-weight: bold;
}

.power:hover {
  cursor: pointer;
}

.red {
  color: red;
}

.display {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 20%;
  width: 95%;
  margin: 2px;
  border: 1px solid black;
  font-weight: bold;
  background-color: silver;
}

.volumecontainer {
  width: 100%;
}

.volume {
  -webkit-appearance: none;
  width: 100%;
  height: 4px;
  margin-top: 15px;
  background: #d3d3d3;
  outline: none;
  opacity: 0.7;
  -webkit-transition: .2s;
  transition: opacity .2s;
  border: 1px solid black;
  -webkit-box-shadow: 0px -1px 4px 1px rgba(3,3,3,1);
  -moz-box-shadow: 0px -1px 4px 1px rgba(3,3,3,1);
  box-shadow: 0px -1px 4px 1px rgba(3,3,3,1);
}

.volume:hover {
  opacity: 1;
  cursor: pointer;
}

.volume::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 10px;
  height: 20px;
  background: black;
  cursor: pointer;
}

.volume::-moz-range-thumb {
  width: 10px;
  height: 20px;
  background: #4CAF50;
  cursor: pointer;
}

.bankContainer {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  font-weight: bold;
}

.bank {
  height: 15px;
  min-width: 35px;
  border: 2px solid black;
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin: 5px;
}

.bank:hover {
  cursor: pointer;
}

.toggle {
  width: 50%;
  height: 100%;
  background-color: black;
  margin: 1px;
}

.left {
  align-self: flex-start;
}

.right {
  align-self: flex-end;
}

@media only screen and (max-width: 600px) {
  .board {
    display: grid;
    grid-template-columns: 1fr;
  }
  
  .keyboard {
    max-width: 250px;
  }
  
  .pane {
    min-height: 200px;
  }
  
  .display {
    min-height: 20px;
  }
  
}
              
            
!

JS

              
                const domContainer = document.getElementById("root");

const keyMap = {
    
    Q: {
      names: ['Heater 1', 'Chord 1'],
      urls: ['https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3','https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3']
      
    },
    W: {
      names: ['Heater 2', 'Chord 2'],
      urls: ['https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3','https://s3.amazonaws.com/freecodecamp/drums/Chord_2.mp3']
      
    },
    E: {
      names: ['Heater 3', 'Chord 3'],
      urls: ['https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3','https://s3.amazonaws.com/freecodecamp/drums/Chord_3.mp3']
      
    },
    A: {
      names: ['Heater 4', 'Shaker'],
      urls: ['https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3','https://s3.amazonaws.com/freecodecamp/drums/Give_us_a_light.mp3']
      
    },
    S: {
      names: ['Clap', 'Open-HH'],
      urls: ['https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3','https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3']
      
    },
    D: {
      names: ['Clap', 'Open-HH'],
      urls: ['https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3','https://s3.amazonaws.com/freecodecamp/drums/Bld_H1.mp3']
      
    },
    Z: {
      names: ["Kick-n'-Hat", 'Punchy-Kick'],
      urls: ['https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3','https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3']
      
    },
    X: {
      names: ["Kick", 'Side-Stick'],
      urls: ['https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3','https://s3.amazonaws.com/freecodecamp/drums/side_stick_1.mp3']
      
    },
    C: {
      names: ["Closed-HH", 'Snare'],
      urls: ['https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3','https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3']
      
    }
    
  };

class App extends React.Component {
  
  constructor(props){
    super(props);
    this.state = {
       power: true,
       bank: 0,
       volume: 30,
       key: '',
       text: '',
       url: ''
    }
  }
 
  
  handlePowerSwitch = (e) => {
    this.setState({power: !this.state.power, key:'', text:''})
  }
  
  handleVolumeChange = (e) => {
    this.setState({volume: e.target.value, text: this.state.power ? 'Volume: '+e.target.value : ''})
  }
  
  handleVolumeRelease = (e) => {
    this.setState({text: '', volume: e.target.value})
  }
  
  handleKeyClicked = (keyName, e) => {
    
     if(this.state.power ){
    
      this.setState({key: keyName}, () => {
      let url = keyMap[this.state.key].urls[this.state.bank];
      let name = keyMap[this.state.key].names[this.state.bank];
      
      console.log(url, name, keyName);
      this.setState({text: name, url: url}, () => {
        let audio = document.getElementById(keyName);
        audio.play();
      });   

    });
       
     }
    
  }
  
  handleBankToggle = (e) => {
    this.setState({bank: this.state.bank ? 0 : 1,
                  text: !this.state.power ? '' : this.state.bank ? 'Heater Kit' : 'Smooth Piano Kit'})
    console.log(this.state.bank, this.state.text)
  }
  
  render () {
    
    return (
      
      <div id="drum-machine" className="container">
        
        <div className="logo">
          <i className="fa fa-free-code-camp"></i>
        </div>
        
        
        <div className="board">
          <Keyboard 
          keys={['Q', 'W', 'E', 'A', 'S', 'D', 'Z', 'X', 'C']}
          onKeyPress={this.handleKeyClicked}
          onClick={this.handleKeyClicked}
          volume={this.state.volume}
          bank={this.state.bank}
          url={this.state.url}
          />
        
        <div className="pane">
            <div className={this.state.power ? "power" : "power red"} onClick={this.handlePowerSwitch}>
              <i className="fa fa-power-off"></i>
            </div>
            <div id="display" className="display">
                {this.state.text}
            </div>
            <div class="volumecontainer">
                <input type="range" min="1" max="100" value={this.state.volume} className="volume" id="myRange" onChange={this.handleVolumeChange} onMouseUp={this.handleVolumeRelease}/>
          </div>
          <div className="bankContainer">
            <p>Bank:</p>
            <div className="bank" onClick={this.handleBankToggle}>
              <div className={this.state.bank ? "toggle right" : "toggle left"} >
                
              </div>
            </div>
          </div>
        </div>
        </div>
              </div>
      
    )
  }
  
}

/**
  Key Component
*/
const Key = (props) => {
  
  return (
    
    <div id={props.name+"-drumpad"} className="drum-pad" onClick={props.onClick} onKeyPress={props.onKeyPress}>
      {props.name}
      <audio className="clip" id={props.name} volume={props.volume} src={props.url} type="audio/mp3"></audio>  

    </div>
    
  )
} 

/**
  Keyboard Component
*/
const Keyboard = (props) => {
  
  return (
     
     <div className="keyboard">
        {props.keys.map(
          (k, index) => <Key key={index} name={k} volume={props.volume} url={keyMap[k].urls[props.bank]} onClick={(e) => props.onClick(k, e)} onKeyPress={(e) => props.onKeyPress(k, e)}/>
        ) }
     </div>
  
  )
  
}

ReactDOM.render(<App />, domContainer)
              
            
!
999px

Console