Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

CSS

              
                body{
  background-color: rgb(200,55,43);
}
#drum-machine{
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 10vh auto;
  border: 1px solid black;
  padding: 2rem;
  width: 60vw;
  border-radius: 5px;
  background-color: rgba(50,130,170,0.8);
}
#display{
  border: 1px black solid;
  height: 2.5rem;
  width: 40vw;
  background-color: white;
  font-size: x-large;
  text-align: center;
}
#buttons{
  margin-top: 3vh;
  display:flex;
  flex-direction: row;
  flex-wrap: wrap;
  max-width: 50vw;
}
button{
  padding: 0;
  margin: 0.1rem;
  background-color: rgba(200,55,43, 0.8);
  border: black solid 0.5px;
  color: white;
  font-size: x-large;
  border-radius: 4px;
  text-align: center;
  width: 15vw;
  height: 15vh;
}
label{
  margin-left: 0.5rem;
  color: white;
}
#controls{
  margin-top: 1rem;
}
              
            
!

JS

              
                import * as React from "https://cdn.skypack.dev/react@17.0.1";
import * as ReactDOM from "https://cdn.skypack.dev/react-dom@17.0.1";
const App = () => {
  const audio = [
    {
      label: "Kick",
      url: "https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3"
    },
    {
      label: "Clap",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3"
    },
    {
      label: "Heater 1",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3"
    },
    {
      label: "Open Hi Hat",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3"
    },
    {
      label: "Closed Hi Hat",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3"
    },
    {
      label: "Heater 2",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3"
    },
    {
    label: 'Heater-3',
    url: 'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3'
    },
    {
     label: "heater-4",
      url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3"
    },
    {
  label: 'Shaker',
  url: 'https://s3.amazonaws.com/freecodecamp/drums/Give_us_a_light.mp3'
    }
  ];
  const [label, setLabel] = React.useState("");
  const handleOnQClick = () => {
    const kick = document.getElementById("Q");
    if (onOff.checked == true){
    setLabel("Kick");
    kick.load();
    kick.play();
    kick.currentTime=0;
    }
  };
  const handleOnWClick = () => {
    const clap = document.getElementById("W");
    if (onOff.checked == true){
    setLabel("Clap");
    clap.load();
    clap.play();
    clap.currentTime=0;
    };
  };
  const handleOnEClick = () => {
    const snare = document.getElementById("E");
    if (onOff.checked == true){
    setLabel("Heater 1");
    snare.load();
    snare.play();
    snare.currentTime=0;
    };
  };
  const handleOnAClick = () => {
    const ohh = document.getElementById("A");
    if (onOff.checked == true){
    setLabel("Open Hi Hat");
    ohh.load();
    ohh.play();
    ohh.currentTime=0;
    };
  };
  const handleOnSClick = () => {
    const chh = document.getElementById("S");
    if (onOff.checked == true){
    setLabel("Closed Hi Hat");
    chh.load();
    chh.play();
    chh.currentTime=0;
    };
  };
  const handleOnDClick = () => {
    const cymbal = document.getElementById("D");
    if (onOff.checked == true){
    setLabel("Heater2");
    cymbal.load();
    cymbal.play();
    cymbal.currentTime=0;
    };
  };
  const handleOnZClick = () => {
    const heater3 = document.getElementById("Z");
    if (onOff.checked == true){
    setLabel("Heater 3");
    heater3.load();
    heater3.play();
    heater3.currentTime=0;
    };
  };
  const handleOnXClick = () =>{
    const heater4 = document.getElementById("X");
    if (onOff.checked == true){
    setLabel("Heater 4");
    heater4.load();
    heater4.play();
    heater4.currentTime=0;
    };
  };
  const handleOnCClick = () => {
    const shaker = document.getElementById("C");
    if (onOff.checked == true){
    setLabel("Shaker");
    shaker.load();
    shaker.play();
    shaker.currentTime=0;
    };
  };
  const handleKeydown = (e) => {
    if (e.keyCode == 81) {
      handleOnQClick();
    } else if (e.keyCode == 87) {
      handleOnWClick();
    } else if (e.keyCode == 69) {
      handleOnEClick();
    } else if (e.keyCode == 65) {
      handleOnAClick();
    } else if (e.keyCode == 83) {
      handleOnSClick();
    } else if (e.keyCode == 68) {
      handleOnDClick();
    } else if (e.keyCode == 90){
      handleOnZClick();
    } else if (e.keyCode == 88){
      handleOnXClick();
    } else if (e.keyCode == 67){
      handleOnCClick();
    }
  };
  React.useEffect(() => {document.addEventListener("keydown"                                                   , handleKeydown)},[]);
  return (
    <div id="drum-machine">
      <div id="display">{label}</div>
      <div id="controls">
        <input type="checkbox" id="onOff" />
        <label for="on-off">ON/OFF </label>
        <input type="range" id="volume" min="0" max="1" step="0.1" />
        <label for="volume">Volume</label>
      </div>
      <div id="buttons">
        <button onClick={handleOnQClick} className="drum-pad" id="kick">
          Q<audio src={audio[0].url} className="clip" id="Q">Q</audio>
        </button>
        <button onClick={handleOnWClick} className="drum-pad" id="clap">
          W<audio src={audio[1].url} className="clip" id="W">W</audio>
        </button>
        <button onClick={handleOnEClick} className="drum-pad" id="snare">
          E<audio src={audio[2].url} className="clip" id="E">E</audio>
        </button>
        <button onClick={handleOnAClick} className="drum-pad" id="ohh">
          A<audio src={audio[3].url} className="clip" id="A">A</audio>
        </button>
        <button onClick={handleOnSClick} className="drum-pad" id="chh">
          S<audio src={audio[4].url} className="clip" id="S">S</audio>
        </button>
        <button onClick={handleOnDClick} className="drum-pad" id="cymbal">
          D<audio src={audio[5].url} className="clip" id="D">D</audio>
        </button>
        <button onClick={handleOnZClick} className="drum-pad" id="heater3">
          Z<audio src={audio[6].url} className="clip" id="Z">Z</audio>
        </button>
        <button onClick={handleOnXClick} className="drum-pad" id="heater4">
          X<audio src={audio[7].url} className="clip" id="X">X</audio>
        </button>
        <button onClick={handleOnCClick} className="drum-pad" id="shaker">
          C<audio src={audio[8].url} className="clip" id="C">C</audio>
        </button>
      </div>
    </div>
  );
};
ReactDOM.render(<App />, document.getElementById("root"));

              
            
!
999px

Console