From 67f210e4996470f92b3876c9c14c26903fcd41fb Mon Sep 17 00:00:00 2001 From: Victor De la Rocha Date: Tue, 27 May 2025 15:54:51 -0600 Subject: [PATCH] Adds an staff (in progress) --- index.html | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ script.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ styles.css | 46 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 164 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index 793f9ce..4ed6c00 100644 --- a/index.html +++ b/index.html @@ -85,6 +85,16 @@

+
+ +
+
+
+
+
+
+ + + + diff --git a/script.js b/script.js index 7c1ef21..ebf3e15 100644 --- a/script.js +++ b/script.js @@ -155,6 +155,7 @@ function playNote(frequency, message) { function playChord(notes, message) { const now = audioCtx.currentTime; + showNotesOnStaff(notes); notes.forEach(note => { const freq = typeof note === 'string' ? noteToFrequency(note) : note; const oscillator = audioCtx.createOscillator(); @@ -181,6 +182,7 @@ function playChord(notes, message) { oscillator.start(now); oscillator.stop(now + duration); }); + // setTimeout(clearStaff, 1000); document.getElementById('poetryBox').textContent = message || '—'; } @@ -415,4 +417,60 @@ function highlightPianoKey(note, duration = 1000) { setTimeout(() => { document.querySelectorAll('.white-key, .black-key').forEach(key => key.classList.remove('active-key')); }, duration); +} + +function showNotesOnStaff(notes) { + const staff = document.querySelector('.staff-large'); + staff.querySelectorAll('.notehead').forEach(n => n.remove()); + +const noteSteps = { + 'E4': 0, // línea 5 (inferior) + 'F4': 1, // espacio + 'G4': 2, // línea 4 + 'A4': 3, // espacio + 'B4': 4, // línea 3 + 'C5': 5, // espacio + 'D5': 6, // línea 2 + 'E5': 7, // espacio + 'F5': 8 // línea 1 (superior) + // agrega más si quieres +}; + const baseLineTop = 208; // top de la línea 1 (superior) + const lineSpacing = 42; // px entre líneas + + const baseLeft = 60; + + notes.forEach(note => { + const step = noteSteps[note]; + if (step !== undefined) { + const y = baseLineTop - (step * (lineSpacing / 2)); + const noteDiv = document.createElement('div'); + noteDiv.className = 'notehead'; + noteDiv.style.position = 'absolute'; + noteDiv.style.top = `${y}px`; + noteDiv.style.left = `${baseLeft}px`; + noteDiv.style.width = '24px'; + noteDiv.style.height = '16px'; + noteDiv.style.background = 'white'; + noteDiv.style.border = '2px solid #222'; + noteDiv.style.borderRadius = '50%'; + noteDiv.style.transform = 'rotate(-20deg)'; + noteDiv.style.zIndex = 10; + + if (note.includes('#')) { + const sharp = document.createElement('span'); + sharp.className = 'sharp'; + sharp.textContent = '♯'; + sharp.style.position = 'absolute'; + sharp.style.left = '-14px'; + sharp.style.top = '2px'; + sharp.style.fontSize = '18px'; + sharp.style.fontWeight = 'bold'; + sharp.style.color = 'black'; + sharp.style.fontFamily = 'serif'; + noteDiv.appendChild(sharp); + } + staff.appendChild(noteDiv); + } + }); } \ No newline at end of file diff --git a/styles.css b/styles.css index 0601c55..3b8a8f0 100644 --- a/styles.css +++ b/styles.css @@ -98,4 +98,48 @@ hr { .active-key { background: #ffe082 !important; box-shadow: 0 0 10px #ffd54f; - } \ No newline at end of file + } + + + +.staff-large { + position: relative; + height: 300px; + margin: 2em auto; + width: 320px; + background: transparent; +} + +.staff-large .line { + position: absolute; + left: 0; + width: 100%; + height: 2px; + background: #fff; +} +.staff-large .line:nth-child(1) { top: 40px; } +.staff-large .line:nth-child(2) { top: 82px; } +.staff-large .line:nth-child(3) { top: 124px; } +.staff-large .line:nth-child(4) { top: 166px; } +.staff-large .line:nth-child(5) { top: 208px; } + +.sharp { + position: absolute; + left: -14px; + top: 2px; + font-size: 18px; + font-weight: bold; + color: red; + font-family: serif; +} + +.notehead { + position: absolute; + width: 24px; + height: 16px; + background: white; + border: 2px solid #222; + border-radius: 50%; + transform: rotate(-20deg); + z-index: 10; +} \ No newline at end of file