chrisbateman / impetus

Agnostic utility for adding momentum to anything. It's like iScroll, except not for scrolling. Supports mouse and touch events.
chrisbateman.github.io/impetus
MIT License
487 stars 70 forks source link

Fix multi-touch (additional touches causing jumps) #44

Open fortinmike opened 5 years ago

fortinmike commented 5 years ago

This is the same fix as #33, but this new pull request contains only the relevant changes (I forgot to create a branch before creating my original pull request).


Considering ev.targetTouches[0] before ev.changedTouches[0] in normalizeEvent() meant that having multiple target touches where the first touch wasn't the same as the one stored by Impetus as the "pointer" would prevent the condition in onUp() from triggering, leaving the internal state in "started" mode (stopTracking() not called) event after all touches had been released. In essence, onUp() was called twice (once for each released touch) but in both cases it tested ev.targetTouches[0] which didn't match the stored "pointer" touch, which was at index 1, causing stopTracking() never to be called.

Unless I'm missing something, as the various event handlers already filter out "unwanted" touches (by storing the first touch's id and checking against that), event normalization can safely consider changed touches only (the touch concerned by the current event), which fixes at least this edge case and makes tracking more reliable when multiple touches are present.

Before (video) After (video)