Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js"></script>

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

CSS

              
                #drum-machine {
  background: hsl(0, 0%, 40%);
  width: 400px;
  margin: auto;
}

#clip-name {
  background: hsl(0, 0%, 90%);
  height: 40px;
  width: 80%;
  margin: auto;
  text-align: center;
  vertical-align: middle;
  line-height: 40px;
  font-family: Verdana;
}

#drum-pads {
  display: grid;
  grid-template-columns: auto auto auto;
  justify-items: center;
}

.drum-pad {
  background: hsl(0, 0%, 30%);
  color: white;
  font-size: 16px;
  height: 80px;
  width: 80px;
  margin: 20px;
  box-shadow: 0 0 10px 2px red;
  border-radius: 10px;
}
              
            
!

JS

              
                /* IN THIS PROJECT I LEARNT HOW TO..

  - Use `for` loops in React
  - Pass through a parent function to a child component
  - Play audio clips
  - Handle key presses from the keyboard
  
  
  - It's not pretty, I know. But I'm focusing on learning code and not visual design right now.
  
*/
const DRUM_PAD_INFO = [
  {
    id: "synth A",
    innerText: 'Q',
    keyCode: 81,
    audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3" 
  },
  {
    id: "synth B", 
    innerText: 'W',
    keyCode: 87, 
    audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3"
  },
  {
    id: "keyboard", 
    innerText: "E", 
    keyCode: 69, 
    audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3"
  },
  {
    id: "guitar", 
    innerText: "A", 
    keyCode: 65,
    audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3"
  },
  {
    id: "clap", 
    innerText: "S", 
    keyCode: 83,
    audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3"
  },
  {
    id: "cymbal", 
    innerText: "D", 
    keyCode: 68,
    audio: "https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3"
  },
  {
    id: "kick", 
    innerText: "Z", 
    keyCode: 90,
    audio: "https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3"
  },
  {
    id: "doof", 
    innerText: "X", 
    keyCode: 88,
    audio: "https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3"
  },
  {
    id: "tick", 
    innerText: "C", 
    keyCode: 67,
    audio: "https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3"
  }
];

let audioClipName = '';

class DrumPad extends React.Component {
  constructor(props) {
    super(props);
    this.playMusic = this.playMusic.bind(this);
    this.handleKeyPress = this.handleKeyPress.bind(this);
  }
  
  componentDidMount() {
    document.addEventListener('keydown', this.handleKeyPress);
  }
  
  componentWillUnmount() {
    document.removeEventListener('keydown', this.handleKeyPress);
  }
  
  playMusic() {
    let audio = document.getElementById(this.props.innerText);
    audio.play();
    this.props.updateDisplay(this.props.id);
  }
  
  handleKeyPress(event) {
    if (event.keyCode === this.props.keyCode) {
      this.playMusic();
    }
  }
  
  render() {
    return (
      <div>
        <button className='drum-pad' id={this.props.id} onClick={this.playMusic}>{this.props.innerText}
          <audio className='clip' id={this.props.innerText} src={this.props.audio}></audio>
        </button>
      </div>
    );
  }
}

DrumPad.defaultProps = {
  id: "lonerly",
  innerText: "boo hoo",
  audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3",
  keyCode: 81
}

class DrumMachine extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      clipName: '...'
    }
    this.updateClipName = this.updateClipName.bind(this);
  }
  
  updateClipName(name) {
    this.setState({
      clipName: name
    });
  }
  
  render() {
    let drumPads = [];
    for (let i=0; i<DRUM_PAD_INFO.length; i++) {
      drumPads.push(<DrumPad 
                      {...DRUM_PAD_INFO[i]}
                      updateDisplay = {this.updateClipName} />
                   );
    }
    
    return (
      <div id="drum-machine">
        <div id="display">
          <div id='clip-name'> {this.state.clipName} </div>
          <div id='drum-pads'> {drumPads} </div>
        </div>
      </div>
    );
  };
}

ReactDOM.render(<DrumMachine />, document.getElementById("app"));

              
            
!
999px

Console