Sure! Below is a simple yet functional rhythm game map editor built with HTML, CSS, and JavaScript. It supports:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Rhythm Game Map Editor</title>
<style>
body {
font-family: Arial, sans-serif;
background: #121212;
color: #eee;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
header {
background: #1e1e1e;
padding: 10px 20px;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 10px;
}
header input[type="text"],
header input[type="number"] {
padding: 6px;
border-radius: 4px;
border: none;
font-size: 14px;
width: 200px;
}
header button {
padding: 6px 12px;
background: #3a3a3a;
border: none;
border-radius: 4px;
color: #eee;
cursor: pointer;
font-size: 14px;
}
header button:hover {
background: #5a5a5a;
}
#status {
font-size: 14px;
flex: 1;
min-width: 100px;
}
main {
flex: 1;
background: #222;
position: relative;
overflow: hidden;
}
#grid {
position: relative;
height: 100%;
overflow-x: auto;
white-space: nowrap;
}
.bar {
display: inline-block;
position: relative;
border-left: 2px solid #555;
height: 100%;
width: 320px; /* 4 beats * 80px per beat */
box-sizing: border-box;
}
.bar:first-child {
border-left: none;
}
.beat {
position: absolute;
width: 2px;
background: #555;
top: 0;
bottom: 0;
}
.beat.strong {
background: #f55;
}
#notes {
position: absolute;
top: 0;
left: 0;
height: 100%;
pointer-events: none;
}
.note {
position: absolute;
top: 50%;
width: 20px;
height: 20px;
background: #4af;
border-radius: 50%;
transform: translate(-50%, -50%);
pointer-events: auto;
cursor: pointer;
transition: background-color 0.2s ease;
}
.note.delete-mode {
background: #f44;
}
#playhead {
position: absolute;
top: 0;
bottom: 0;
width: 3px;
background: #0f0;
pointer-events: none;
z-index: 10;
}
#shareOutput {
background: #333;
color: #eee;
font-size: 14px;
padding: 6px;
margin-left: auto;
border-radius: 4px;
min-width: 300px;
overflow-wrap: anywhere;
}
@media (max-width: 600px) {
#grid {
width: 100%;
}
header input[type="text"] {
width: 100%;
flex: 1 0 100%;
}
#shareOutput {
min-width: unset;
margin-left: 0;
}
}
</style>
</head>
<body>
<header>
<input type="text" id="audioURL" placeholder="Enter audio URL here (CORS enabled)" />
<button id="loadAudioBtn">Load Audio</button>
<label for="bpmInput">BPM:</label>
<input type="number" id="bpmInput" min="30" max="300" step="1" value="120" />
<label for="barsInput">Bars (4 beats per bar):</label>
<input type="number" id="barsInput" min="1" max="100" step="1" value="4" />
<button id="playPauseBtn" disabled>Play</button>
<button id="deleteModeBtn">Delete Mode: Off</button>
<button id="shareBtn">Share</button>
<div id="shareOutput"></div>
<div id="status"></div>
</header>
<main>
<div id="grid"></div>
<canvas id="notes" height="100"></canvas>
<div id="playhead"></div>
</main>
<script>
(() => {
const audioURLInput = document.getElementById('audioURL');
const loadAudioBtn = document.getElementById('loadAudioBtn');
const bpmInput = document.getElementById('bpmInput');
const barsInput = document.getElementById('barsInput');
const playPauseBtn = document.getElementById('playPauseBtn');
const deleteModeBtn = document.getElementById('deleteModeBtn');
const shareBtn = document.getElementById('shareBtn');
const shareOutput = document.getElementById('shareOutput');
const statusDiv = document.getElementById('status');
const gridDiv = document.getElementById('grid');
const notesCanvas = document.getElementById('notes');
const playheadDiv = document.getElementById('playhead');
const ctx = notesCanvas.getContext('2d');
// Constants
const BEAT_WIDTH_PX = 80; // Width of one beat
const BEATS_PER_BAR = 4;
const NOTE_RADIUS = 8;
const GRID_HEIGHT = 100;
notesCanvas.height = GRID_HEIGHT;
// State
let audioCtx, audioBuffer, audioSource;
let isPlaying = false;
let startTime = 0; // when audio started
let pauseTime = 0; // paused at (seconds)
let bpm = 120;
let bars = 4;
let duration = 0; // audio duration in seconds
let scheduledNotes = []; // Array of {beatIndex} e.g. 0..bars*beats-1
let deleteMode = false;
// Initialize grid and notes positions
function buildGrid() {
gridDiv.innerHTML = '';
// Width = bars * beats * width per beat
const totalBeats = bars * BEATS_PER_BAR;
gridDiv.style.width = `${BEAT_WIDTH_PX * totalBeats}px`;
playheadDiv.style.height = `${GRID_HEIGHT}px`;
for(let barIdx = 0; barIdx < bars; ++barIdx) {
const barDiv = document.createElement('div');
barDiv.className = 'bar';
barDiv.style.width = `${BEAT_WIDTH_PX * BEATS_PER_BAR}px`;
barDiv.style.position = 'relative';
// Add beat lines
for(let beatIdx = 0; beatIdx < BEATS_PER_BAR; ++beatIdx) {
const beatLine = document.createElement('div');
beatLine.className = 'beat';
if(beatIdx === 0) beatLine.classList.add('strong');
beatLine.style.left = `${(beatIdx * BEAT_WIDTH_PX)}px`;
// Allow clicking on beats:
beatLine.dataset.beatIndex = barIdx * BEATS_PER_BAR + beatIdx;
barDiv.appendChild(beatLine);
}
gridDiv.appendChild(barDiv);
}
// Resize notes canvas to grid size to overlay notes properly:
notesCanvas.width = BEAT_WIDTH_PX * totalBeats;
notesCanvas.style.width = `${notesCanvas.width}px`;
}
// Draw notes
function drawNotes() {
ctx.clearRect(0, 0, notesCanvas.width, notesCanvas.height);
scheduledNotes.forEach(noteBeatIndex => {
const x = noteBeatIndex * BEAT_WIDTH_PX + BEAT_WIDTH_PX / 2;
const y = GRID_HEIGHT / 2;
ctx.beginPath();
ctx.arc(x, y, NOTE_RADIUS, 0, Math.PI * 2);
ctx.fillStyle = deleteMode ? '#f44' : '#4af';
ctx.fill();
ctx.lineWidth = 2;
ctx.strokeStyle = '#222';
ctx.stroke();
});
}
// Add note at beat index
function addNote(beatIndex) {
if (beatIndex < 0 || beatIndex >= bars * BEATS_PER_BAR) return;
if (!scheduledNotes.includes(beatIndex)) {
scheduledNotes.push(beatIndex);
scheduledNotes.sort((a, b) => a - b);
drawNotes();
}
}
// Delete note at beat index (closest one within a threshold)
function deleteNoteAt(beatIndex) {
const threshold = 0.5; // in beats
let closestIdx = -1;
let closestDist = Infinity;
for(let i = 0; i < scheduledNotes.length; ++i) {
const dist = Math.abs(scheduledNotes[i] - beatIndex);
if(dist < closestDist && dist <= threshold) {
closestDist = dist;
closestIdx = i;
}
}
if(closestIdx >= 0) {
scheduledNotes.splice(closestIdx, 1);
drawNotes();
}
}
// Get current beat index from time
function getBeatFromTime(timeSec) {
// beatLength in seconds = 60/BPM
const beatLen = 60 / bpm;
return timeSec / beatLen;
}
// Get time from beat index
function getTimeFromBeat(beatIndex) {
const beatLen = 60 / bpm;
return beatIndex * beatLen;
}
// Play / Pause control
function playAudio() {
if(!audioBuffer) return;
if(!audioCtx) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}
if(audioCtx.state === 'suspended') {
audioCtx.resume();
}
audioSource = audioCtx.createBufferSource();
audioSource.buffer = audioBuffer;
audioSource.connect(audioCtx.destination);
// Calculate offset to start at pauseTime
startTime = audioCtx.currentTime - pauseTime;
audioSource.start(0, pauseTime);
isPlaying = true;
playPauseBtn.textContent = 'Pause';
// When audio ends
audioSource.onended = () => {
isPlaying = false;
playPauseBtn.textContent = 'Play';
pauseTime = 0;
updatePlayhead();
};
}
function pauseAudio() {
if(audioSource) {
try {
audioSource.stop();
} catch(e) {}
audioSource.disconnect();
audioSource = null;
}
if(audioCtx) {
pauseTime = audioCtx.currentTime - startTime;
}
isPlaying = false;
playPauseBtn.textContent = 'Play';
}
function togglePlayPause() {
if(isPlaying) {
pauseAudio();
} else {
playAudio();
}
}
// Update playhead position on grid
function updatePlayhead() {
if(!isPlaying) {
// If paused, position playhead at paused place
const beatPos = getBeatFromTime(pauseTime);
playheadDiv.style.left = `${beatPos * BEAT_WIDTH_PX}px`;
return;
}
if(!audioCtx) return;
const currentTime = audioCtx.currentTime - startTime;
if(currentTime >= duration) {
pauseAudio();
return;
}
const beatPos = getBeatFromTime(currentTime);
playheadDiv.style.left = `${beatPos * BEAT_WIDTH_PX}px`;
requestAnimationFrame(updatePlayhead);
}
// Load audio from URL with fetch and decode
async function loadAudioFromURL(url) {
try {
statusDiv.textContent = 'Loading audio...';
if(!audioCtx) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}
const response = await fetch(url, {mode:'cors'});
if(!response.ok) throw new Error('Fetch failed');
const arrayBuffer = await response.arrayBuffer();
audioBuffer = await audioCtx.decodeAudioData(arrayBuffer);
duration = audioBuffer.duration;
statusDiv.textContent = `Audio loaded - Duration: ${duration.toFixed(2)} s`;
playPauseBtn.disabled = false;
pauseTime = 0;
isPlaying = false;
playPauseBtn.textContent = 'Play';
// Reset notes and grid if you want to here? (leaving as is)
return true;
} catch(e) {
console.error(e);
statusDiv.textContent = 'Failed to load audio (check CORS and URL)';
playPauseBtn.disabled = true;
return false;
}
}
// Event handlers
// Clicking on grid to add/delete note
gridDiv.addEventListener('click', e => {
if(deleteMode) return; // disable adding notes
// Calculate beat index based on click x pos relative to gridDiv
const rect = gridDiv.getBoundingClientRect();
const x = e.clientX - rect.left + gridDiv.scrollLeft;
const beatIndex = Math.floor(x / BEAT_WIDTH_PX);
if(deleteMode) {
deleteNoteAt(beatIndex);
} else {
addNote(beatIndex);
}
});
// Adding notes by clicking the lines (beat lines)
gridDiv.querySelectorAll('.beat').forEach(beatLine => {
beatLine.addEventListener('click', e => {
e.stopPropagation();
const bidx = +beatLine.dataset.beatIndex;
if(deleteMode) {
deleteNoteAt(bidx);
} else {
addNote(bidx);
}
});
});
// Keyboard shortcuts for add/delete note at playhead position
document.addEventListener('keydown', e => {
if(e.target.tagName === 'INPUT') return; // avoid typing interference
if(e.code === 'Space') {
e.preventDefault();
if(!audioCtx) return;
if(!audioBuffer) return;
const currentTime = isPlaying ? (audioCtx.currentTime - startTime) : pauseTime;
const beatIndex = Math.round(getBeatFromTime(currentTime));
if(deleteMode) {
deleteNoteAt(beatIndex);
} else {
addNote(beatIndex);
}
}
else if(e.code === 'Backspace') {
e.preventDefault();
if(!audioCtx) return;
if(!audioBuffer) return;
const currentTime = isPlaying ? (audioCtx.currentTime - startTime) : pauseTime;
const beatIndex = Math.round(getBeatFromTime(currentTime));
deleteNoteAt(beatIndex);
}
});
// Handle Load Audio button
loadAudioBtn.addEventListener('click', async () => {
const url = audioURLInput.value.trim();
if(!url) return alert('Enter audio URL first');
const success = await loadAudioFromURL(url);
if(success) {
buildGrid();
drawNotes();
setupGridClicks();
updatePlayhead();
}
});
// Handle BPM change
bpmInput.addEventListener('change', () => {
const val = +bpmInput.value;
if(val < 30) bpmInput.value = 30;
if(val > 300) bpmInput.value = 300;
bpm = +bpmInput.value;
buildGrid();
drawNotes();
updatePlayhead();
});
// Handle Bars change
barsInput.addEventListener('change', () => {
let val = +barsInput.value;
if(val < 1) barsInput.value = 1;
if(val > 100) barsInput.value = 100;
bars = +barsInput.value;
buildGrid();
drawNotes();
updatePlayhead();
});
// Play/Pause button
playPauseBtn.addEventListener('click', () => {
togglePlayPause();
if(isPlaying) updatePlayhead();
});
// Delete Mode toggle
deleteModeBtn.addEventListener('click', () => {
deleteMode = !deleteMode;
deleteModeBtn.textContent = `Delete Mode: ${deleteMode ? 'On' : 'Off'}`;
drawNotes();
});
// Share button - encode map and parameters into URL hash
shareBtn.addEventListener('click', () => {
const data = {
audioURL: audioURLInput.value.trim(),
bpm,
bars,
notes: scheduledNotes
};
try {
const json = JSON.stringify(data);
const encoded = btoa(unescape(encodeURIComponent(json)));
const url = `${location.origin}${location.pathname}#/` + encoded;
shareOutput.textContent = url;
} catch(e) {
alert('Failed to generate share link');
}
});
// Load from URL hash on page load
function loadFromHash() {
if(location.hash.startsWith('#/')) {
const encoded = location.hash.slice(2);
try {
const json = decodeURIComponent(escape(atob(encoded)));
const data = JSON.parse(json);
if (data.audioURL) {
audioURLInput.value = data.audioURL;
}
if(data.bpm) {
bpm = data.bpm;
bpmInput.value = bpm;
}
if(data.bars) {
bars = data.bars;
barsInput.value = bars;
}
if(Array.isArray(data.notes)) {
scheduledNotes = data.notes;
} else {
scheduledNotes = [];
}
loadAudioFromURL(data.audioURL).then(ok => {
if(ok) {
buildGrid();
drawNotes();
updatePlayhead();
}
});
} catch (e) {
console.warn('Failed to load map from URL hash', e);
}
}
}
// Setup beat line click listeners again (must be after buildGrid called)
function setupGridClicks() {
gridDiv.querySelectorAll('.beat').forEach(beatLine => {
beatLine.addEventListener('click', e => {
e.stopPropagation();
const bidx = +beatLine.dataset.beatIndex;
if(deleteMode) {
deleteNoteAt(bidx);
} else {
addNote(bidx);
}
});
});
}
// Initialize
buildGrid();
drawNotes();
setupGridClicks();
loadFromHash();
})();
</script>
</body>
</html>