It sounds like you want help building a rhythm game editor in the browser with features like:
- Loading an MP3 from a URL
- Setting BPM (beats per minute)
- Sharing the chart/state via URL hash (
#)
- Keyboard shortcuts
- Controlling bars count
- Showing combo and note grid
- Play/pause, delete notes, and general note placing
I can help you with a minimal example that combines these elements into a simple rhythm editor using plain HTML, CSS, and JavaScript. You can extend from here.
Minimal Rhythm Game Editor Example
Features:
- Load MP3 from URL input
- Set BPM
- Display bars & grid
- Place and delete notes on grid by clicking
- Keyboard shortcuts for play/pause (space), delete last note (backspace)
- Share editor state via URL hash (encoded JSON)
- Simple combo counting (count of placed notes)
- Play audio with simple timing highlighting current bar
Full Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Rhythm Game Editor</title>
<style>
body {
font-family: sans-serif;
margin: 0; padding: 0;
display: flex; flex-direction: column; align-items: center;
background: #111;
color: #eee;
}
header {
margin: 10px;
text-align: center;
}
#controls {
display: flex; gap: 10px; flex-wrap: wrap;
justify-content: center;
margin-bottom: 10px;
}
input[type=text], input[type=number] {
padding: 5px; font-size: 16px;
width: 200px;
background: #222;
border: 1px solid #444;
color: #eee;
}
button {
padding: 6px 12px;
font-size: 16px;
background: #555;
border: none;
color: white;
cursor: pointer;
border-radius: 3px;
}
button:hover {
background: #777;
}
#canvas-container {
position: relative;
margin: 20px 0;
}
#noteCanvas {
background: #222;
border: 1px solid #444;
border-radius: 4px;
}
#combo {
margin-top: 10px;
font-weight: bold;
}
#instructions {
max-width: 600px;
font-size: 14px;
color: #aaa;
margin: 10px;
}
</style>
</head>
<body>
<header>
<h1>Rhythm Game Editor</h1>
</header>
<div id="controls">
<input type="text" id="audioUrl" placeholder="Enter MP3 URL (https://...)" />
<input type="number" id="bpm" min="30" max="300" value="120" />
<input type="number" id="barsCount" min="1" max="64" value="8" />
<button id="loadBtn">Load Audio</button>
<button id="playPauseBtn">Play</button>
<button id="shareBtn">Share Link</button>
</div>
<div id="canvas-container">
<canvas id="noteCanvas" width="800" height="200"></canvas>
</div>
<div id="combo">Combo: 0</div>
<div id="instructions">
<p><strong>Instructions:</strong></p>
<ul>
<li>Click grid cells to add/remove notes.</li>
<li>Space: Play/Pause</li>
<li>Backspace/Delete: Delete last note</li>
<li>Adjust BPM and bars count</li>
<li>Load audio from URL</li>
<li>Click Share Link to get URL with your chart</li>
</ul>
</div>
<script>
(() => {
// Elements
const audioUrlInput = document.getElementById('audioUrl');
const bpmInput = document.getElementById('bpm');
const barsCountInput = document.getElementById('barsCount');
const loadBtn = document.getElementById('loadBtn');
const playPauseBtn = document.getElementById('playPauseBtn');
const shareBtn = document.getElementById('shareBtn');
const comboDisplay = document.getElementById('combo');
const canvas = document.getElementById('noteCanvas');
const ctx = canvas.getContext('2d');
// Constants
const CANVAS_WIDTH = canvas.width;
const CANVAS_HEIGHT = canvas.height;
const ROWS = 4; // number of note rows (could represent 4 lanes)
let barsCount = parseInt(barsCountInput.value);
let bpm = parseInt(bpmInput.value);
const beatsPerBar = 4; // quarter notes per bar
const totalBeats = barsCount * beatsPerBar;
const cols = totalBeats; // one column per beat (simplify 1 note per beat)
const cellWidth = CANVAS_WIDTH / cols;
const cellHeight = CANVAS_HEIGHT / ROWS;
// State
let notes = []; // {bar, beatInBar, row}
let audioCtx = null;
let audioBuffer = null;
let sourceNode = null;
let isPlaying = false;
let startTime = 0;
let pauseTime = 0;
// When playing, track current time for highlighting
let animationFrameId = null;
// Helper to encode/decode notes for URL hash
function encodeState() {
const state = {
audioUrl: audioUrlInput.value.trim(),
bpm,
barsCount,
notes
};
return encodeURIComponent(JSON.stringify(state));
}
function decodeState(hash) {
try {
const decoded = decodeURIComponent(hash);
return JSON.parse(decoded);
} catch {
return null;
}
}
// Drawing function
function drawGrid() {
ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
// Background
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
// Vertical bars lines
ctx.strokeStyle = '#444';
ctx.lineWidth = 1;
for (let i = 0; i <= cols; i++) {
const x = i * cellWidth;
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, CANVAS_HEIGHT);
ctx.stroke();
}
// Horizontal lines for rows
for (let r = 0; r <= ROWS; r++) {
const y = r * cellHeight;
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(CANVAS_WIDTH, y);
ctx.stroke();
}
// Draw thicker lines for bars (every 4 beats)
ctx.strokeStyle = '#888';
ctx.lineWidth = 2;
for (let b = 0; b <= barsCount; b++) {
const x = b * beatsPerBar * cellWidth;
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, CANVAS_HEIGHT);
ctx.stroke();
}
}
function drawNotes(highlightBeat = null) {
drawGrid();
// Draw notes
ctx.fillStyle = '#0f0';
for (const note of notes) {
const beatIndex = (note.bar - 1) * beatsPerBar + (note.beatInBar - 1);
const x = beatIndex * cellWidth;
const y = note.row * cellHeight;
ctx.fillRect(x + 2, y + 2, cellWidth - 4, cellHeight - 4);
}
// Highlight current beat
if (highlightBeat !== null) {
const x = highlightBeat * cellWidth;
ctx.fillStyle = 'rgba(255, 255, 0, 0.3)';
ctx.fillRect(x, 0, cellWidth, CANVAS_HEIGHT);
}
}
// Update combo display
function updateCombo() {
comboDisplay.textContent = `Combo: ${notes.length}`;
}
// Place or remove note at clicked grid cell
function handleCanvasClick(e) {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// Calculate which beat and row was clicked
const beatIndex = Math.floor(x / cellWidth);
const row = Math.floor(y / cellHeight);
if (row < 0 || row >= ROWS) return;
// Convert beatIndex into bar and beatInBar
let localBar = Math.floor(beatIndex / beatsPerBar) + 1;
if (localBar > barsCount) return;
let localBeat = (beatIndex % beatsPerBar) + 1;
// Check if note exists at this position
const existingIndex = notes.findIndex(note =>
note.bar === localBar && note.beatInBar === localBeat && note.row === row);
if (existingIndex >= 0) {
// Remove note
notes.splice(existingIndex, 1);
} else {
// Add note
notes.push({bar: localBar, beatInBar: localBeat, row});
}
updateCombo();
drawNotes();
}
// Audio loading and playback
async function loadAudio(url) {
if (!audioCtx) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}
try {
const resp = await fetch(url);
const arrayBuffer = await resp.arrayBuffer();
audioBuffer = await audioCtx.decodeAudioData(arrayBuffer);
console.log("Audio loaded", audioBuffer);
return true;
} catch(e) {
alert("Failed to load audio.");
console.error(e);
return false;
}
}
function playAudio() {
if (!audioBuffer) return;
if (isPlaying) return;
sourceNode = audioCtx.createBufferSource();
sourceNode.buffer = audioBuffer;
sourceNode.connect(audioCtx.destination);
const offset = pauseTime;
startTime = audioCtx.currentTime - offset;
sourceNode.start(0, offset);
isPlaying = true;
playPauseBtn.textContent = "Pause";
animationFrameId = requestAnimationFrame(updatePlayback);
}
function pauseAudio() {
if (!isPlaying) return;
// Stop source node
sourceNode.stop();
sourceNode.disconnect();
sourceNode = null;
pauseTime = audioCtx.currentTime - startTime;
isPlaying = false;
playPauseBtn.textContent = "Play";
cancelAnimationFrame(animationFrameId);
}
function togglePlay() {
if (isPlaying) {
pauseAudio();
} else {
if (audioCtx.state === 'suspended') {
audioCtx.resume();
}
playAudio();
}
}
// Handle playback visualization
function updatePlayback() {
if (!isPlaying) return;
const elapsed = audioCtx.currentTime - startTime;
const beatsElapsed = (elapsed / 60) * bpm;
const currentBeat = Math.floor(beatsElapsed) % totalBeats;
drawNotes(currentBeat);
// Loop audio if ended
if (elapsed >= audioBuffer.duration) {
pauseAudio();
pauseTime = 0;
drawNotes();
return;
}
animationFrameId = requestAnimationFrame(updatePlayback);
}
// Keyboard shortcuts
document.addEventListener('keydown', e => {
if (e.target.tagName === 'INPUT') return;
switch(e.code) {
case 'Space':
e.preventDefault();
togglePlay();
break;
case 'Backspace':
case 'Delete':
e.preventDefault();
if (notes.length > 0) {
notes.pop();
updateCombo();
drawNotes();
}
break;
}
});
// Share editor state in URL hash
function updateHash() {
const stateEncoded = encodeState();
window.location.hash = stateEncoded;
}
function loadFromHash() {
if (window.location.hash.length < 2) return;
const state = decodeState(window.location.hash.substring(1));
if (!state) return;
audioUrlInput.value = state.audioUrl || '';
barsCount = state.barsCount || 8;
barsCountInput.value = barsCount;
bpm = state.bpm || 120;
bpmInput.value = bpm;
notes = state.notes || [];
// Redraw and update combo
updateCombo();
drawNotes();
}
// Update barsCount and bpm globally and refresh grid
function updateBarsBPM() {
barsCount = Math.min(Math.max(1, parseInt(barsCountInput.value) || 8), 64);
bpm = Math.min(Math.max(30, parseInt(bpmInput.value) || 120), 300);
barsCountInput.value = barsCount;
bpmInput.value = bpm;
// Resize canvas grid data depends on barsCount
// We don't change canvas size but recalc grid
// Since canvas width/height fixed, cellWidth changes
// We'll recalc and redraw
drawNotes();
}
// Controls events
loadBtn.addEventListener('click', async () => {
const url = audioUrlInput.value.trim();
if (!url) {
alert("Enter an MP3 URL");
return;
}
loadBtn.disabled = true;
const success = await loadAudio(url);
loadBtn.disabled = false;
if (success) {
pauseTime = 0;
pauseAudio();
drawNotes();
}
});
playPauseBtn.addEventListener('click', () => {
togglePlay();
});
shareBtn.addEventListener('click', () => {
updateHash();
alert("URL updated with editor state. You can copy the URL from your browser's address bar.");
});
bpmInput.addEventListener('change', () => {
updateBarsBPM();
});
barsCountInput.addEventListener('change', () => {
updateBarsBPM();
});
canvas.addEventListener('click', handleCanvasClick);
// On load: check if we have state in URL hash
window.addEventListener('load', async () => {
loadFromHash();
updateBarsBPM();
// If audio URL present, auto load
const url = audioUrlInput.value.trim();
if (url) {
await loadAudio(url);
drawNotes();
} else {
drawNotes();
}
});
// Resize handler for crispness if needed (optional)
})();
</script>
</body>
</html>
How to Use
- Paste this into a
.html file and open in a modern browser.
- Enter an MP3 URL (example:
https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3).
- Click "Load Audio" — the audio loads (may take a moment).
- Set BPM and bars count as you like.
- Click the grid to place or remove notes (green squares).
- Click "Play" (or press space) to play audio; the current beat column is highlighted in yellow.
- You can press BACKSPACE or DELETE key to remove last placed note.
- Click "Share Link" to update the URL with your editor state.
- Copy the URL and share it — opening it will restore the current song, BPM, bars, and notes.
How This Works
- The grid visualizes bars and beats based on BPM and bars count.
- Notes are stored as
{bar, beatInBar, row} where row is 0-3 (lanes).
- Audio is loaded with Web Audio API for accurate timing.
- Playback highlights the current beat column.
- State is stored in URL hash as a JSON string encoded, so sharing state is easy.
Limitations / Next Steps
- Supports only 4 rows / fixed lanes.
- Notes snap to beat (no subdivisions).
- No velocity/volume or note length (only taps).
- No save/load files, only URL sharing.
- No UI for exact note removal besides clicking or deleting last note.
- No scrolling if bars count is large (canvas fixed width).
- No visual feedback on which note row is clicked beyond the cell highlight.
- Can add audio controls, metronome, zoom, subdivisions, and more.
If you'd like me to extend this with features (like scrolling, note velocities, subdivisions, or better UI), just ask!