Drum Kit & Guitar
by PlasmaMeteor43241 lines7.6 KB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Drum Kit & Guitar</title>
<style>
body {
background-color: #181818;
color: #ffffff;
font-family: Arial, sans-serif;
text-align: center;
padding: 25px;
margin: 0;
}
h1 {
color: #00ffcc;
margin-bottom: 5px;
}
h2 {
color: #ff9900;
margin-top: 35px;
margin-bottom: 5px;
}
p {
color: #aaaaaa;
margin-bottom: 20px;
}
.board {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 15px;
max-width: 680px;
margin: 0 auto;
}
.drum-btn {
width: 115px;
height: 115px;
border-radius: 50%;
border: 4px solid #00ffcc;
background-color: #262626;
color: #ffffff;
font-size: 16px;
font-weight: bold;
cursor: pointer;
}
.guitar-btn {
width: 95px;
height: 110px;
border-radius: 14px;
border: 4px solid #ff9900;
background-color: #2e1a0e;
color: #ffffff;
font-size: 16px;
font-weight: bold;
cursor: pointer;
}
.drum-btn span {
display: block;
font-size: 22px;
color: #00ffcc;
margin-bottom: 4px;
}
.guitar-btn span {
display: block;
font-size: 22px;
color: #ff9900;
margin-bottom: 4px;
}
.active {
background-color: #555555;
transform: scale(0.92);
}
.strum-btn {
margin-top: 20px;
padding: 12px 28px;
font-size: 16px;
font-weight: bold;
background-color: #ff9900;
color: #181818;
border: none;
border-radius: 8px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Virtual Drum Kit</h1>
<p>Click a button or press A, S, D, F, G, H, J, K</p>
<div class="board">
<button type="button" id="pad-a" class="drum-btn" onclick="trigger('a')"><span>A</span>Kick</button>
<button type="button" id="pad-s" class="drum-btn" onclick="trigger('s')"><span>S</span>Snare</button>
<button type="button" id="pad-d" class="drum-btn" onclick="trigger('d')"><span>D</span>Hi-Hat</button>
<button type="button" id="pad-f" class="drum-btn" onclick="trigger('f')"><span>F</span>Clap</button>
<button type="button" id="pad-g" class="drum-btn" onclick="trigger('g')"><span>G</span>Tom 1</button>
<button type="button" id="pad-h" class="drum-btn" onclick="trigger('h')"><span>H</span>Tom 2</button>
<button type="button" id="pad-j" class="drum-btn" onclick="trigger('j')"><span>J</span>Crash</button>
<button type="button" id="pad-k" class="drum-btn" onclick="trigger('k')"><span>K</span>Ride</button>
</div>
<h2>Virtual Guitar</h2>
<p>Click a button or press 1, 2, 3, 4, 5, 6 (Spacebar to strum)</p>
<div class="board">
<button type="button" id="pad-1" class="guitar-btn" onclick="trigger('1')"><span>1</span>Low E</button>
<button type="button" id="pad-2" class="guitar-btn" onclick="trigger('2')"><span>2</span>A</button>
<button type="button" id="pad-3" class="guitar-btn" onclick="trigger('3')"><span>3</span>D</button>
<button type="button" id="pad-4" class="guitar-btn" onclick="trigger('4')"><span>4</span>G</button>
<button type="button" id="pad-5" class="guitar-btn" onclick="trigger('5')"><span>5</span>B</button>
<button type="button" id="pad-6" class="guitar-btn" onclick="trigger('6')"><span>6</span>High E</button>
</div>
<button type="button" class="strum-btn" onclick="strumAll()">Strum All Strings</button>
<script>
var audioContext = null;
function getAudio() {
if (!audioContext) {
var AudioCtx = window.AudioContext || window.webkitAudioContext;
audioContext = new AudioCtx();
}
if (audioContext.state === 'suspended') {
audioContext.resume();
}
return audioContext;
}
function playNoise(ctx, duration, filterType, freq, volume) {
var bufferSize = Math.floor(ctx.sampleRate * duration);
var buffer = ctx.createBuffer(1, bufferSize, ctx.sampleRate);
var data = buffer.getChannelData(0);
for (var i = 0; i < bufferSize; i++) {
data[i] = Math.random() * 2 - 1;
}
var noise = ctx.createBufferSource();
noise.buffer = buffer;
var filter = ctx.createBiquadFilter();
filter.type = filterType;
filter.frequency.value = freq;
var gain = ctx.createGain();
gain.gain.setValueAtTime(volume, ctx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.01, ctx.currentTime + duration);
noise.connect(filter);
filter.connect(gain);
gain.connect(ctx.destination);
noise.start(ctx.currentTime);
}
function playTone(ctx, startFreq, endFreq, duration, waveType, volume) {
var osc = ctx.createOscillator();
var gain = ctx.createGain();
osc.type = waveType;
osc.frequency.setValueAtTime(startFreq, ctx.currentTime);
if (endFreq) {
osc.frequency.exponentialRampToValueAtTime(endFreq, ctx.currentTime + duration);
}
gain.gain.setValueAtTime(volume, ctx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + duration);
osc.connect(gain);
gain.connect(ctx.destination);
osc.start(ctx.currentTime);
osc.stop(ctx.currentTime + duration);
}
function trigger(key) {
var ctx = getAudio();
var el = document.getElementById('pad-' + key);
if (el) {
el.className += ' active';
setTimeout(function() {
el.className = el.className.replace(' active', '');
}, 120);
}
if (key === 'a') playTone(ctx, 150, 0.01, 0.45, 'sine', 1.0);
else if (key === 's') playNoise(ctx, 0.2, 'highpass', 1000, 0.7);
else if (key === 'd') playNoise(ctx, 0.08, 'highpass', 5000, 0.4);
else if (key === 'f') playNoise(ctx, 0.15, 'bandpass', 1500, 0.7);
else if (key === 'g') playTone(ctx, 130, 40, 0.3, 'sine', 0.8);
else if (key === 'h') playTone(ctx, 95, 40, 0.3, 'sine', 0.8);
else if (key === 'j') playNoise(ctx, 0.7, 'highpass', 4000, 0.5);
else if (key === 'k') playNoise(ctx, 0.7, 'bandpass', 3000, 0.5);
else if (key === '1') playTone(ctx, 82.41, null, 1.5, 'triangle', 0.6);
else if (key === '2') playTone(ctx, 110.00, null, 1.5, 'triangle', 0.6);
else if (key === '3') playTone(ctx, 146.83, null, 1.5, 'triangle', 0.6);
else if (key === '4') playTone(ctx, 196.00, null, 1.5, 'triangle', 0.6);
else if (key === '5') playTone(ctx, 246.94, null, 1.5, 'triangle', 0.6);
else if (key === '6') playTone(ctx, 329.63, null, 1.5, 'triangle', 0.6);
}
function strumAll() {
var notes = ['1', '2', '3', '4', '5', '6'];
for (var i = 0; i < notes.length; i++) {
(function(note, delay) {
setTimeout(function() {
trigger(note);
}, delay);
})(notes[i], i * 50);
}
}
document.onkeydown = function(event) {
var e = event || window.event;
var code = e.keyCode || e.which;
if (code === 32) {
if (e.preventDefault) e.preventDefault();
strumAll();
return;
}
var map = {
65: 'a', 83: 's', 68: 'd', 70: 'f',
71: 'g', 72: 'h', 74: 'j', 75: 'k',
49: '1', 50: '2', 51: '3', 52: '4', 53: '5', 54: '6'
};
if (map[code]) {
trigger(map[code]);
}
};
</script>
</body>
</html>Game Source: Drum Kit & Guitar
Creator: PlasmaMeteor43
Libraries: none
Complexity: complex (241 lines, 7.6 KB)
The full source code is displayed above on this page.
Remix Instructions
To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: drum-kit-guitar-plasmameteor43" to link back to the original. Then publish at arcadelab.ai/publish.