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: 1em;
background: #111;
color: #eee;
}
header, main {
max-width: 900px;
margin: auto;
}
input[type="text"], input[type="number"] {
padding: 0.4em;
font-size: 1em;
width: 100%;
box-sizing: border-box;
margin-bottom: 1em;
}
#grid {
display: grid;
grid-template-columns: repeat(16, 1fr);
grid-gap: 2px;
margin-bottom: 1em;
}
.cell {
background: #222;
border: 1px solid #444;
padding-top: 100%; /* 1:1 ratio square*/
position: relative;
cursor: pointer;
}
.cell.note {
background: #f39c12;
}
.cell.current-bar {
border-color: #27ae60;
}
.cell.current-note {
box-shadow: 0 0 8px 4px #27ae60 inset;
}
.cell-label {
position: absolute;
top: 2px;
right: 4px;
font-size: 0.7em;
color: #666;
}
button {
padding: 0.5em 1em;
font-size: 1em;
margin-right: 1em;
cursor: pointer;
}
#status {
margin-top: 1em;
font-size: 1em;
}
#combo {
font-weight: bold;
color: #e74c3c;
}
nav {
margin-bottom: 1em;
}
label {
display: block;
margin-bottom: 0.2em;
}
</style>
</head>
<body>
<header>
<h1>Rhythm Game Editor</h1>
</header>
<main>
<nav>
<label for="audio-url">MP3 URL:</label>
<input type="text" id="audio-url" placeholder="Paste MP3 URL here" />
<label for="bpm">BPM (Beats Per Minute):</label>
<input type="number" id="bpm" value="120" min="30" max="300" />
<label for="bars">Bars (16 beats/4 bars default):</label>
<input type="number" id="bars" value="4" min="1" max="32" />
<button id="load-btn">Load Audio & Setup</button>
<button id="play-btn" disabled>Play</button>
<button id="share-btn">Copy Share URL</button>
</nav>
<div id="grid"></div>
<div id="status">
Bars Count: <span id="bars-count">0</span> | Combo: <span id="combo">0</span>
</div>
</main>
<script>
(() => {
const audioUrlInput = document.getElementById('audio-url');
const bpmInput = document.getElementById('bpm');
const barsInput = document.getElementById('bars');
const loadBtn = document.getElementById('load-btn');
const playBtn = document.getElementById('play-btn');
const shareBtn = document.getElementById('share-btn');
const grid = document.getElementById('grid');
const barsCountEl = document.getElementById('bars-count');
const comboEl = document.getElementById('combo');
let audio = new Audio();
audio.crossOrigin = "anonymous";
let bpm = 120;
let bars = 4;
const beatsPerBar = 4;
let totalBeats = 0;
let notes = new Set(); // Store as "index" strings, where index = beat number starting at 0 up to totalBeats-1
let playing = false;
let startTime = 0;
let animationFrameId = null;
let combo = 0;
// Helpers
function indexToGridPosition(index) {
// index 0..totalBeats-1
// returns {row, col}
return { row: Math.floor(index / 16), col: index % 16 };
}
function gridPositionToIndex(row, col) {
return row * 16 + col;
}
function buildGrid() {
grid.innerHTML = '';
totalBeats = bars * beatsPerBar; // e.g. 4 bars * 4 beats = 16 beats
barsCountEl.textContent = bars;
grid.style.gridTemplateColumns = `repeat(${Math.min(16, totalBeats)}, 1fr)`;
const rows = Math.ceil(totalBeats / 16);
for (let i = 0; i < totalBeats; i++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.index = i;
if (notes.has(String(i))) {
cell.classList.add('note');
}
// Small label for beat number (optional)
// const lbl = document.createElement('div');
// lbl.className = 'cell-label';
// lbl.textContent = i + 1;
// cell.appendChild(lbl);
cell.addEventListener('click', () => {
toggleNote(i);
});
grid.appendChild(cell);
}
}
function toggleNote(index) {
const idxStr = String(index);
if (notes.has(idxStr)) {
notes.delete(idxStr);
} else {
notes.add(idxStr);
}
updateGridNotes();
}
function updateGridNotes() {
const cells = grid.children;
for (let i = 0; i < cells.length; i++) {
const cell = cells[i];
if (notes.has(String(i))) {
cell.classList.add('note');
} else {
cell.classList.remove('note');
}
cell.classList.remove('current-note');
cell.classList.remove('current-bar');
}
}
// Playback controls
function play() {
if (!audio.src) return;
audio.currentTime = 0;
audio.play();
startTime = performance.now();
playing = true;
combo = 0;
comboEl.textContent = combo;
updateGridNotes();
requestAnimationFrame(run);
playBtn.textContent = 'Pause';
}
function pause() {
if (!playing) return;
audio.pause();
playing = false;
playBtn.textContent = 'Play';
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
updateGridNotes();
}
function run() {
if (!playing) return;
const elapsed = (performance.now() - startTime) / 1000; // seconds
// beats passed
const beatDuration = 60 / bpm;
const currentBeat = Math.floor(elapsed / beatDuration);
updateGridNotes();
if (currentBeat >= totalBeats) {
pause();
return;
}
// Highlight current beat cell and bar
const cells = grid.children;
for (let i = 0; i < cells.length; i++) {
const cell = cells[i];
// Bar highlight
const barIndex = Math.floor(currentBeat / beatsPerBar);
const cellBarIndex = Math.floor(i / beatsPerBar);
if (cellBarIndex === barIndex) {
cell.classList.add('current-bar');
} else {
cell.classList.remove('current-bar');
}
}
if (notes.has(String(currentBeat))) {
const noteCell = cells[currentBeat];
if (noteCell) {
noteCell.classList.add('current-note');
}
combo++;
}
comboEl.textContent = combo;
animationFrameId = requestAnimationFrame(run);
}
// Load audio and setup grid with inputs
function load() {
const url = audioUrlInput.value.trim();
if (!url) {
alert('Please enter a valid MP3 URL!');
return;
}
bpm = Math.max(30, Math.min(300, Number(bpmInput.value) || 120));
bars = Math.max(1, Math.min(32, Number(barsInput.value) || 4));
audio.src = url;
audio.load();
// Initialize notes empty on load
notes = new Set();
buildGrid();
playBtn.disabled = false;
// Reset combo display
combo = 0;
comboEl.textContent = combo;
}
// Serialize notes to string for URL hash
function serialize() {
// Store bpm, bars, notes as a JSON string encoded in base64 (compact-ish)
const data = {
bpm,
bars,
notes: Array.from(notes).map(n => Number(n)),
url: audioUrlInput.value.trim(),
};
return btoa(JSON.stringify(data));
}
// Deserialize URL hash to restore state
function deserialize(str) {
try {
const data = JSON.parse(atob(str));
if (data.url) {
audioUrlInput.value = data.url;
}
if (data.bpm) bpmInput.value = data.bpm;
if (data.bars) barsInput.value = data.bars;
bpm = data.bpm;
bars = data.bars;
notes = new Set(data.notes.map(n => String(n)));
load();
} catch (e) {
console.warn('Failed to load shared data', e);
}
}
// Copy shareable URL to clipboard
function shareUrl() {
const encoded = serialize();
const fullUrl = `${location.origin}${location.pathname}#${encoded}`;
navigator.clipboard.writeText(fullUrl).then(() => {
alert('Share URL copied to clipboard!');
}, () => {
alert('Failed to copy URL');
});
}
// Handle keyboard shortcuts
function onKeyDown(e) {
if (e.target.tagName === 'INPUT') return; // ignore inputs
if (e.key === ' ') {
e.preventDefault();
if (playing) pause();
else play();
} else if (e.key === 'Delete' || e.key === 'Backspace') {
// Delete all selected notes or last placed note for demo just remove last note
if (notes.size > 0) {
// Remove last note (max index)
const maxNoteIndex = Math.max(...Array.from(notes).map(Number));
notes.delete(String(maxNoteIndex));
updateGridNotes();
}
} else if (e.key >= '1' && e.key <= '9') {
// Shortcut: toggle note at beat (key-1) * Some factor, demo: first 9 beats
const beatIndex = parseInt(e.key) - 1;
if (beatIndex < totalBeats) {
toggleNote(beatIndex);
}
}
}
// On load: if hash present try to restore
window.addEventListener('DOMContentLoaded', () => {
if (location.hash.length > 1) {
deserialize(location.hash.slice(1));
}
});
loadBtn.addEventListener('click', load);
playBtn.addEventListener('click', () => {
if (playing) pause();
else play();
});
shareBtn.addEventListener('click', shareUrl);
window.addEventListener('keydown', onKeyDown);
// On BPM or bars input change, rebuild grid
bpmInput.addEventListener('change', () => {
bpm = Math.max(30, Math.min(300, Number(bpmInput.value) || 120));
});
barsInput.addEventListener('change', () => {
bars = Math.max(1, Math.min(32, Number(barsInput.value) || 4));
buildGrid();
});
})();
</script>
</body>
</html>