- App to create a simple drum machine using React. This was an objective for FreeCodeCamp. Uses babel javascript transpiler.
- Note: to open web links in a new window use: ctrl+click on link
FCC Objective: Build a CodePen.io app using HTML, JavaScript, SCSS and React.
Requirements:
- User Story #1: I should be able to see an outer container with a corresponding id="drum-machine" that contains all other elements.
- User Story #2: Within #drum-machine I can see an element with a corresponding id="display".
- User Story #3: Within #drum-machine I can see 9 clickable drum pad elements, each with a class name of drum-pad, a unique id that describes the audio clip the drum pad will be set up to trigger, and an inner text that corresponds to one of the following keys on the keyboard: Q, W, E, A, S, D, Z, X, C. The drum pads MUST be in this order.
- User Story #4: Within each .drum-pad, there should be an HTML5 audio element which has a src attribute pointing to an audio clip, a class name of clip, and an id corresponding to the inner text of its parent .drum-pad (e.g. id="Q", id="W", id="E" etc.).
- User Story #5: When I click on a .drum-pad element, the audio clip contained in its child audio element should be triggered.
- User Story #6: When I press the trigger key associated with each .drum-pad, the audio clip contained in its child audio element should be triggered (e.g. pressing the Q key should trigger the drum pad which contains the string "Q", pressing the W key should trigger the drum pad which contains the string "W", etc.). User Story #7: When a .drum-pad is triggered, a string describing the associated audio clip is displayed as the inner text of the #display element (each string must be unique).
- React v16.4.0 Javascript library.
- Open html file in the browser and it will run. Open browser dev tools for more information.
- extract of
class DrumPad
//method to handle a key down event from keyboard - play HTML audio and change display
//no need to state actual keyCode
handleKeyDown = event => {
if(event.keyCode === this.props.padLetter.charCodeAt()) {
this.audio.play()
this.audio.currentTime = 0 //set time audio should play at (0 seconds)
this.props.handleDisplay(this.props.id)
}
}
- uses an array of drumAudio elements that are accessed later.
const drumAudio = [ //9 drum elements with src attribute for audio element
{ id: 'kit & hat', padLetter: 'Q', keyCode: 81, src: 'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3', type: 'audio/mp3' },
{ id: 'Bld H1', padLetter: 'W', keyCode: 87, src: 'https://s3.amazonaws.com/freecodecamp/drums/Bld_H1.mp3', type: 'audio/mp3' },
{ id: 'Dry Ohh', padLetter: 'E', keyCode: 69, src: 'https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3', type: 'audio/mp3' },
{ id: 'Punchy Kick', padLetter: 'A', keyCode: 65, src: 'https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3', type: 'audio/mp3' },
{ id: 'Side Stick', padLetter: 'S', keyCode: 83, src: 'https://s3.amazonaws.com/freecodecamp/drums/side_stick_1.mp3', type: 'audio/mp3' },
{ id: 'Heater 1', padLetter: 'D', keyCode: 68, src: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3', type: 'audio/mp3' },
{ id: 'Brk Snr', padLetter: 'Z', keyCode: 90, src: 'https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3', type: 'audio/mp3' },
{ id: 'Heater 2', padLetter: 'X', keyCode: 88, src: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3', type: 'audio/mp3' },
{ id: 'Heater 3', padLetter: 'C', keyCode: 67, src: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3', type: 'audio/mp3' },
]
- Status: Tested and 100% working.
- Front End Libraries Projects - Build a Drum Machine
- Drum Machine React Tutorial - Part 1
- Drum Machine React Tutorial - Part 2
- Drum Machine React Tutorial - Part 3
- Drum Machine React Tutorial - Part 4
- N/A
- Repo created by ABateman, email: [email protected]