gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
html css js rhythm game editor load url mp3 bpm share "#/" shortcuts bars combo delete
- User
html css js rhythm game editor load url mp3 bpm share "#/" shortcuts bars combo delete
- Assistant
It sounds like you're interested in creating a simple rhythm game editor (like a beatmap editor) in HTML/CSS/JS that has the following features:
- Load an MP3 file from a URL
- Set BPM (beats per minute)
- Display editor UI with "bars" (measures)
- Support combo counts (notes combos)
- Allow deletion of notes
- Share the beatmap/state via URL hash (like
#/based state sharing) - Keyboard shortcuts to control the editor
Here's a minimal working example to get you started that covers these features:
Features in this demo:
- Audio loaded from URL input
- BPM input setter
- Draw measures and grid for beats based on BPM
- Click to add/remove notes on the grid
- Display combo number on notes
- Delete a note when clicked again
- Save/load editor state in URL hash
- Keyboard shortcuts:
bto toggle bars visibilitycto toggle combos displayDelete/Backspacedelete selected note
rhythm-editor.html
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; } header { background: #222; color: #eee; padding: 10px; display: flex; align-items: center; gap: 10px; } input, button { font-size: 1rem; padding: 5px; } #editor { position: relative; background: #111; color: white; height: 400px; user-select: none; overflow-x: auto; white-space: nowrap; } .bar { display: inline-block; border-left: 2px solid #555; width: 300px; height: 100%; position: relative; } .beat { border-bottom: 1px solid #444; height: 25%; position: relative; } .note { position: absolute; left: 20px; width: 20px; height: 20px; background: #0f0; border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 12px; user-select: none; } .note.selected { box-shadow: 0 0 10px 3px #0f0; } #status { padding: 5px; } </style> </head> <body> <header> <label> MP3 URL: <input id="audioUrl" type="text" placeholder="https://example.com/song.mp3" size="40" /> </label> <button id="loadAudio">Load Audio</button> <label> BPM: <input id="bpmInput" type="number" min="10" max="400" value="120" style="width:60px" /> </label> <button id="setBpm">Set BPM</button> <button id="shareBtn">Share URL</button> </header> <div id="status">Load a song and set BPM to start editing.</div> <div id="editor"></div> <audio id="audioPlayer" controls style="width: 100%;"></audio> <script> (() => { const editor = document.getElementById('editor'); const audioPlayer = document.getElementById('audioPlayer'); const audioUrlInput = document.getElementById('audioUrl'); const loadAudioBtn = document.getElementById('loadAudio'); const bpmInput = document.getElementById('bpmInput'); const setBpmBtn = document.getElementById('setBpm'); const shareBtn = document.getElementById('shareBtn'); const status = document.getElementById('status'); // State variables let bpm = 120; // defaults let barsCount = 8; let notes = []; // Array of objects: {bar:0-based, beat:0-based (0-15 for 16th notes), combo: number} let selectedNoteIndex = -1; // We’ll assume 16 beats/bar (4/4 with 16th notes) const beatsPerBar = 16; function renderEditor() { editor.innerHTML = ''; for(let bar=0; bar<barsCount; bar++) { const barDiv = document.createElement('div'); barDiv.className = 'bar'; barDiv.dataset.bar = bar; // 16 beats vertical grid (4 rows * 4 beats since each beat is a 16th note) // To keep it simple, we show 4 rows per bar (each is a quarter note) // Actually, let's show 16 horizontal beats (like grid horizontally), as better for rhythm // Instead, we do a horizontal grid: // bar width 300px / 16 = 18.75px per beat // Create 16 vertical beat markers (lines) for(let beat = 0; beat < beatsPerBar; beat++) { const beatMarker = document.createElement('div'); beatMarker.className = 'beat'; // position horizontally by beat beatMarker.style.position = 'absolute'; beatMarker.style.top = '0'; beatMarker.style.left = (beat * (300 / beatsPerBar)) + 'px'; beatMarker.style.width = (300 / beatsPerBar) + 'px'; beatMarker.style.height = '100%'; beatMarker.style.borderLeft = ((beat % 4 === 0) ? '2px solid #888' : '1px solid #444'); beatMarker.style.boxSizing = 'border-box'; barDiv.appendChild(beatMarker); } barDiv.style.position = 'relative'; editor.appendChild(barDiv); } // Render notes notes.forEach((note, i) => { let barDiv = editor.querySelector(`.bar[data-bar="${note.bar}"]`); if(!barDiv) return; // Safety const noteEl = document.createElement('div'); noteEl.className = 'note'; if(i === selectedNoteIndex) noteEl.classList.add('selected'); // Horizontal position: beat * width per beat const beatWidth = 300 / beatsPerBar; noteEl.style.left = (note.beat * beatWidth + beatWidth/2 - 10) + 'px'; // center note noteEl.style.top = '40%'; noteEl.textContent = note.combo || ''; noteEl.title = `Bar ${note.bar+1}, Beat ${note.beat+1}`; noteEl.dataset.index = i; // click selects/deselects note or deletes it on second click noteEl.onclick = e => { e.stopPropagation(); if(selectedNoteIndex === i) { // Delete note notes.splice(i, 1); selectedNoteIndex = -1; renderEditor(); updateStatus(`Note deleted at Bar ${note.bar+1} Beat ${note.beat+1}.`); } else { selectedNoteIndex = i; renderEditor(); updateStatus(`Note selected at Bar ${note.bar+1} Beat ${note.beat+1}.`); } }; barDiv.appendChild(noteEl); }); } function updateStatus(msg) { status.textContent = msg; } // Adds or removes note on click in empty space function onEditorClick(e) { if(e.target !== editor && e.target.classList.contains('note')) return; // ignore clicks on notes // Find bar clicked let barDiv = null; for(let el of editor.children) { let rect = el.getBoundingClientRect(); if(e.clientX >= rect.left && e.clientX <= rect.right) { barDiv = el; break; } } if(!barDiv) return; const bar = parseInt(barDiv.dataset.bar); const rect = barDiv.getBoundingClientRect(); // Get beat inside bar clicked (0 based) const relativeX = e.clientX - rect.left; const beatWidth = 300 / beatsPerBar; const beat = Math.floor(relativeX / beatWidth); // Check if note exists at bar and beat let idx = notes.findIndex(n => n.bar === bar && n.beat === beat); if(idx === -1) { // Add note - combo numbering = notes.length+1 or increment const combo = notes.length ? Math.max(...notes.map(n => n.combo)) + 1 : 1; notes.push({bar, beat, combo}); selectedNoteIndex = notes.length - 1; updateStatus(`Added note at Bar ${bar+1} Beat ${beat+1}`); } else { // Remove note notes.splice(idx, 1); if(selectedNoteIndex === idx) selectedNoteIndex = -1; updateStatus(`Removed note at Bar ${bar+1} Beat ${beat+1}`); } renderEditor(); } // Set BPM and adjust bars/measures if needed function setBpm(newBpm) { bpm = newBpm; // for demo - fixed bars count updateStatus(`BPM set to ${bpm}`); } // Load audio from URL, with error handling function loadAudioFromUrl(url) { if(!url.startsWith('http') && !url.startsWith('blob:')) { updateStatus('Invalid URL'); return; } audioPlayer.src = url; audioPlayer.load(); updateStatus('Audio loaded, ready to play.'); } // Save state as JSON in base64 in hash URL function saveStateToUrl() { const state = {bpm, notes, audioUrl: audioUrlInput.value}; const jsonStr = JSON.stringify(state); const encoded = btoa(unescape(encodeURIComponent(jsonStr))); const shareUrl = `${window.location.origin}${window.location.pathname}#/` + encoded; navigator.clipboard.writeText(shareUrl) .then(() => updateStatus('Share URL copied to clipboard!')) .catch(() => updateStatus('Failed to copy URL. Here is the URL: ' + shareUrl)); } // Load state from URL hash function loadStateFromUrlHash() { if(location.hash.startsWith('#/')) { try { const encoded = location.hash.substring(2); const decoded = decodeURIComponent(escape(atob(encoded))); const state = JSON.parse(decoded); bpm = state.bpm || 120; notes = state.notes || []; audioUrlInput.value = state.audioUrl || ''; setBpmInput.value = bpm; if(audioUrlInput.value) { loadAudioFromUrl(audioUrlInput.value); } updateStatus('Loaded state from URL'); renderEditor(); } catch(e) { updateStatus('Failed to load state from URL'); console.error(e); } } } // Keyboard shortcuts function onKeyDown(e) { if(e.target.tagName === 'INPUT') return; // ignore if typing in inputs switch(e.key.toLowerCase()) { case 'b': // Toggle bars visibility const bars = document.querySelectorAll('.bar'); bars.forEach(bar => { bar.style.borderLeftColor = bar.style.borderLeftColor === 'transparent' ? '#555' : 'transparent'; }); updateStatus('Toggled bars visibility'); break; case 'c': // Toggle combo numbers visibility const notesEls = document.querySelectorAll('.note'); notesEls.forEach(noteEl => { if(noteEl.style.color === 'transparent') { noteEl.style.color = 'inherit'; } else { noteEl.style.color = 'transparent'; } }); updateStatus('Toggled combo numbers visibility'); break; case 'delete': case 'backspace': if(selectedNoteIndex !== -1) { const removed = notes.splice(selectedNoteIndex, 1); selectedNoteIndex = -1; renderEditor(); updateStatus('Deleted selected note'); e.preventDefault(); } break; case 'arrowleft': if(selectedNoteIndex > 0) { selectedNoteIndex--; renderEditor(); updateStatus('Selected previous note'); } break; case 'arrowright': if(selectedNoteIndex < notes.length -1) { selectedNoteIndex++; renderEditor(); updateStatus('Selected next note'); } break; } } loadAudioBtn.onclick = () => { const url = audioUrlInput.value.trim(); if(url) { loadAudioFromUrl(url); } }; setBpmBtn.onclick = () => { const val = parseInt(bpmInput.value); if(val && val >= 10 && val <= 400) { setBpm(val); } }; shareBtn.onclick = () => { saveStateToUrl(); }; editor.onclick = onEditorClick; window.addEventListener('keydown', onKeyDown); loadStateFromUrlHash(); renderEditor(); })(); </script> </body> </html>
How to Use This Editor
- Enter an MP3 URL in the first input and click Load Audio.
- Set the BPM of the song and click Set BPM.
- The editor area will display bars (measures). Click anywhere inside a bar to add a note at the closest beat.
- Click an existing note to select it. Clicking the selected note again deletes it.
- Keyboard shortcuts:
Btoggles the visibility of bar lines.Ctoggles the visibility of combo note numbers.DeleteorBackspacedeletes the currently selected note.Left/Right Arrowkeys move note selection.
- Click Share URL to encode the current editor state (audio URL, BPM, notes) into the URL hash and copy it to your clipboard. You can share this URL with others to load the same beatmap state.
Notes:
- This example saves a base64-encoded JSON in the URL after
#/to share the map. - It only supports 8 bars for now, you can extend it by adding UI controls.
- The editor shows a basic visual bar grid with notes positioned at 16 beats per bar.
- Combos are just sequential numbering of notes in order added.
- Notes are green circles on the beat grid.
- The audio player uses the loaded MP3 and supports play/pause.
If you'd like, I can help you extend this with drag-and-drop MP3 loading, multi-beat notes, export/import JSON files, note timing snapping, or real-time playback sync! Just ask.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.