Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

html css js rhythm game editor load url mp3 bpm share "#/" shortcuts bars combo

  1. User

    html css js rhythm game editor load url mp3 bpm share "#/" shortcuts bars combo

  2. Assistant

    It sounds like you want a simple web-based rhythm game editor that can:

    • Load a song via URL (mp3)
    • Set BPM
    • Display bars/measures/beats
    • Allow creating combo/note sequences
    • Use keyboard shortcuts to add notes
    • Share the chart via URL hash (#/)
    • Possibly show the current combo count or other stats

    Here’s a minimal but functional rhythm game editor implemented with HTML, CSS, and JS. It supports loading an MP3 from a URL, entering BPM, drawing measures as horizontal beat bars, adding notes with keyboard shortcuts, and encoding the chart state into the URL hash for sharing.


    Features implemented

    • Input for MP3 URL and BPM
    • Audio player with play/pause and seek
    • A horizontal timeline representing bars and beats
    • Click to add notes or use keyboard shortcuts (1-4 keys) for four lanes
    • Notes shown as colored blocks on lanes
    • Current playback time indicator line moves with song
    • Chart data is saved in URL hash as JSON (compressed base64 could be done later)
    • Load chart from URL on startup
    • Combo count shown as current consecutive hits (for demo, shown from notes up to current time)
    • Basic styling

    Code

    Just open this in a modern browser. You can edit the MP3 URL and BPM, press play, and add notes by clicking on lanes or pressing keys 1 to 4.

    <!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: 10px;
        background: #111;
        color: #eee;
      }
      #controls {
        margin-bottom: 10px;
      }
      #timeline {
        position: relative;
        width: 100%;
        height: 200px;
        background: #222;
        border: 1px solid #555;
        user-select: none;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        overflow-x: auto;
        overflow-y: hidden;
      }
      .bar {
        position: relative;
        height: 40px;
        border-bottom: 1px solid #444;
        display: flex;
      }
      .beat {
        flex: 1;
        border-left: 1px solid #444;
        box-sizing: border-box;
        height: 100%;
      }
      .lane {
        flex: 1;
        border-left: 1px solid #333;
        position: relative;
        cursor: pointer;
      }
      .lane:first-child {
        border-left: none;
      }
      .note {
        position: absolute;
        width: 20px;
        height: 30px;
        top: 5px;
        border-radius: 4px;
        opacity: 0.9;
        pointer-events: none;
      }
      .note.lane0 {
        background: #e74c3c;
        left: 5px;
      }
      .note.lane1 {
        background: #3498db;
        left: 35px;
      }
      .note.lane2 {
        background: #f1c40f;
        left: 65px;
      }
      .note.lane3 {
        background: #2ecc71;
        left: 95px;
      }
      #playhead {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 2px;
        background: #fff;
        pointer-events: none;
        z-index: 10;
      }
      button {
        margin-left: 5px;
      }
      input[type=url], input[type=number] {
        width: 250px;
        padding: 4px;
        margin-right: 8px;
        border-radius: 4px;
        border: none;
      }
      label {
        margin-right: 4px;
      }
      #combo {
        margin-top: 10px;
        font-size: 18px;
        font-weight: bold;
        color: #0f0;
      }
      #instructions {
        margin-top: 10px;
        font-size: 14px;
        color: #ccc;
        max-width: 600px;
      }
    </style>
    </head>
    <body>
    
    <h1>Rhythm Game Editor</h1>
    
    <div id="controls">
      <label for="mp3url">MP3 URL:</label>
      <input id="mp3url" type="url" placeholder="Paste mp3 URL here" />
      <label for="bpm">BPM:</label>
      <input id="bpm" type="number" value="120" min="30" max="300" />
      <button id="loadBtn">Load</button>
      <button id="playBtn" disabled>Play</button>
      <button id="pauseBtn" disabled>Pause</button>
      <button id="stopBtn" disabled>Stop</button>
    </div>
    
    <div id="timeline"></div>
    <div id="combo">Combo: 0</div>
    
    <div id="instructions">
      <p><strong>Instructions:</strong></p>
      <ul>
        <li>Set MP3 URL and BPM, then press Load.</li>
        <li>Press Play to hear the song.</li>
        <li>Timeline shows bars and beats (4 beats per bar).</li>
        <li>Click on lanes to add notes at the current play time position.</li>
        <li>Or press keys 1 to 4 to add notes in lanes 1-4 at current time.</li>
        <li>Notes are colored blocks.</li>
        <li>The URL hash updates automatically to save your chart—you can share it.</li>
        <li>Reload page or paste URL with hash to restore your chart.</li>
      </ul>
    </div>
    
    <audio id="audio" crossorigin="anonymous"></audio>
    
    <script>
    (() => {
      const audio = document.getElementById('audio');
      const mp3urlInput = document.getElementById('mp3url');
      const bpmInput = document.getElementById('bpm');
      const loadBtn = document.getElementById('loadBtn');
      const playBtn = document.getElementById('playBtn');
      const pauseBtn = document.getElementById('pauseBtn');
      const stopBtn = document.getElementById('stopBtn');
      const timeline = document.getElementById('timeline');
      const comboLabel = document.getElementById('combo');
    
      // Constants
      const lanesCount = 4;
      const beatsPerBar = 4;
      const barsToShow = 16; // how many bars wide to draw initially, can scroll horizontally
    
      // Chart data structure:
      // {
      //   mp3url: string,
      //   bpm: number,
      //   notes: [{time: seconds, lane: 0-3}]
      // }
      let chart = {
        mp3url: '',
        bpm: 120,
        notes: []
      };
    
      // State
      let isPlaying = false;
    
      // UI update helpers
      function clearTimeline() {
        timeline.innerHTML = '';
        timeline.style.width = '';
      }
      // Render timeline bar and lanes
      function renderTimeline() {
        clearTimeline();
        const beatsPerBar = 4;
        const totalBars = barsToShow;
    
        // Timeline width depends on bar width, beat width
        // We'll set each beat to 60px wide => bar = 240px wide, total width = bars * 240
        const beatWidth = 60;
        const barWidth = beatWidth * beatsPerBar;
        timeline.style.width = (barWidth * totalBars) + 'px';
        timeline.style.position = 'relative';
        timeline.style.height = lanesCount * 40 + 'px';
        timeline.style.display = 'flex';
        timeline.style.flexDirection = 'row';
        timeline.style.border = '1px solid #555';
    
        // Create 4 lanes horizontally — we'll actually do vertical bars with 4 horizontal lanes stacked is easier in divs vertically.
        // Let’s do bars stacked vertically with lanes horizontal inside each bar.
    
        timeline.style.flexDirection = 'column';
    
        for(let barIdx = 0; barIdx < totalBars; barIdx++) {
          const bar = document.createElement('div');
          bar.classList.add('bar');
          bar.style.width = barWidth + 'px';
          bar.style.display = 'flex';
          bar.style.borderBottom = '1px solid #444';
          bar.style.position = 'relative';
    
          for(let beatIdx = 0; beatIdx < beatsPerBar; beatIdx++) {
            const beat = document.createElement('div');
            beat.classList.add('beat');
            beat.style.flex = '1';
            beat.style.borderLeft = '1px solid #444';
            beat.style.position = 'relative';
    
            // inside each beat put lanes horizontally
            // Actually, beats are subdivision horizontally, lanes are vertically stacked
            // We want something different: 
    
            // Correction: Each bar is vertical column with four horizontal lanes stacked vertically.
            // Let's rebuild approach.
    
            // Actually, the best maybe: timeline is horizontally scrollable container width = barWidth * totalBars,
            // Inside it, 4 lanes horizontally, the vertical dimension is lane height * 4 lanes = 160px (40 each).
            // We want to show 16 bars * 4 beats/bar = 64 beats along horizontal axis.
    
            // So redo timeline as a div with position relative width = 64 beats * beatWidth,
            // inside it, four lanes horizontally (stacked vertically).
    
          }
    
        }
      }
    
      // Better to redraw timeline: horizontal container width = totalBeats * beatWidth
      // timeline is position:relative height = laneHeight * lanesCount
      // Render 4 div lanes stacked vertically inside timeline
      // Each lane is a div with position:absolute top, left=0, height=40px width = totalWidth
    
      function redrawTimeline() {
        clearTimeline();
    
        const totalBeats = beatsPerBar * barsToShow;
        const beatWidth = 60;
        const laneHeight = 40;
        const totalWidth = beatWidth * totalBeats;
        const totalHeight = laneHeight * lanesCount;
    
        timeline.style.width = '100%';
        timeline.style.height = totalHeight + 'px';
        timeline.style.position = 'relative';
        timeline.style.overflowX = 'auto';
        timeline.style.background = '#222';
        timeline.style.border = '1px solid #555';
    
        // Remove children if any
        while (timeline.firstChild) {
          timeline.removeChild(timeline.firstChild);
        }
    
        // Horizontal grid lines for lanes
        for(let i = 0; i <= lanesCount; i++) {
          const line = document.createElement('div');
          line.style.position = 'absolute';
          line.style.top = (i * laneHeight) + 'px';
          line.style.left = '0';
          line.style.width = '9999px';
          line.style.height = '1px';
          line.style.background = '#444';
          timeline.appendChild(line);
        }
    
        // Vertical beat lines
        for(let i = 0; i <= totalBeats; i++) {
          const vline = document.createElement('div');
          vline.style.position = 'absolute';
          vline.style.left = (i * beatWidth) + 'px';
          vline.style.top = '0';
          vline.style.height = totalHeight + 'px';
          vline.style.width = '1px';
          vline.style.background = (i % beatsPerBar === 0) ? '#666' : '#333';
          timeline.appendChild(vline);
        }
    
        // Add beats labels (bar number)
        for(let bar = 0; bar < barsToShow; bar++) {
          const barLabel = document.createElement('div');
          barLabel.textContent = 'Bar ' + (bar + 1);
          barLabel.style.position = 'absolute';
          barLabel.style.left = (bar * beatsPerBar * beatWidth + 5) + 'px';
          barLabel.style.top = (lanesCount * laneHeight) + 'px';
          barLabel.style.color = '#888';
          barLabel.style.fontSize = '12px';
          timeline.appendChild(barLabel);
        }
    
        // Create lanes container (transparent div for clicks)
        for(let lane=0; lane < lanesCount; lane++) {
          const laneDiv = document.createElement('div');
          laneDiv.classList.add('lane');
          laneDiv.style.position = 'absolute';
          laneDiv.style.top = (lane * laneHeight) + 'px';
          laneDiv.style.left = '0px';
          laneDiv.style.width = totalBeats * beatWidth + 'px';
          laneDiv.style.height = laneHeight + 'px';
          laneDiv.style.zIndex = '5';
          timeline.appendChild(laneDiv);
    
          // Click handler on lane to add note at click position time
          laneDiv.addEventListener('click', e => {
            const rect = laneDiv.getBoundingClientRect();
            const x = e.clientX - rect.left;
            const beatPos = x / beatWidth;
            const timePosition = beatPos * (60/chart.bpm); // seconds
    
            addNote(timePosition, lane);
          });
        }
    
        // Add notes elements on timeline
        renderNotes();
    
        // Playhead line
        if (!document.getElementById('playhead')) {
          const playhead = document.createElement('div');
          playhead.id = 'playhead';
          playhead.style.position = 'absolute';
          playhead.style.top = '0';
          playhead.style.bottom = '0';
          playhead.style.width = '2px';
          playhead.style.backgroundColor = '#fff';
          playhead.style.zIndex = '10';
          timeline.appendChild(playhead);
        }
      }
    
      // Render notes on timeline
      function renderNotes() {
        // Remove previous notes
        document.querySelectorAll('#timeline .note').forEach(n => n.remove());
    
        const beatWidth = 60;
        const laneHeight = 40;
        chart.notes.forEach(note => {
          const noteDiv = document.createElement('div');
          noteDiv.classList.add('note', 'lane' + note.lane);
    
          // position horizontally = time / beat length * beatWidth
          // beatLength (seconds) = 60 / bpm
          const beatLength = 60 / chart.bpm;
          const x = (note.time / beatLength) * beatWidth;
          noteDiv.style.position = 'absolute';
          noteDiv.style.left = (x - 10) + 'px'; // center 20px width
          noteDiv.style.top = (note.lane * laneHeight + 5) + 'px'; // top padding 5px
          noteDiv.style.width = '20px';
          noteDiv.style.height = '30px';
          noteDiv.style.borderRadius = '4px';
          noteDiv.style.backgroundColor = ['#e74c3c', '#3498db', '#f1c40f', '#2ecc71'][note.lane] || '#999';
          noteDiv.style.opacity = '0.9';
    
          timeline.appendChild(noteDiv);
        });
      }
    
      // Add note helper
      function addNote(time, lane) {
        // Clamp time to audio duration if loaded
        if(audio.duration && time > audio.duration) time = audio.duration;
        chart.notes.push({time, lane});
        // Sort notes by time
        chart.notes.sort((a,b) => a.time - b.time);
        renderNotes();
        saveChartToUrl();
      }
    
      // Playback controls
      playBtn.addEventListener('click', () => {
        if (!audio.src) return;
        audio.play();
        isPlaying = true;
      });
      pauseBtn.addEventListener('click', () => {
        audio.pause();
        isPlaying = false;
      });
      stopBtn.addEventListener('click', () => {
        audio.pause();
        audio.currentTime = 0;
        isPlaying = false;
      });
    
      // Load MP3 and BPM into player and chart
      loadBtn.addEventListener('click', () => {
        const url = mp3urlInput.value.trim();
        let bpmVal = Number(bpmInput.value);
        if(!url) {
          alert('Please enter a valid MP3 URL');
          return;
        }
        if(isNaN(bpmVal) || bpmVal < 30 || bpmVal > 300) {
          alert('BPM must be number between 30 and 300');
          return;
        }
    
        audio.src = url;
        chart.mp3url = url;
        chart.bpm = bpmVal;
        chart.notes = [];
    
        playBtn.disabled = false;
        pauseBtn.disabled = false;
        stopBtn.disabled = false;
        isPlaying = false;
    
        redrawTimeline();
        updateCombo();
    
        saveChartToUrl();
      });
    
      // Update playhead line position based on current time
      function updatePlayhead() {
        const playhead = document.getElementById('playhead');
        if(!playhead) return;
        const beatLength = 60/chart.bpm;
        const beatWidth = 60;
        const x = (audio.currentTime / beatLength) * beatWidth;
        playhead.style.left = x + 'px';
    
        updateCombo();
      }
    
      // Update combo count: count how many notes have been passed consecutively
      // For simplicity count all notes with time <= current time
      function updateCombo() {
        const currentTime = audio.currentTime;
        let combo = 0;
        for(const note of chart.notes) {
          if(note.time <= currentTime) combo++;
          else break;
        }
        comboLabel.textContent = 'Combo: ' + combo;
      }
    
      audio.addEventListener('timeupdate', () => {
        updatePlayhead();
      });
    
      audio.addEventListener('ended', () => {
        isPlaying = false;
      });
    
      // Keyboard shortcuts: keys 1-4 add note at current time on lane 0-3
      window.addEventListener('keydown', e => {
        if(!audio.src) return;
    
        if(e.key >= '1' && e.key <= '4') {
          const lane = Number(e.key) - 1;
          addNote(audio.currentTime, lane);
          e.preventDefault();
        }
      });
    
      // Save chart to URL hash as JSON string - compressed could be done in future
      function saveChartToUrl() {
        try {
          // We'll store minimal data: bpm, mp3url, notes (time rounded 3 decimals)
          const data = {
            bpm: chart.bpm,
            mp3url: chart.mp3url,
            notes: chart.notes.map(n => ({time: +n.time.toFixed(3), lane: n.lane}))
          };
          const json = JSON.stringify(data);
          // Encode to URI safe string (encodeURIComponent)
          location.hash = '#/' + encodeURIComponent(json);
        } catch(e) {
          console.warn('Failed to save chart to URL', e);
        }
      }
      // Load chart from URL
      function loadChartFromUrl() {
        if(location.hash.startsWith('#/')) {
          try {
            const json = decodeURIComponent(location.hash.slice(2));
            const data = JSON.parse(json);
            if(data.mp3url && data.bpm && Array.isArray(data.notes)) {
              chart.mp3url = data.mp3url;
              chart.bpm = data.bpm;
              chart.notes = data.notes;
    
              mp3urlInput.value = chart.mp3url;
              bpmInput.value = chart.bpm;
    
              audio.src = chart.mp3url;
              playBtn.disabled = false;
              pauseBtn.disabled = false;
              stopBtn.disabled = false;
              isPlaying = false;
    
              redrawTimeline();
              renderNotes();
              updateCombo();
            }
          } catch(e) {
            console.warn('Failed to load chart from URL hash', e);
          }
        }
      }
    
      // Auto load on page ready
      loadChartFromUrl();
    
      // On window resize, redraw (optional)
      window.addEventListener('resize', () => {
        if(chart.mp3url) redrawTimeline();
      });
    })();
    </script>
    
    </body>
    </html>
    

    How to use

    1. Paste a direct MP3 URL (must be CORS accessible or same origin) in the MP3 URL input.
    2. Set BPM (default 120).
    3. Click Load — timeline generated and audio loaded.
    4. Press Play to play the audio.
    5. You can click on one of the four horizontal lanes at any point to add a note at that time.
    6. Or press keys 1, 2, 3, or 4 to add a note in lanes 0-3 at the current play time.
    7. The timeline is scrollable horizontally if you want to see 16 bars (64 beats).
    8. The current combo shows how many notes have been reached.
    9. The URL hash updates automatically so you can share your chart by copying the URL.
    10. Open same URL elsewhere to load the chart and continue editing.

    Limitations / Next steps

    • No note deletion or moving yet.
    • No snapping/precision beyond clicks and key presses.
    • No preview of note timing or hit detection.
    • URL stored data can become large; compression/filtering can be added.
    • More complex UI improvements possible (zoom, scroll syncing with audio).
    • Visual feedback on currently pressed keys.

    If you want me to help enhance this editor with more advanced features or a UI framework, or convert to React/Vue, just ask!