JSFiddle - React, Tailwind, and code Playground

by gustav75

HTML

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Web Drums</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            background-color: #333;
            color: white;
            font-family: Arial, sans-serif;
        }
        .drum-pad {
            display: grid;
            grid-template-columns: repeat(3, 100px);
            grid-gap: 20px;
            margin-top: 50px;
        }
        .pad {
            width: 100px;
            height: 100px;
            background-color: #555;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            cursor: pointer;
            border-radius: 10px;
            user-select: none;
        }
        .pad:active {
            background-color: #777;
        }
    </style>
</head>
<body>
    <h1>Web Drums</h1>
    <div class="drum-pad">
        <div class="pad" data-sound="kick">Kick</div>
        <div class="pad" data-sound="snare">Snare</div>
        <div class="pad" data-sound="hihat">Hi-Hat</div>
        <div class="pad" data-sound="tom1">Tom 1</div>
        <div class="pad" data-sound="tom2">Tom 2</div>
        <div class="pad" data-sound="tom3">Tom 3</div>
    </div>
    <script src="script.js"></script>
</body>
</html>

CSS

#E2F6D5

JavaScript

const sounds = {
    kick: new Audio('sounds/kick.wav'),
    snare: new Audio('sounds/snare.wav'),
    hihat: new Audio('sounds/hihat.wav'),
    tom1: new Audio('sounds/tom1.wav'),
    tom2: new Audio('sounds/tom2.wav'),
    tom3: new Audio('sounds/tom3.wav')
};

document.querySelectorAll('.pad').forEach(pad => {
    pad.addEventListener('click', () => {
        const sound = pad.getAttribute('data-sound');
        sounds[sound].currentTime = 0; // Reset audio to start
        sounds[sound].play();
    });
});