Fix touches being empty in touchEnded() by using changedTouches#8601
Fix touches being empty in touchEnded() by using changedTouches#8601suhr25 wants to merge 1 commit into
Conversation
During a touchend event the W3C Touch Events spec moves the lifted finger out of e.touches (active contacts only) and into e.changedTouches. _updateTouchCoords iterated over e.touches.length, which is 0 when the last finger lifts, so touches[] was always empty inside touchEnded(). Fall back to e.changedTouches when e.touches is empty so that the final touch position is accessible to user code in touchEnded(). getTouchInfo already contained the e.touches[i] || e.changedTouches[i] fallback for individual lookups; this change fixes the loop bound that prevented it from being reached. Fixes: touches[] always being [] inside touchEnded() on single-touch
|
Hi @davepagurek @ksen0, |
|
@suhr25 is there a bug report or a sketch that shows the problem behavior that this is fixing? Thanks! |
|
Hi @ksen0, function setup() {
createCanvas(200, 200);
}
function touchEnded() {
console.log(touches.length);
}On mobile browsers , lifting the last finger logs Thanks! |
perminder-17
left a comment
There was a problem hiding this comment.
| Event | e.touches | e.changedTouches | Before (touches.length) | After this PR (touches.length) |
|---|---|---|---|---|
| 1st finger lifted | [F2, F3] | [F1] | 2 | 2 |
| 2nd finger lifted | [F3] | [F2] | 1 | 1 |
| 3rd (last) finger lifted | [] | [F3] | 0 | 1 (Since you added a fallback) |
For me, this is not a bug but an API design question, where you could probably open up an issue and stewards or other contributor could discuss on the behaviour.
So, hence closing this PR, and you could probably open an issue for that and if the issue got approved, we can then go ahead with this PR. Thanks for your work and patience.
SUMMARY
Fixes
touchEnded()getting an emptytouches[].Now uses
changedToucheswhentouchesis empty (per spec).Changes in
src/events/touch.js+ a small unit test.FIX
e.touchesis empty ontouchend, so loop never runse.changedTouchesBefore
After
VERIFICATION
Before: 0
After: 1 (correct final touch)