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. 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

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

              
                @mixin flexCenter(){
  display: flex;
  justify-content: center;
  align-items: center;
}

#root{
  max-width:500px;
  margin:auto;
}

#drum-machine {
  @include flexCenter;
  flex-direction: column;
  border:2px solid black;
}

.padBoard {
  width: 50%;
  @include flexCenter;
  flex-wrap: wrap;
}

.toolBar{
  @include flexCenter;
  justify-content:space-between;
  width: 50%;
}

.power{
  border:2px solid black;
  padding:1em;
  border-radius:50%;
  cursor:pointer;
}

#display{
  width:7em;
  margin-left:3px;
  border:2px solid black;
  padding:0.1em;
}

.drum-pad {
  border: 2px solid black;
  padding: 1em;
  margin: 1em;
  cursor: pointer;
}

              
            
!

JS

              
                const bank = [{
    keyCode: 81,
    keyTrigger: 'Q',
    id: 'Heater-1',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3'
  }, {
    keyCode: 87,
    keyTrigger: 'W',
    id: 'Heater-2',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3'
  }, {
    keyCode: 69,
    keyTrigger: 'E',
    id: 'Heater-3',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3'
  }, {
    keyCode: 65,
    keyTrigger: 'A',
    id: 'Heater-4',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3'
  }, {
    keyCode: 83,
    keyTrigger: 'S',
    id: 'Clap',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3'
  }, {
    keyCode: 68,
    keyTrigger: 'D',
    id: 'Open-HH',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3'
  }, {
    keyCode: 90,
    keyTrigger: 'Z',
    id: "Kick-n'-Hat",
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3'
  }, {
    keyCode: 88,
    keyTrigger: 'X',
    id: 'Kick',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3'
  }, {
    keyCode: 67,
    keyTrigger: 'C',
    id: 'Closed-HH',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3'
  },
];

class DrumPad extends React.Component {
  constructor(props) {
    super(props);
    this.handleKeyPress = this.handleKeyPress.bind(this);
    this.playSound = this.playSound.bind(this);
  }

  componentDidMount() {
    document.addEventListener("keydown", this.handleKeyPress);
  }
  componentWillUnmount() {
    document.removeEventListener("keydown", this.handleKeyPress);
  }

  //have to separate because we also want to allow playing by mouse
  handleKeyPress(e) {
    if (e.keyCode === this.props.clipKeyCode) {
      this.playSound();
    }
  }

  playSound(e) {
    if (this.props.power) {
      const sound = document.getElementById(this.props.clipKeyTrigger);
      sound.currentTime = 0;
      sound.play();
      this.props.updateDisplay(this.props.clipId.replace("/-/g", " "));
    }
  }

  render() {
    return (
      <div className="drum-pad" id={this.props.clipId} onClick={this.playSound}>
        {this.props.clipKeyTrigger}
        <audio
          src={this.props.clipUrl}
          className="clip"
          id={this.props.clipKeyTrigger}
        />
      </div>
    );
  }
}

class PadBoard extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    let board = this.props.bank.map((object, index, array) => {
      return (
        <DrumPad
          clipId={object.id}
          clipKeyCode={object.keyCode}
          clipKeyTrigger={object.keyTrigger}
          clipUrl={object.url}
          power={this.props.power}
          updateDisplay={this.props.updateDisplay}
        />
      );
    });
    return <div className="padBoard">{board}</div>;
  }
}

class Machine extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      bank: bank,
      display: "power on",
      power: true
    };

    this.updateDisplay = this.updateDisplay.bind(this);
    this.togglePower = this.togglePower.bind(this);
  }

  togglePower() {
    let currentPower = this.state.power ? "power off" : "power on";
    this.setState({
      power: !this.state.power,
      display: currentPower
    });
  }

  updateDisplay(name) {
    if (this.state.power) {
      this.setState({
        display: name
      });
    }
  }

  render() {
    return (
      <div id="drum-machine">
        <div className="toolBar">
          <div className="power" onClick={this.togglePower}>
            <i className="fas fa-power-off" />
          </div>
          <h1 id="display">{this.state.display}</h1>
        </div>
        <PadBoard
          bank={this.state.bank}
          power={this.state.power}
          updateDisplay={this.updateDisplay}
        />
      </div>
    );
  }
}

ReactDOM.render(<Machine />, document.getElementById("root"));

              
            
!
999px

Console