Certainly! Here's a simple rhythm game editor implemented with HTML, CSS, and JavaScript featuring:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Rhythm Game Editor</title>
<style>
body {
font-family: Arial, sans-serif;
margin:0; padding: 10px;
background: #111;
color: #eee;
user-select: none;
}
header {
margin-bottom: 10px;
}
input[type=text] {
width: 60vw;
font-size: 1rem;
}
button {
font-size: 1rem;
padding: 5px 10px;
margin-left: 5px;
cursor: pointer;
}
#audio-controls {
margin: 10px 0;
}
#timeline {
overflow-x: auto;
white-space: nowrap;
background: #222;
border: 1px solid #555;
position: relative;
height: 120px;
user-select:none;
}
.bar {
display: inline-block;
border-left: 1px solid #444;
border-bottom: 1px solid #555;
height: 100%;
width: 160px; /* 4 beats * 40px per beat */
position: relative;
vertical-align: top;
}
.beat {
position: absolute;
height: 100%;
border-right: 1px solid #333;
top: 0; bottom: 0;
}
.note {
background-color: #0f0;
width: 32px;
height: 32px;
border-radius: 4px;
position: absolute;
top: 40px;
cursor: pointer;
opacity: 0.9;
}
.note.selected {
outline: 2px solid yellow;
}
#status {
margin-top: 10px;
}
#bpm-container {
margin: 10px 0;
}
#share-url {
width: 100%;
margin-top:5px;
}
</style>
</head>
<body>
<header>
<label>Audio URL: <input id="audio-url" type="text" placeholder="Paste audio URL and press Load" /></label>
<button id="load-button">Load</button>
</header>
<div id="audio-controls" style="display:none;">
<button id="play-pause">Play</button>
<button id="stop">Stop</button>
<button id="record-toggle">Record</button>
<button id="clear-notes">Clear Notes</button>
<button id="share">Share</button>
<div id="bpm-container">
BPM: <input type="number" id="bpm-input" value="120" min="40" max="300" />
Bars: <input type="number" id="bars-input" value="16" min="1" max="64" />
</div>
</div>
<div id="timeline"></div>
<div id="status">Status: Idle</div>
<textarea id="share-url" readonly placeholder="Share URL will appear here"></textarea>
<audio id="audio" crossorigin="anonymous"></audio>
<script>
(function() {
const audioURLInput = document.getElementById('audio-url');
const loadBtn = document.getElementById('load-button');
const audio = document.getElementById('audio');
const playPauseBtn = document.getElementById('play-pause');
const stopBtn = document.getElementById('stop');
const recordToggleBtn = document.getElementById('record-toggle');
const clearNotesBtn = document.getElementById('clear-notes');
const shareBtn = document.getElementById('share');
const timeline = document.getElementById('timeline');
const status = document.getElementById('status');
const bpmInput = document.getElementById('bpm-input');
const barsInput = document.getElementById('bars-input');
const shareUrlTextarea = document.getElementById('share-url');
// Configuration
let BPM = 120;
let beatsPerBar = 4;
let barsCount = 16;
let beatWidthPx = 40;
let noteSizePx = 32;
// State
let notes = []; // {bar, beat}
let selectedNote = null;
let isPlaying = false;
let isRecording = false;
let selectionIndex = -1;
function setStatus(text) {
status.textContent = 'Status: ' + text;
}
// Helpers to map time to bar/beat
// Beat duration = 60/BPM
// Each bar: beatsPerBar beats
function timeToBarBeat(time) {
let beatDuration = 60 / BPM;
let totalBeats = time / beatDuration;
let bar = Math.floor(totalBeats / beatsPerBar);
let beat = Math.floor(totalBeats % beatsPerBar);
if (bar >= barsCount) return null;
return {bar, beat};
}
function barBeatToTime(bar, beat) {
let beatDuration = 60 / BPM;
return (bar * beatsPerBar + beat) * beatDuration;
}
function buildTimeline() {
timeline.innerHTML = '';
for(let b=0; b<barsCount; b++) {
const barEl = document.createElement('div');
barEl.classList.add('bar');
barEl.style.width = (beatsPerBar * beatWidthPx) + 'px';
barEl.dataset.bar = b;
for(let bt=0; bt<=beatsPerBar; bt++) { // beat lines
let beatLine = document.createElement('div');
beatLine.classList.add('beat');
beatLine.style.left = (bt * beatWidthPx) + 'px';
beatLine.style.borderRightColor = (bt % beatsPerBar === 0) ? '#666' : '#333';
barEl.appendChild(beatLine);
}
// Label bar number on top-left
let label = document.createElement('div');
label.textContent = 'Bar ' + (b+1);
label.style.position = 'absolute';
label.style.top = '0';
label.style.left ='0';
label.style.color = '#888';
label.style.fontSize = '10px';
label.style.padding = '2px';
label.style.userSelect = 'none';
barEl.appendChild(label);
timeline.appendChild(barEl);
}
updateNotes();
}
function updateNotes() {
// Clear existing notes from timeline
document.querySelectorAll('.note').forEach(n => n.remove());
notes.forEach((note, i) => {
const barEl = timeline.querySelector(`.bar[data-bar='${note.bar}']`);
if (!barEl) return;
let noteEl = document.createElement('div');
noteEl.classList.add('note');
if (i === selectionIndex) {
noteEl.classList.add('selected');
}
// Position note within bar + beat
noteEl.style.left = (note.beat * beatWidthPx + (beatWidthPx - noteSizePx)/2) + 'px';
noteEl.dataset.index = i;
noteEl.title = `Bar ${note.bar+1} Beat ${note.beat+1}`;
barEl.appendChild(noteEl);
noteEl.addEventListener('click', e => {
e.stopPropagation();
selectionIndex = i;
updateNotes();
});
});
}
function encodeNotes(notes) {
// Encode notes as base64 compressed string or simple JSON then base64
// For simplicity: JSON and base64
return btoa(JSON.stringify(notes));
}
function decodeNotes(str) {
try {
return JSON.parse(atob(str));
} catch(e) {
return [];
}
}
function loadFromHash() {
if(window.location.hash.length > 2){
try {
const hash = window.location.hash.substring(2);
// format: bpm|bars|notes
const parts = decodeURIComponent(hash).split('|');
BPM = parseInt(parts[0]) || 120;
beatsPerBar = 4;
barsCount = parseInt(parts[1]) || 16;
bpmInput.value = BPM;
barsInput.value = barsCount;
notes = decodeNotes(parts[2]) || [];
buildTimeline();
updateNotes();
setStatus("Loaded from URL");
} catch(e) {
setStatus("Error loading from URL");
}
}
}
function updateHash() {
const dataStr = encodeNotes(notes);
const hashStr = encodeURIComponent(BPM + '|' + barsCount + '|' + dataStr);
window.location.hash = "#/" + hashStr;
}
function clearSelection() {
selectionIndex = -1;
updateNotes();
}
function playAudio() {
if (!audio.src) return;
audio.play();
isPlaying = true;
playPauseBtn.textContent = 'Pause';
setStatus("Playing");
}
function pauseAudio() {
if (!audio.src) return;
audio.pause();
isPlaying = false;
playPauseBtn.textContent = 'Play';
setStatus("Paused");
}
function stopAudio() {
if(!audio.src) return;
audio.pause();
audio.currentTime = 0;
isPlaying = false;
playPauseBtn.textContent = 'Play';
setStatus("Stopped");
}
// On timeline grid click - place/remove note at bar/beat clicked
timeline.addEventListener('click', e => {
if(!audio.src) return;
const rect = timeline.getBoundingClientRect();
const x = e.clientX - rect.left + timeline.scrollLeft;
const barIndex = Math.floor(x / (beatsPerBar * beatWidthPx));
if(barIndex<0 || barIndex>= barsCount) return;
const barOffset = x - barIndex * beatsPerBar * beatWidthPx;
const beatIndex = Math.floor(barOffset / beatWidthPx);
if(beatIndex<0 || beatIndex >= beatsPerBar) return;
// Check if note exists on bar, beat
const foundIndex = notes.findIndex(n => n.bar === barIndex && n.beat === beatIndex);
if(foundIndex !== -1){
// remove note
notes.splice(foundIndex,1);
clearSelection();
} else {
notes.push({bar: barIndex, beat: beatIndex});
selectionIndex = notes.length - 1;
}
updateNotes();
updateHash();
});
// Load audio from URL input
loadBtn.addEventListener('click', () => {
const url = audioURLInput.value.trim();
if(!url) return;
audio.src = url;
audio.crossOrigin = "anonymous";
audio.load();
document.getElementById('audio-controls').style.display = 'block';
setStatus('Audio loaded, ready');
notes = [];
buildTimeline();
updateNotes();
clearSelection();
updateHash();
});
playPauseBtn.addEventListener('click', () => {
if(!audio.src) return;
if(isPlaying) pauseAudio();
else playAudio();
});
stopBtn.addEventListener('click', () => {
stopAudio();
});
recordToggleBtn.addEventListener('click', () => {
if(!audio.src) return;
if(isRecording){
isRecording = false;
recordToggleBtn.textContent = 'Record';
setStatus('Stopped recording');
} else {
if(!isPlaying) playAudio();
notes = [];
buildTimeline();
updateNotes();
isRecording = true;
recordToggleBtn.textContent = 'Stop Rec';
setStatus('Recording...');
}
});
clearNotesBtn.addEventListener('click', () => {
notes = [];
clearSelection();
buildTimeline();
updateNotes();
updateHash();
setStatus('Cleared notes');
});
bpmInput.addEventListener('change', () => {
BPM = parseInt(bpmInput.value);
buildTimeline();
updateNotes();
updateHash();
});
barsInput.addEventListener('change', () => {
barsCount = parseInt(barsInput.value);
buildTimeline();
updateNotes();
updateHash();
});
shareBtn.addEventListener('click', () => {
updateHash();
shareUrlTextarea.value = window.location.href;
});
// When audio plays, if recording, add note for each beat passed
let lastRecordedBeat = -1;
audio.addEventListener('timeupdate', () => {
if(isRecording){
let bb = timeToBarBeat(audio.currentTime);
if(!bb) return;
let index = bb.bar * beatsPerBar + bb.beat;
if(index !== lastRecordedBeat){
lastRecordedBeat = index;
// add note if doesn't exist
if(!notes.find(n=>n.bar===bb.bar && n.beat===bb.beat)){
notes.push({bar: bb.bar, beat: bb.beat});
selectionIndex = notes.length - 1;
updateNotes();
updateHash();
}
}
}
});
// Keyboard shortcuts
document.body.addEventListener('keydown', e => {
if(!audio.src) return;
if(e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
switch(e.key.toLowerCase()){
case ' ': // Play/pause space
e.preventDefault();
if(isPlaying) pauseAudio();
else playAudio();
break;
case 'r': // Record toggle
e.preventDefault();
recordToggleBtn.click();
break;
case 'delete':
case 'backspace': // Delete selected note
if(selectionIndex >= 0 && notes[selectionIndex]){
notes.splice(selectionIndex,1);
selectionIndex = -1;
updateNotes();
updateHash();
setStatus('Deleted selected note');
}
break;
case 'arrowright':
if(notes.length===0) break;
e.preventDefault();
if(selectionIndex < notes.length-1) selectionIndex++;
else selectionIndex = 0;
updateNotes();
break;
case 'arrowleft':
if(notes.length===0) break;
e.preventDefault();
if(selectionIndex > 0) selectionIndex--;
else selectionIndex = notes.length-1;
updateNotes();
break;
}
});
// On page load check if initial data is in URL
loadFromHash();
})();
</script>
</body>
</html>