html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Rhythm Game Editor</title>
<style>
body { font-family: Arial, sans-serif; margin:20px; background:#222; color:#eee; }
input, button { margin: 5px 0; padding:6px; }
#editor { position: relative; width: 720px; height: 200px; background:#333; margin-top: 10px; border: 1px solid #555; }
#grid { position: absolute; width: 720px; height: 200px; pointer-events:none; }
#notes { position: absolute; width: 720px; height: 200px; }
.note {
position: absolute;
width: 20px; height: 20px;
background: #f39c12;
border-radius: 50%;
cursor: pointer;
}
#playhead {
position: absolute;
top: 0; bottom: 0;
width: 2px;
background: red;
pointer-events: none;
}
#info { margin-top: 10px; }
</style>
</head>
<body>
<h1>Rhythm Game Editor</h1>
<label>MP3 URL: <input type="text" id="mp3Input" size="60" placeholder="Enter MP3 URL here" /></label><br>
<label>BPM: <input type="number" id="bpmInput" value="120" min="30" max="300" /></label><br>
<button id="loadBtn">Load</button>
<button id="playPauseBtn" disabled>Play</button>
<button id="shareBtn">Share URL</button>
<div id="editor">
<canvas id="grid"></canvas>
<div id="notes"></div>
<div id="playhead"></div>
</div>
<div id="info">
<p>Notes: <span id="noteCount">0</span></p>
<p>Combo: <span id="comboCount">0</span></p>
<p>Shortcuts: Space=Play/Pause, A=Add Note, D=Delete Last, ←/→=Seek</p>
</div>
<script>
(() => {
const bpmInput = document.getElementById('bpmInput');
const mp3Input = document.getElementById('mp3Input');
const loadBtn = document.getElementById('loadBtn');
const playPauseBtn = document.getElementById('playPauseBtn');
const shareBtn = document.getElementById('shareBtn');
const notesContainer = document.getElementById('notes');
const noteCountSpan = document.getElementById('noteCount');
const comboCountSpan = document.getElementById('comboCount');
const editor = document.getElementById('editor');
const gridCanvas = document.getElementById('grid');
const ctx = gridCanvas.getContext('2d');
const playhead = document.getElementById('playhead');
gridCanvas.width = editor.clientWidth;
gridCanvas.height = editor.clientHeight;
let audio = new Audio();
let bpm = 120;
let isPlaying = false;
let notes = []; // array of note objects: {beatIndex: int}
let playheadPosition = 0; // seconds
let duration = 0;
const beatsPerBar = 4;
const noteWidth = 20;
let combo = 0;
let maxCombo = 0;
// Draw grid (bars and beats)
function drawGrid() {
ctx.clearRect(0,0, gridCanvas.width, gridCanvas.height);
const width = gridCanvas.width;
const height = gridCanvas.height;
const totalBeats = Math.floor(duration / (60/bpm));
// Draw bar and beat lines
for(let i=0; i<=totalBeats; i++) {
let x = i * (noteWidth);
if (i % beatsPerBar === 0) {
ctx.strokeStyle = '#777';
ctx.lineWidth = 2;
} else {
ctx.strokeStyle = '#444';
ctx.lineWidth = 1;
}
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, height);
ctx.stroke();
}
}
// Render notes on grid
function renderNotes() {
notesContainer.innerHTML = '';
notes.forEach((note, i) => {
const noteEl = document.createElement('div');
noteEl.className = 'note';
noteEl.style.left = (note.beatIndex * noteWidth) + 'px';
noteEl.style.top = (editor.clientHeight / 2 - 10) + 'px';
noteEl.title = `Note #${i+1} at beat ${note.beatIndex}`;
noteEl.dataset.index = i;
notesContainer.appendChild(noteEl);
noteEl.onclick = () => {
// delete note on click
notes.splice(i,1);
updateNoteCount();
renderNotes();
updateURLHash();
};
});
}
// Update note count display
function updateNoteCount() {
noteCountSpan.textContent = notes.length;
}
// Update combo display (for demo just max combo = number of notes played successfully)
function updateCombo(currentTime) {
// simple check: increment combo if playhead passed notes sequentially without gaps > half beat
let newCombo = 0;
notes.sort((a,b)=>a.beatIndex - b.beatIndex);
for(let i=0; i<notes.length; i++) {
let noteSec = notes[i].beatIndex * (60/bpm);
if(currentTime >= noteSec) {
newCombo++;
} else {
break;
}
}
combo = newCombo;
if(combo > maxCombo) maxCombo = combo;
comboCountSpan.textContent = combo;
}
// Playhead update loop
function onTimeUpdate() {
let currentTime = audio.currentTime;
playheadPosition = currentTime;
let x = (currentTime / (60/bpm)) * noteWidth;
playhead.style.left = x + 'px';
updateCombo(currentTime);
}
// Add a note at current beat index rounded from current playhead
function addNoteAtCurrent() {
const currentBeat = Math.round(audio.currentTime / (60/bpm));
if(!notes.find(n => n.beatIndex === currentBeat)) {
notes.push({beatIndex: currentBeat});
notes.sort((a,b)=>a.beatIndex-b.beatIndex);
updateNoteCount();
renderNotes();
updateURLHash();
}
}
// Delete last note
function deleteLastNote() {
if(notes.length > 0) {
notes.pop();
updateNoteCount();
renderNotes();
updateURLHash();
}
}
// Load audio and setup
function loadAudio() {
const url = mp3Input.value.trim();
if(!url) return alert('Enter MP3 URL!');
audio.src = url;
audio.onloadedmetadata = () => {
duration = audio.duration;
drawGrid();
updateNoteCount();
renderNotes();
playPauseBtn.disabled = false;
playPauseBtn.textContent = 'Play';
playhead.style.left = '0px';
};
audio.onended = () => {
isPlaying = false;
playPauseBtn.textContent = 'Play';
};
}
// Play/pause toggle
function togglePlayPause() {
if(isPlaying) {
audio.pause();
isPlaying = false;
playPauseBtn.textContent = 'Play';
} else {
audio.play();
isPlaying = true;
playPauseBtn.textContent = 'Pause';
}
}
// Seek by beats: + or - 1 beat
function seekByBeats(offset) {
let newTime = audio.currentTime + offset*(60/bpm);
if(newTime < 0) newTime = 0;
if(newTime > duration) newTime = duration;
audio.currentTime = newTime;
playhead.style.left = (newTime/(60/bpm))*noteWidth + 'px';
}
// Encode state as base64 JSON into url hash
function updateURLHash() {
const state = {
bpm,
url: mp3Input.value.trim(),
notes,
};
const str = JSON.stringify(state);
const b64 = btoa(unescape(encodeURIComponent(str)));
location.hash = '#/' + b64;
}
// Decode state from URL hash
function loadFromURLHash() {
if(location.hash.startsWith('#/')) {
try {
const b64 = location.hash.slice(2);
const str = decodeURIComponent(escape(atob(b64)));
const state = JSON.parse(str);
bpmInput.value = state.bpm;
bpm = state.bpm;
mp3Input.value = state.url;
notes = state.notes || [];
loadAudio();
} catch(e) {
console.warn('Failed to load state from URL', e);
}
}
}
// Event bindings
loadBtn.onclick = () => {
bpm = parseInt(bpmInput.value) || 120;
loadAudio();
updateURLHash();
};
playPauseBtn.onclick = () => {
togglePlayPause();
};
shareBtn.onclick = () => {
updateURLHash();
prompt('Share this URL:', location.href);
};
audio.ontimeupdate = () => {
onTimeUpdate();
};
// Click on grid to add note at clicked beat
editor.onclick = (e) => {
// Calculate beat index from click X
const rect = editor.getBoundingClientRect();
const x = e.clientX - rect.left;
const beatIndex = Math.floor(x / noteWidth);
if(!notes.find(n=>n.beatIndex === beatIndex)) {
notes.push({beatIndex});
notes.sort((a,b)=>a.beatIndex-b.beatIndex);
updateNoteCount();
renderNotes();
updateURLHash();
}
};
// Keyboard shortcuts
window.onkeydown = (e) => {
if (e.target.tagName === 'INPUT') return; // do not override typing inputs
if(e.code === 'Space') {
e.preventDefault();
togglePlayPause();
} else if(e.key.toLowerCase() === 'a') {
e.preventDefault();
addNoteAtCurrent();
} else if(e.key.toLowerCase() === 'd') {
e.preventDefault();
deleteLastNote();
} else if(e.code === 'ArrowLeft') {
e.preventDefault();
seekByBeats(-1);
} else if(e.code === 'ArrowRight') {
e.preventDefault();
seekByBeats(1);
}
};
// Load state from URL on start
loadFromURLHash();
})();
</script>
</body>
</html>