Skip to content

Commit

Permalink
✨ Web: fade things out on note off events
Browse files Browse the repository at this point in the history
  • Loading branch information
ewen-lbh committed May 2, 2024
1 parent 0f85abe commit 94d8fc2
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@
async function run() {
await init()
window.renderImage = (vel, col) => {
console.log([...arguments])
document.querySelector(`.frame[data-color=${color_name(col)}]`)?.remove()
document
.querySelectorAll(`.frame[data-color=${color_name(col)}]`)
?.forEach((el) => el.remove())
render_image(vel, col)
}
}
run()

function fadeOutElement(el) {
if (!el) return
const duration = window.pedal_held ? 5e3 : 200
el.style.transition = `opacity ${duration}ms ease-out`
el.style.opacity = 0
setTimeout(() => el.remove(), duration)
}

function frameElement(color) {
return document.querySelector(`.frame[data-color=${color_name(color)}]`)
}

window.pedal_held = false

window.addEventListener("keypress", (e) => {
Expand All @@ -35,16 +48,15 @@
console.log(cmd, args)
if (cmd === 176 && args[0] === 64) {
const [_, intensity] = args
window.pedal_held = intensity > 0
if (intensity === 0) {
document
.querySelectorAll(".frame")
?.forEach((frame) => frame.remove())
document.querySelectorAll(".frame")?.forEach(fadeOutElement)
}
return
}
if (cmd !== 144) return
const [pitch, velocity] = args
if (velocity === 0) return

const colors = [
Color.Blue,
Color.Purple,
Expand All @@ -54,14 +66,19 @@
Color.Yellow,
Color.Green,
Color.Cyan,
Color.White,
]
// get color uniformly in this range from 28 (lowest pitch) to 103 (highest pitch)

const color =
colors[Math.floor(((pitch - 28) / (103 - 28)) * colors.length)]
console.log(pitch, color)
window.renderImage(velocity / 128, color ?? "white")
// get octave from pitch
const octave = Math.floor(pitch / 12) - 1

// if octave is 0, use the first color
const color = colors[octave] ?? colors[0]

if (velocity === 0) {
fadeOutElement(frameElement(color))
} else {
window.renderImage(velocity / 128, color ?? "white")
}
}
})
})
Expand Down

0 comments on commit 94d8fc2

Please sign in to comment.