Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Front End Libraries Projects - Build a Drum Machine
<head>
<title>Front End Libraries Projects - Build a Drum Machine</title>
<meta name="description" content="Build a Drum Machine">
<meta name="keywords" content="HTML, CSS, JavaScript, React.js, freeCodeCamp, Front End Libraries Projects">
<meta name="author" content="Chan Jian Hui Jonathan">
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
</head>
<!--
Hello Camper!
For now, the test suite only works in Chrome! Please read the README below in the JS Editor before beginning. Feel free to delete this message once you have read it. Good luck and Happy Coding!
- The freeCodeCamp Team
-->
<div id="root"></div>
// !! IMPORTANT README:
// You may add additional external JS and CSS as needed to complete the project, however the current external resource MUST remain in place for the tests to work. BABEL must also be left in place.
/***********
INSTRUCTIONS:
- Select the project you would
like to complete from the dropdown
menu.
- Click the "RUN TESTS" button to
run the tests against the blank
pen.
- Click the "TESTS" button to see
the individual test cases.
(should all be failing at first)
- Start coding! As you fulfill each
test case, you will see them go
from red to green.
- As you start to build out your
project, when tests are failing,
you should get helpful errors
along the way!
************/
// PLEASE NOTE: Adding global style rules using the * selector, or by adding rules to body {..} or html {..}, or to all elements within body or html, i.e. h1 {..}, has the potential to pollute the test suite's CSS. Try adding: * { color: red }, for a quick example!
// Once you have read the above messages, you can delete all comments.
const audios = {
Q: {
keypad: "Q",
audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3",
displayText: "Heater 1"
},
W: {
keypad: "W",
audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3",
displayText: "Heater 2"
},
E: {
keypad: "E",
audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3",
displayText: "Heater 3"
},
A: {
keypad: "A",
audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3",
displayText: "Heater 4"
},
S: {
keypad: "S",
audio: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
displayText: "Clap"
},
D: {
keypad: "D",
audio: "https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3",
displayText: "Open HH"
},
Z: {
keypad: "Z",
audio: "https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3",
displayText: "Kick n' Hat"
},
X: {
keypad: "X",
audio: "https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3",
displayText: "Kick"
},
C: {
keypad: "C",
audio: "https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3",
displayText: "Closed HH"
}
};
const DrumPad = props => {
const padId = props.keypad.displayText.replace(/\s/g, "-")
return (
<div onClick={props.onClick} id={padId} className="drum-pad">
<audio src={props.keypad.audio} id={props.keypad.keypad} className="clip"/>
{props.keypad.keypad}
</div>
);
};
const PadBank = props => {
return <div id="container-bank">{props.children}</div>;
};
const InterfaceBank = props => {
return <div id="container-interface">{props.children}</div>
}
const Display = props => {
return <h1 id="display">{props.display}</h1>;
};
class DrumMachine extends React.Component {
constructor() {
super();
this.state = {
display: "",
volume: 1
}
}
componentDidMount() {
document.addEventListener("keydown", this.handleKeyPress);
}
componentWillUnmount() {
document.removeEventListener("keydown", this.handleKeyPress)
}
handleKeyPress = event => {
const keypress = event.key.toUpperCase();
const elementPressed = document.getElementById(keypress).parentElement
elementPressed.click()
};
handleClick = event => {
const element = event.target
const audio = element.querySelector("audio");
const display = audios[audio.id].displayText
element.classList.toggle("drum-pad-active")
setTimeout(() => element.classList.toggle("drum-pad-active"), 100)
this.handleDisplay(display)
this.handlePlay(audio);
};
handleDisplay = display => {
this.setState({
display: display
})
};
handlePlay = audio => {
audio.currentTime = 0;
audio.volume = this.state.volume
audio.play();
};
render() {
const drumPads = Object.values(audios).map(audio => (
<DrumPad keypad={audio} key={audio.keypad} onClick={this.handleClick} />
));
return (
<div id="drum-machine">
<PadBank>{drumPads}</PadBank>
<InterfaceBank>
<Display display={this.state.display} />
</InterfaceBank>
</div>
);
}
}
ReactDOM.render(<DrumMachine />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
$background: #092834;
$borders: #FE2712;
$root: #F0F7D4;
$pad: #347B98;
$active: #B2D732;
* {
box-sizing: border-box;
margin: 0px;
font-family: sans-serif;
}
#root {
display: grid;
justify-items: center;
align-content: center;
height: 100vh;
background-color: $root;
}
#drum-machine {
background-color: $background;
width: 80%;
height: 400px;
border: 5px solid $borders;
display: grid;
grid-template-columns: 2fr 1fr;
grid-template-rows: 100%;
align-content: center;
justify-items: center;
}
.drum-pad, .drum-pad-active {
border: none;
padding: 20px;
border-radius: 5px;
height: 100%;
font-weight: bold;
font-size: 2em;
display: grid;
justify-content: center;
align-items: center;
user-select: none;
}
.drum-pad {
background-color: $pad;
}
.drum-pad-active {
background-color: $active;
}
#display {
background-color: $pad;
height: 50px;
width: 90%;
display: grid;
justify-content: center;
align-items: center;
}
#container-bank, #container-interface {
width: 100%;
display: grid;
}
#container-bank {
grid-template-columns: 30% 30% 30%;
grid-template-rows: 30% 30% 30%;
justify-content: center;
align-content: center;
grid-gap: 10px 10px;
}
#container-interface {
grid-template-columns: 100%;
justify-items: center;
align-content: center;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment