css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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="app"> </div>

<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" integrity="sha384-/rXc/GQVaYpyDdyxK+ecHPVYJSN9bmVFBvjA/9eOB+pb3F2w2N6fc5qB9Ew5yIns" crossorigin="anonymous">

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/5.1.0/react-redux.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.1/redux.min.js"></script>

<!--
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
-->

            
          
!
            
              $buttonDimension: 180px;
$drumBorderWidth: $buttonDimension/30;
$drumHeight: $buttonDimension*3 + $drumBorderWidth*2;
$drumWidth: $buttonDimension*3 + $drumBorderWidth*2;
$displayHeight: $drumHeight%($buttonDimension*5) - $drumBorderWidth*2;
$lightBlue: #66ccff;
$brightBlue: #66ffff;
$lightGreen: #66ffcc;
$lightRed: #ff6666;
$backgroundColor: $lightGreen;
$buttonBorder: 1px solid $lightBlue;
$drumBorder: $drumBorderWidth solid black;

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#app {
  font-family: 'Raleway', sans-serif;
  height: 100vh;
  background: $backgroundColor;
  display: flex;
  justify-content: center;
  align-items: center;
}

#display {
  display: flex;
  flex-direction: column;
  -moz-user-select: none;
  -ms-user-select: none;
  -webkit-user-select: none;
  user-select: none;
}

#buttonsBox {
  width: $drumWidth;
  height: $drumHeight;
  border: $drumBorder;
  border-radius: 5px;
  box-shadow: 0 0 50px;
  display: flex;
  flex-wrap: wrap;
}

.drum-pad {
  height: $buttonDimension;
  width: $buttonDimension;
  font-size: 2em;
  background: white;
  border: $buttonBorder;
  cursor: pointer;
  &:hover {
    background: $brightBlue;
  }
}

#description {
  margin-top: 20px;
  font-size: 2em;
  color: black;
  background: white;
  display: flex;
  justify-content: center;
  align-items: center;
  height: $buttonDimension/2;
  width: $drumWidth;
  border: $drumBorder;
  border-radius: 5px;
  box-shadow: 0 0 100px;
}
            
          
!
            
              const { render } = ReactDOM;
const { Provider, connect } = ReactRedux;
const { createStore, combineReducers, bindActionCreators } = Redux;

// Components

class App extends React.Component {
  constructor(props) {
    super(props);
    
  }
  render() {
    return (
      <div id="drum-machine">
          <DrumPadContainer />
      </div>
    )
  } 
} // end class App

class DrumPad extends React.Component {
  constructor(props) {
    super(props);
    
    this.handleClick = this.handleClick.bind(this);
    this.handleKeyPress = this.handleKeyPress.bind(this);
    this.playSound = this.playSound.bind(this);
    this.playSoundById = this.playSoundById.bind(this);
    this.createDrumPad = this.createDrumPad.bind(this);
  }
  
  componentDidMount() {
    document.addEventListener('keydown', this.handleKeyPress);
  }
  componentWillUnmount() {
    document.removeEventListener('keydown', this.handleKeyPress);
  }
  
  handleClick(key) {
    this.props.clickKey(key);
    this.playSoundById(key);
  }
  
  handleKeyPress(input) {
    const play = this.props.keys.find( (key) => {
      if (input.keyCode == key.keyCode) {
        this.props.pressKey(key);
        this.playSoundById(key);
      }
    })
  }
               
  playSound(key) {
    let audio = new Audio(key.url);
    audio.play();
  }
  
  playSoundById(key) {
    let audio = document.getElementById(key.keyTrigger);
    audio.play();
  }
  
  createDrumPad() {
    return this.props.keys.map( (key) => {
      return (
        <button id={key.id} className="drum-pad" key={key.id} onClick={ () => this.handleClick(key) }>
          <audio id={key.keyTrigger} className="clip" src={key.url} />
          {key.keyTrigger}
        </button>
        )
    })
  }
  
  render() {
    return (
      <div id="display">
        <div id="buttonsBox">{this.createDrumPad()}</div>
        <div id="description">{this.props.activeKey.id}</div>
      </div>
    )
  }
} // end class DrumPad

// Actions

const KEY_CLICKED = "KEY_CLICKED";
const KEY_PRESSED = "KEY_PRESSED";

const clickKey = (key) => {
  console.log("You have clicked on: ", key);
  return {
    type: KEY_CLICKED,
    payload: key
  }
};

const pressKey = (key) => {
  console.log("You have pressed on: ", key);
  return {
    type: KEY_PRESSED,
    payload: key
  }
};

const mapStateToProps = (state, props) => {
  return {
    keys: state.keys,
    activeKey: state.activeKey
  }
}

const mapDispatchToProps = (dispatch, props) => {
  return bindActionCreators(
    {clickKey: clickKey,
    pressKey: pressKey},
    dispatch);
}

const DrumPadContainer = connect(mapStateToProps, mapDispatchToProps)(DrumPad);

// Reducers

const keysReducer = () => [
  {
    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'
  }
];

const activeKeyReducer = (state = null, action) => {
  switch (action.type) {
    case KEY_CLICKED:
      console.log(action.payload);
      return action.payload;
    case KEY_PRESSED:
      console.log(action.payload);
      return action.payload;
    default:
      return state;
  }  
};

const allReducers = combineReducers({
  keys: keysReducer,
  activeKey: activeKeyReducer
});

const initialState = {
  activeKey: ""
};

const allEnhancers = (
  window.devToolsExtension && window.devToolsExtension()
);

const store = createStore(
  allReducers,
  initialState,
  allEnhancers
);

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('app'));
            
          
!
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.
Loading ..................

Console