Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet">

<div id="root"></div>
              
            
!

CSS

              
                * {
    box-sizing: border-box;
}

body {
    margin: 0 auto;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

h1 {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    text-align: center;
}

#drum-machine {
    padding: 1px;
    margin: 10vh auto;
    height: 500px;
    width: 600px;
    border-radius: 3px;
    background: linear-gradient(45deg, #444, #555);
    box-shadow: 1px 1px #333, 2px 2px #333, 3px 3px #333, 4px 4px #333, 5px 5px #333, 6px 6px #333, 7px 7px #333, 8px 8px #333, 8px 9px #333, 8px 10px #333;
}

#buttons-box {
    width: 400px;
    margin: 0 auto;
}

#display {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 4% auto;
    height: 8%;
    width: 40%;
    border-radius: 10px;
    background: yellowgreen;
    font-family: 'Press Start 2P', cursive;
    color: rgba(0, 0, 0, 0.8);
    border-top: 2px solid #333;
    border-left: 2px solid #333;
}

.drum-pad {
    background: lightgrey;
    font-size: 2rem;
    margin: 1rem;
    height: 100px;
    width: 100px;
    border: none;
    outline: none;
    border-radius: 5px;
    box-shadow: 3px 3px grey;
    cursor: pointer;
}

.drum-pad.one-shot:active {
    background: radial-gradient(circle, rgba(189, 148, 82, 1) 0%, rgba(255, 224, 175, 1) 100%);
}

@media screen and (max-width: 550px) {
    h1 {
        position: static;
        color: whitesmoke;
        font-size: 1.5rem;
        text-align: center;
        width: 100%;
    }
    #drum-machine {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-between;
        padding: 0;
        margin: 0;
        height: 100vh;
        width: 100vw;
    }
    .drum-pad {
        height: 30vw;
        margin: 1vw;
        width: 30vw;
        font-size: 2rem;
        box-shadow: 1px 1px grey;
    }
    #buttons-box {
        width: 100%;
        text-align: center;
    }
    #display {
        margin: 0 auto;
        padding: 0;
        width: 90%;
        height: 10vh;
    }
    .drum-pad.one-shot:active {
        background: orange;
    }
}
              
            
!

JS

              
                const samplesList = [
	{
		key: '🎸',
		keyCode: 81,
		audioUrl: 'https://www.mboxdrive.com/guitarloop.mp3',
		sound: 'Guitar Loop',
		type: 'loop'
	},
	{
		key: '🎼',
		keyCode: 87,
		audioUrl: 'https://www.mboxdrive.com/bassloop.mp3',
		sound: 'Bass Loop',
		type: 'loop'
	},
	{
		key: '🗣️',
		keyCode: 69,
		audioUrl: 'https://www.mboxdrive.com/crowdloop.mp3',
		sound: 'Crowd Loop',
		type: 'loop'
	},
	{
		key: '🧊',
		keyCode: 65,
		audioUrl: 'https://www.mboxdrive.com/closedhh.mp3',
		sound: 'Closed-HH',
		type: 'one-shot'
	},
	{
		key: '🥞',
		keyCode: 83,
		audioUrl: 'https://www.mboxdrive.com/openhh.mp3',
		sound: 'Open-HH',
		type: 'one-shot'
	},
	{
		key: '🌬️',
		keyCode: 68,
		audioUrl: 'https://www.mboxdrive.com/ride.mp3',
		sound: 'Ride',
		type: 'one-shot'
	},
	{
		key: '🥁',
		keyCode: 90,
		audioUrl: 'https://www.mboxdrive.com/snare.mp3',
		sound: 'Snare',
		type: 'one-shot'
	},
	{
		key: '💥',
		keyCode: 88,
		audioUrl: 'https://www.mboxdrive.com/kick.mp3',
		sound: 'Kick',
		type: 'one-shot'
	},
	{
		key: '👏',
		keyCode: 67,
		audioUrl: 'https://www.mboxdrive.com/clap.mp3',
		sound: 'Clap',
		type: 'one-shot'
	}
];

class DrumPad extends React.Component {
	constructor(props) {
		super(props);
		this.state = {};
	}
	render() {
		document.addEventListener('keydown', this.handleKeyDown);
		document.addEventListener('keyup', this.handleKeyUp);
		return (
			<button className={this.handleClassName()} id={this.props.sample.key} onClick={this.handleClick}>
				{this.props.sample.key}
				<audio
					loop={this.props.sample.type === 'loop' ? true : false}
					id={this.props.sample.sound}
					src={this.props.sample.audioUrl}
					preload="auto"
				/>
			</button>
		);
	}
	//Gives one class if it's a loop, another if it's a One Shot sample.
	handleClassName = () => {
		if (this.props.sample.type === 'loop') {
			return `drum-pad ${this.props.sample.type}`;
		} else {
			return `drum-pad ${this.props.sample.type}`;
		}
	};
	handleClick = (e) => {
		const sampleAudio = document.getElementById(this.props.sample.sound);
		const button = document.getElementById(this.props.sample.key);
		const displayScreen = document.getElementById('display');
		displayScreen.innerHTML = this.props.sample.sound;
		button.style.boxShadow = '2px 2px grey';
		if (e.target.children[0].paused) {
			if (e.target.children[0].loop) {
				button.style.background = 'radial-gradient(circle, rgba(102,189,82,1) 0%, rgba(209,255,175,1) 100%)';
				sampleAudio.play();
			} else if (!e.target.children[0].loop) {
				sampleAudio.play();
			}
		} else {
			if (e.target.children[0].loop) {
				button.style.background = 'lightgrey';
				sampleAudio.pause();
			}
		}
	};
	handleKeyDown = (e) => {
		const sampleAudio = document.getElementById(this.props.sample.sound);
		const button = document.getElementById(this.props.sample.key);
		const displayScreen = document.getElementById('display');
		if (
			e.keyCode === this.props.sample.keyCode &&
			button.className === 'drum-pad loop' &&
			sampleAudio.paused === false
		) {
			sampleAudio.pause();
			button.style.background = 'lightgrey';
		} else if (e.keyCode === this.props.sample.keyCode) {
			sampleAudio.play();
			displayScreen.innerHTML = this.props.sample.sound;
			button.style.boxShadow = '2px 2px grey';
			if (button.className === 'drum-pad loop') {
				button.style.background = 'radial-gradient(circle, rgba(102,189,82,1) 0%, rgba(209,255,175,1) 100%)';
			} else {
				button.style.background =
					'radial-gradient(circle, rgba(189, 148, 82, 1) 0%, rgba(255, 224, 175, 1) 100%)';
			}
		}
	};
	handleKeyUp = (e) => {
		const button = document.getElementById(this.props.sample.key);
		button.style.boxShadow = '3px 3px grey';
		if (e.keyCode === this.props.sample.keyCode && this.props.sample.type !== 'loop') {
			button.style.background = 'lightgrey';
		}
	};
}

class Maschine extends React.Component {
	constructor(props) {
		super(props);
		this.state = {};
	}
	render() {
		return (
			<div id="drum-machine">
				<div id="display" />
				<div id="buttons-box">{this.props.samples.map((i) => <DrumPad sample={i} key={i.key} />)}</div>
        <h1>Dem Drum Machine 🔥 </h1>
			</div>
		);
	}
}

function App() {
	return (
		<div>
			<Maschine samples={samplesList} />
		</div>
	);
}

ReactDOM.render(<App />, document.getElementById('root'));


              
            
!
999px

Console