fix: correct click percentage and listener removal#13
Open
chrisweb wants to merge 1 commit into
Open
Conversation
Two bugs in the waveform click handling: 1. Click position used canvas.width (the internal pixel resolution set in draw(), e.g. 599px for 200 peaks) as the divisor while measuring the click offset in CSS pixels via getBoundingClientRect(). When the canvas is scaled with CSS (e.g. width: 100%) the two units differ, so the reported percent is wrong and the error grows toward the right edge. Now divides by the rendered width from getBoundingClientRect(). 2. _removeClickWaveListener() called .bind(this) again, producing a new function reference, so removeEventListener never matched the listener added in _addClickWaveListener() and the listener leaked on destroy(). The bound handler is now stored and reused for add and remove. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes two bugs in the waveform canvas click handling (
src/library/core.ts).1. Click percentage is wrong on a CSS-scaled canvas
_canvasElementClickmeasured the click offset in CSS pixels (getBoundingClientRect()) but divided bycanvas.width, which is the internal pixel resolution set indraw()(e.g.200 * 2 + 199 * 1 = 599pxfor 200 peaks).When the canvas is scaled with CSS (e.g.
width: 100%), the rendered width andcanvas.widthdiffer, so the reported percentage is off and the error grows toward the right edge — a click at the far right of a 300px-wide canvas reports300 / (599/100) ≈ 50%instead of 100%.The fix divides by the rendered width from
getBoundingClientRect().width, which matches the README's stated behaviour (position relative to the rendered canvas width) and is correct regardless of internal resolution / devicePixelRatio. Guards against a zero rendered width.2. Click listener is never removed on
destroy()_removeClickWaveListener()calledthis._canvasElementClick.bind(this)again — a new function reference — soremoveEventListenernever matched the listener added by_addClickWaveListener(). The listener leaked, and consumers that re-create aWaveformon the same canvas accumulated handlers.The bound handler is now stored once (
_boundCanvasElementClick) and reused for both add and remove.Testing
npm run lintpassestsc --noEmitonsrc/index.ts(strict) passes(
npm run buildfails locally on a missingtslib, unrelated to this change.)