Skip to content

Commit

Permalink
fix release piano keys when mouse cursor leaves them while pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
TeemuKoivisto committed Apr 7, 2024
1 parent 6b4ea76 commit 4d1ace1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const C0_MIDI = 12
const C7_MIDI = 96
let innerWidth = window.innerWidth
let innerWidth = typeof window !== 'undefined' ? window.innerWidth : 500
$: pianoWidth = Math.min(innerWidth, 780)
const bottomNoteMidi = writable(findClosestC($midiRange[0]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
isLast: boolean,
pianoWidth: number
type Mouse = MouseEvent & { currentTarget: EventTarget & HTMLButtonElement }
type Touch = TouchEvent & { currentTarget: EventTarget & HTMLButtonElement }
let keyHeld = false
let timeout: ReturnType<typeof setTimeout> | undefined
Expand All @@ -28,13 +31,13 @@
$: width = isWhite ? whiteWidth : blackWidth
$: zIndex = isWhite ? 0 : 1
function handlePointerDown(e: any) {
function handlePointerDown(_e: Mouse | Touch) {
if (!keyHeld && !timeout) {
dispatch('pressed', { idx: key.idx, row })
keyHeld = true
}
}
function handlePointerUp(e: any) {
function handlePointerUp(_e: Mouse | Touch) {
keyHeld = false
timeout = setTimeout(() => {
timeout = undefined
Expand All @@ -55,6 +58,7 @@
class:is-held={keyHeld}
on:mousedown={handlePointerDown}
on:mouseup={handlePointerUp}
on:mouseleave={handlePointerUp}
on:touchstart|passive={handlePointerDown}
on:touchend|passive={handlePointerUp}
>
Expand Down

0 comments on commit 4d1ace1

Please sign in to comment.