Day 20-24: Drum Machine

kemystra

Kemystra

Posted on May 1, 2022

Day 20-24: Drum Machine

The project

As the name suggest, we're gonna make a site with drum pads that play sound when triggered 🥁. The site was made with React and SASS. I figured I'll take 3 days to complete this, but circumstances pushed this to 4 days. Life always get in the way, so I should be more prepared about it 💪

What I learned

  • How to dynamically render components in React

We can use array and its methods, such as Array.map():

render() {
    const soundArr = ["blip", "clan", "clap", "cymbal", "kclick", "piano1", "piano2", "snaps", "whoop"];
    const keyArr = ["Q", "W", "E", "A", "S", "D", "Z", "X", "C"];

    return (
        <div id="drum-machine">
            <p id="drum-title">Drum Machine</p>
            <Display />
            {soundArr.map((soundClip, index) => <DrumPad audioName={soundClip} key={keyArr[index]} cap={keyArr[index]} />)}
        </div>
    );
}
Enter fullscreen mode Exit fullscreen mode
  • onkeydown event on the document

To detect key presses on the HTML document, we can use the onkeydown event:

document.onkeydown = (e) => {
    let pressedSound = document.getElementById(e.key.toUpperCase());
    if(pressedSound) {
        pressedSound.play();
        document.getElementById("display").innerText = pressedSound.parentElement.id;
}
Enter fullscreen mode Exit fullscreen mode

Note that there used to be onkeypress event, but it is now deprecated.

  • Add gradient overlay over background image

We can use background-image CSS property for this:

background-image: linear-gradient(180deg, rgba(0,255,233,0.0) 0%, rgba(0,0,0,1) 100%), 
    url('https://t4.ftcdn.net/jpg/03/58/36/11/360_F_358361101_z9baaIEmkyst0OQ8mov82r9hen7glZGb.jpg');
Enter fullscreen mode Exit fullscreen mode

Afterwords

It's nearing the end of Ramadan, which means preparation for Eid is incoming. It will be a very busy week. Hope I can still continue doing this throughout Eid celebration.

Anyway, happy Eid for Muslims around the world! ✨

Follow me on Github!
Also on Twitter!

💖 💪 🙅 🚩
kemystra
Kemystra

Posted on May 1, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 16: Final SASS touch-up
100daysofcode Day 16: Final SASS touch-up

April 21, 2022

Day 13: Preparing React Practice
100daysofcode Day 13: Preparing React Practice

April 18, 2022

Day 14: Random Quote Machine
100daysofcode Day 14: Random Quote Machine

April 19, 2022

Day 8: Almost finished with React
100daysofcode Day 8: Almost finished with React

April 13, 2022

Day 20-24: Drum Machine
100daysofcode Day 20-24: Drum Machine

May 1, 2022