<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Guitar Tablature Editor</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#sheet {
width: 700px;
border: 1px solid #ccc;
margin-bottom: 20px;
position: relative;
}
.line {
display: flex;
border-bottom: 1px solid #eee;
min-height: 100px;
}
.bar {
display: flex;
border-right: 1px solid #ddd;
position: relative;
}
.subbar {
border-right: 1px dashed #eee;
position: relative;
padding: 5px;
}
.duration-label {
position: absolute;
bottom: 0;
left: 5px;
font-size: 10px;
color: #666;
}
.note {
position: absolute;
width: 20px;
height: 20px;
background-color: #f0f0f0;
border: 1px solid #ccc;
border-radius: 3px;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
cursor: move;
}
.controls {
margin-bottom: 20px;
}
button {
padding: 5px 10px;
margin-right: 10px;
}
</style>
</head>
<body>
<div class="controls">
<button id="addBar">Add Bar</button>
<button id="addSubbar">Add Subbar</button>
<select id="duration">
<option value="w">Whole (w)</option>
<option value="h">Half (h)</option>
<option value="q">Quarter (q)</option>
<option value="e">Eighth (e)</option>
<option value="s">Sixteenth (s)</option>
</select>
</div>
<div id="sheet"></div>
<script>
// Constants
const defaultDurations = {
'w': { value: 1.0, visual: 75 },
'h': { value: 0.5, visual: 50 },
'q': { value: 0.25, visual: 33 },
'e': { value: 0.125, visual: 20 },
's': { value: 0.0625, visual: 13 }
};
const sheetWidth = 700;
const strings = 6; // Standard guitar strings
// Data structures
let track = {
instrument: 0, // 0 = guitar
bars: []
};
// UI elements
const sheet = document.getElementById('sheet');
const addBarBtn = document.getElementById('addBar');
const addSubbarBtn = document.getElementById('addSubbar');
const durationSelect = document.getElementById('duration');
// Initialize with one bar and one subbar
addBar();
drawTrack();
// Event listeners
addBarBtn.addEventListener('click', () => {
addBar();
drawTrack();
});
addSubbarBtn.addEventListener('click', () => {
if (track.bars.length === 0) {
addBar();
}
const lastBar = track.bars[track.bars.length - 1];
addSubbar(lastBar);
drawTrack();
});
// Functions
function addBar() {
const newBar = {
time_signature: [4, 4], // Default 4/4 time
subbars: []
};
track.bars.push(newBar);
addSubbar(newBar); // Add initial subbar
}
function addSubbar(bar) {
const duration = durationSelect.value;
const newSubbar = {
dur: duration,
pixeldur: defaultDurations[duration].visual,
notes: Array(strings).fill(-1) // -1 means no note
};
bar.subbars.push(newSubbar);
}
function drawTrack() {
// Clear the sheet
sheet.innerHTML = '';
// Calculate how many bars fit per line
let lines = [];
let currentLine = { bars: [], totalWidth: 0 };
let currentBarIndex = 0;
for (const bar of track.bars) {
// Calculate total width of subbars in this bar
const barWidth = bar.subbars.reduce((sum, subbar) => sum + subbar.pixeldur, 0);
// Check if this bar fits in the current line
if (currentLine.totalWidth + barWidth > sheetWidth) {
// Doesn't fit, start new line
lines.push(currentLine);
currentLine = { bars: [], totalWidth: 0 };
}
// Add bar to current line
currentLine.bars.push({
index: currentBarIndex,
width: barWidth
});
currentLine.totalWidth += barWidth;
currentBarIndex++;
}
// Add the last line if it has content
if (currentLine.bars.length > 0) {
lines.push(currentLine);
}
// Now draw the lines and bars
for (const line of lines) {
const lineDiv = document.createElement('div');
lineDiv.className = 'line';
for (const barInfo of line.bars) {
const bar = track.bars[barInfo.index];
const barDiv = document.createElement('div');
barDiv.className = 'bar';
barDiv.style.width = `${barInfo.width}px`;
for (const subbar of bar.subbars) {
const subbarDiv = document.createElement('div');
subbarDiv.className = 'subbar';
subbarDiv.style.width = `${subbar.pixeldur}px`;
// Add duration label
const durationLabel = document.createElement('div');
durationLabel.className = 'duration-label';
durationLabel.textContent = subbar.dur;
subbarDiv.appendChild(durationLabel);
// Add notes
for (let i = 0; i < subbar.notes.length; i++) {
if (subbar.notes[i] !== -1) {
const noteDiv = document.createElement('div');
noteDiv.className = 'note';
noteDiv.textContent = subbar.notes[i];
noteDiv.style.top = `${i * 20}px`;
// Store position info for dragging
noteDiv.dataset.stringIndex = i;
noteDiv.dataset.subbarIndex = bar.subbars.indexOf(subbar);
noteDiv.dataset.barIndex = barInfo.index;
// Make draggable
makeDraggable(noteDiv);
subbarDiv.appendChild(noteDiv);
}
}
// Make subbar clickable to add notes
subbarDiv.addEventListener('click', (e) => {
if (e.target === subbarDiv) {
// Find which string was clicked
const rect = subbarDiv.getBoundingClientRect();
const y = e.clientY - rect.top;
const stringIndex = Math.floor(y / 20);
// Add a note (default to fret 0)
subbar.notes[stringIndex] = 0;
drawTrack();
}
});
barDiv.appendChild(subbarDiv);
}
lineDiv.appendChild(barDiv);
}
sheet.appendChild(lineDiv);
}
}
function makeDraggable(element) {
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
element.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// Get the mouse cursor position at startup
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// Calculate the new cursor position
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// Set the element's new position
element.style.top = (element.offsetTop - pos2) + "px";
element.style.left = (element.offsetLeft - pos1) + "px";
}
function closeDragElement() {
// Stop moving when mouse button is released
document.onmouseup = null;
document.onmousemove = null;
// Update the note position in the data structure
const stringIndex = parseInt(element.dataset.stringIndex);
const subbarIndex = parseInt(element.dataset.subbarIndex);
const barIndex = parseInt(element.dataset