html css js rhythm game editor load url mp3 bpm share "#/" shortcuts bars count combo delete play note textarea
Assistant
Sure! Below is a minimal example of a Rhythm Game Editor built with HTML, CSS, and JavaScript that allows you to:
Load an MP3 from a URL
Set BPM
Manage bars count
Add notes via shortcuts
Play/pause music with synced note playback visualization
Display combo count
Delete notes
Share the editor state via URL hash
Edit notes data via textarea JSON
Features
Load MP3: Enter an MP3 URL and it loads the audio.
Set BPM & Bars: Controls let you set BPM and number of bars.
Notes: Each note is defined by its beat position and lane (simple 4 lanes).
Shortcuts: Use keys 1-4 to add a note at the current playback time.
Delete notes: Click notes to delete.
Play: Button toggles audio and starts playback.
Combo: Display current combo (notes hit in sequence).
Share: Editor state is saved in the URL hash as JSON (compressed with base64).
Textarea: Directly edit notes JSON, bars, bpm to customize and sync.
How to use
Paste an MP3 URL, set BPM, bars count.
Click Load.
Use number keys (1-4) to add notes during playback or paused.
Click notes to delete.
Press Play to start audio and follow the notes.
Share your state by copying the URL (hash contains all data).
Paste shared URLs to load saved states.
Edit the notes JSON directly if you want.
Code
html
<!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: Arial, sans-serif;
margin: 0; padding: 0; background: #111; color: #eee;
display: flex; flex-direction: column; align-items: center;
min-height: 100vh;
}
header {
background: #222;
width: 100%;
padding: 1em;
box-sizing: border-box;
text-align: center;
}
main {
flex: 1;
width: 95vw;
max-width: 900px;
margin-top: 1em;
display: flex;
flex-direction: column;
gap: 1em;
}
label {
display: inline-block;
margin-right: 0.5em;
}
input, button, select, textarea {
font-size: 1em;
padding: 0.3em 0.5em;
margin-right: 1em;
background: #222;
border: 1px solid #555;
color: #eee;
border-radius: 4px;
}
button:hover {
border-color: #0fc;
cursor: pointer;
}
#player-container {
position: relative;
height: 150px;
background: #222;
border: 2px solid #0fa;
border-radius: 6px;
overflow: hidden;
}
#notes-canvas {
width: 100%;
height: 150px;
background: #111;
display: block;
}
#combo-display {
font-size: 1.2em;
font-weight: bold;
margin-top: 0.5em;
color: #0f0;
text-align: center;
}
textarea {
min-height: 120px;
font-family: monospace;
background: #222;
color: #0f0;
border: 1px solid #0a0;
resize: vertical;
}
.controls-row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 0.5em;
}
a {
color: #0af;
word-break: break-all;
}
</style>
</head>
<body>
<header><h1>Rhythm Game Editor</h1></header>
<main>
<div class="controls-row">
<label for="mp3-url">MP3 URL:</label>
<input id="mp3-url" type="url" placeholder="Enter MP3 URL" size="40" spellcheck="false" />
<button id="load-btn">Load</button>
</div>
<div class="controls-row">
<label for="bpm-input">BPM:</label>
<input type="number" id="bpm-input" min="30" max="300" value="120" />
<label for="bars-count">Bars:</label>
<input type="number" id="bars-count" min="1" max="50" value="8" />
<button id="play-pause-btn">Play</button>
</div>
<div id="player-container">
<canvas id="notes-canvas" width="800" height="150"></canvas>
</div>
<div id="combo-display">Combo: 0</div>
<label for="notes-json">Notes JSON (edit to modify notes):</label><br />
<textarea id="notes-json" spellcheck="false"></textarea>
</main>
<script>
(() => {
const lanesCount = 4;
const laneColors = ['#f00', '#0f0', '#00f', '#ff0'];
const canvas = document.getElementById('notes-canvas');
const ctx = canvas.getContext('2d');
const mp3Input = document.getElementById('mp3-url');
const loadBtn = document.getElementById('load-btn');
const bpmInput = document.getElementById('bpm-input');
const barsInput = document.getElementById('bars-count');
const playPauseBtn = document.getElementById('play-pause-btn');
const comboDisplay = document.getElementById('combo-display');
const notesJsonTextarea = document.getElementById('notes-json');
let audio = null;
let isPlaying = false;
let bpm = 120;
let barsCount = 8;
let notes = [];
let combo = 0;
// Constants for drawing
const width = canvas.width;
const height = canvas.height;
const laneWidth = width / lanesCount;
const beatsPerBar = 4;
// Load from URL hash on start
function loadFromHash(){
try {
const hash = window.location.hash.slice(2);
if (!hash) return;
// decode base64 JSON
const jsonStr = atob(hash);
const data = JSON.parse(jsonStr);
if (data.bpm) bpm = data.bpm;
if (data.bars) barsCount = data.bars;
if (Array.isArray(data.notes)) notes = data.notes;
if (data.mp3) mp3Input.value = data.mp3;
bpmInput.value = bpm;
barsInput.value = barsCount;
updateNotesJson();
} catch(e) {
console.warn('Failed to load state from URL:', e);
}
}
function saveToHash(){
const data = {
bpm,
bars: barsCount,
notes,
mp3: mp3Input.value.trim()
};
const jsonStr = JSON.stringify(data);
const b64 = btoa(jsonStr);
window.location.hash = '#/'+b64;
}
// Play and pause
function togglePlay(){
if (!audio) return;
if (isPlaying) {
audio.pause();
isPlaying = false;
playPauseBtn.textContent = 'Play';
} else {
audio.play();
isPlaying = true;
playPauseBtn.textContent = 'Pause';
}
}
function loadAudio(url){
if (audio) {
audio.pause();
audio = null;
}
audio = new Audio();
audio.src = url;
audio.crossOrigin = "anonymous";
audio.onended = () => {
isPlaying = false;
playPauseBtn.textContent = 'Play';
combo = 0;
updateComboDisplay();
};
audio.onerror = () => alert("Failed to load audio.");
}
function updateNotesJson(){
notesJsonTextarea.value = JSON.stringify(notes,null,2);
}
function parseNotesJson(){
try {
const v = JSON.parse(notesJsonTextarea.value);
if (!Array.isArray(v)) throw new Error("Notes JSON must be an array");
// validate notes
for (const n of v) {
if (typeof n.beat !== 'number' || typeof n.lane !== 'number') throw new Error('Invalid note format');
if (n.lane < 0 || n.lane >= lanesCount) throw new Error('Note lane out of range');
if (n.beat < 0) throw new Error('Note beat must be non-negative');
}
notes = v;
saveToHash();
return true;
} catch(e){
alert('Invalid notes JSON: ' + e.message);
return false;
}
}
// Drawing
/*
Notes are drawn as rectangles moving from right to left.
Current time is a vertical line on left 20% of width.
Notes appear on the right side and move left based on timing.
*/
function draw(currentTime){
ctx.clearRect(0,0,width,height);
// Draw lanes background
for(let i=0; i < lanesCount; i++){
ctx.fillStyle = (i % 2) === 0 ? "#222" : "#111";
ctx.fillRect(i*laneWidth, 0, laneWidth, height);
}
// Draw vertical beat lines (bars)
const secondsPerBeat = 60 / bpm;
const totalBeats = barsCount * beatsPerBar;
ctx.strokeStyle = '#0a0';
ctx.lineWidth = 1;
for(let b=0; b <= totalBeats; b++){
const x = width * (1 - (b * secondsPerBeat - currentTime) / (barsCount * beatsPerBar * secondsPerBeat));
if (x < 0 || x > width) continue;
if (b % beatsPerBar === 0) {
ctx.lineWidth = 2;
ctx.strokeStyle = '#0f0';
} else {
ctx.lineWidth = 1;
ctx.strokeStyle = '#080';
}
ctx.beginPath();
ctx.moveTo(x,0);
ctx.lineTo(x,height);
ctx.stroke();
}
// Draw current time indicator line
const indicatorX = width * 0.2;
ctx.strokeStyle = '#ff0';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(indicatorX,0);
ctx.lineTo(indicatorX,height);
ctx.stroke();
// Draw notes:
// Notes' x position is calculated by beat relative to currentTime.
// If beat time has passed the indicator, note is on left side
// Only render notes that are within visible time window
const visibleWindowSeconds = barsCount * beatsPerBar * secondsPerBeat;
const noteWidth = laneWidth*0.7;
const noteHeight = 20;
const approachRateSeconds = visibleWindowSeconds * 0.8; // note travel time from right to indicator
for (const note of notes) {
const noteTime = note.beat * secondsPerBeat;
const delta = noteTime - currentTime;
if (delta < -1 || delta > approachRateSeconds + 1) continue; // cull
// Map delta to x position:
// when delta = approachRateSeconds -> x = width (right),
// when delta = 0 -> x = indicatorX
let x = indicatorX + (delta / approachRateSeconds) * (width - indicatorX);
const y = note.lane * (height / lanesCount) + (height/lanesCount - noteHeight)/2;
// Draw note rectangle
ctx.fillStyle = laneColors[note.lane];
ctx.strokeStyle = '#fff';
ctx.lineWidth = 1;
ctx.fillRect(x - noteWidth / 2, y, noteWidth, noteHeight);
ctx.strokeRect(x - noteWidth / 2, y, noteWidth, noteHeight);
}
}
// Interaction - add notes with keyboard shortcuts 1-4 at current beat
function addNoteAtCurrentTime(lane){
if (!audio) return;
if (!isPlaying) {
alert("Play the audio to add notes in sync or seek to a position.");
return;
}
const currentTime = audio.currentTime;
const secondsPerBeat = 60 / bpm;
const beat = currentTime / secondsPerBeat;
const roundedBeat = Math.round(beat * 4) / 4; // snap to 16th (0.25) beats
notes.push({beat: roundedBeat, lane});
sortNotes();
updateNotesJson();
saveToHash();
}
// Sort notes by beat
function sortNotes(){
notes.sort((a,b) => a.beat - b.beat || a.lane - b.lane);
}
// Delete note on click
canvas.addEventListener('click', e => {
const rect = canvas.getBoundingClientRect();
const clickX = e.clientX - rect.left;
const clickY = e.clientY - rect.top;
const secondsPerBeat = 60 / bpm;
const currentTime = audio ? (audio.currentTime) : 0;
const visibleWindowSeconds = barsCount * 4 * secondsPerBeat;
const approachRateSeconds = visibleWindowSeconds * 0.8;
const indicatorX = width * 0.2;
const noteWidth = laneWidth*0.7;
const noteHeight = 20;
for(let i=0; i<notes.length; i++){
const note = notes[i];
const noteTime = note.beat * secondsPerBeat;
const delta = noteTime - currentTime;
if (delta < -1 || delta > approachRateSeconds + 1) continue;
let x = indicatorX + (delta / approachRateSeconds) * (width - indicatorX);
const y = note.lane * (height / lanesCount) + (height/lanesCount - noteHeight)/2;
if (clickX >= x - noteWidth/2 && clickX <= x + noteWidth/2 &&
clickY >= y && clickY <= y + noteHeight) {
// Remove note
notes.splice(i,1);
updateNotesJson();
saveToHash();
break;
}
}
});
// Update combo display - we consider combo as notes "passed" within small hitWindow (e.g. 0.15s)
function updateCombo() {
if (!audio) {
combo = 0;
updateComboDisplay();
return;
}
const currentTime = audio.currentTime;
const secondsPerBeat = 60 / bpm;
const hitWindow = 0.125;
// count notes that time < currentTime + hitWindow and after last combo time
let count = 0;
for (const note of notes) {
const noteTime = note.beat * secondsPerBeat;
if (noteTime <= currentTime + hitWindow) count++;
}
combo = count;
updateComboDisplay();
}
function updateComboDisplay(){
comboDisplay.textContent = "Combo: " + combo;
}
// Handle play/pause btn
playPauseBtn.addEventListener('click', () => {
togglePlay();
});
// Load btn logic
loadBtn.addEventListener('click', () => {
const url = mp3Input.value.trim();
if (!url) {
alert('Enter valid MP3 URL');
return;
}
bpm = +bpmInput.value || 120;
barsCount = +barsInput.value || 8;
sortNotes();
loadAudio(url);
combo = 0;
updateComboDisplay();
updateNotesJson();
saveToHash();
});
// Update on bpm or bars change
bpmInput.addEventListener('change', () => {
bpm = +bpmInput.value || 120;
saveToHash();
});
barsInput.addEventListener('change', () => {
barsCount = +barsInput.value || 8;
saveToHash();
});
// Notes JSON textarea edit
notesJsonTextarea.addEventListener('change', () => {
if (parseNotesJson()) {
combo = 0;
updateComboDisplay();
}
});
// Keyboard shortcuts (1-4)
window.addEventListener('keydown', e => {
if (e.target === notesJsonTextarea || e.target === mp3Input) return; // ignore when typing
if (!isPlaying) return;
if (e.key >= '1' && e.key <= String(lanesCount)) {
const lane = parseInt(e.key) - 1;
addNoteAtCurrentTime(lane);
}
});
// Animation Loop
function animationLoop(){
const currentTime = audio && isPlaying ? audio.currentTime : 0;
draw(currentTime);
if (isPlaying) updateCombo();
requestAnimationFrame(animationLoop);
}
// Initial setup
loadFromHash();
updateNotesJson();
animationLoop();
})();
</script>
</body>
</html>
Explanation:
The editor loads your MP3 URL into an Audio element.
User controls BPM and bars count.
Notes are stored as {beat: number, lane: 0-3}.
Notes animate from right to left on a canvas synced with audio time.
Press keys 1,2,3,4 while playing to add notes on those lanes at current audio time, snapped to 16th notes.
The combo counter counts how many notes have passed "hit window".
Clicking on a note deletes it.
Notes can be directly edited as JSON.
The entire editor state is saved as base64 JSON in the URL hash (#/<base64>), so sharing the URL shares map and audio URL.
This should give you a solid foundation for a rhythm game chart editor that works entirely in browser! You can expand it further to support other features such as hold notes, more lanes, better audio synchronization, or visual themes. Let me know if you want me to add any.