Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix touches being empty in touchEnded() by using changedTouches#8601

Open
suhr25 wants to merge 1 commit intoprocessing:mainfrom
suhr25:fix/touches-empty-in-touchended
Open

Fix touches being empty in touchEnded() by using changedTouches#8601
suhr25 wants to merge 1 commit intoprocessing:mainfrom
suhr25:fix/touches-empty-in-touchended

Conversation

@suhr25
Copy link

@suhr25 suhr25 commented Mar 2, 2026

SUMMARY

Fixes touchEnded() getting an empty touches[].
Now uses changedTouches when touches is empty (per spec).
Changes in src/events/touch.js + a small unit test.


FIX

  • Issue: e.touches is empty on touchend, so loop never runs
  • Fix: fallback to e.changedTouches

Before

for (let i = 0; i < e.touches.length; i++) {

After

const touchList = e.touches.length ? e.touches : e.changedTouches;
for (let i = 0; i < touchList.length; i++) {

VERIFICATION

  • Run tests → should pass
  • Manual test:
function touchEnded() {
  console.log(touches.length);
}

Before: 0
After: 1 (correct final touch)

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
@suhr25
Copy link
Author

suhr25 commented Mar 2, 2026

Hi @davepagurek @ksen0,
Can you please take a look at this.
Thanks!

@ksen0
Copy link
Member

ksen0 commented Mar 3, 2026

@suhr25 is there a bug report or a sketch that shows the problem behavior that this is fixing? Thanks!

@suhr25
Copy link
Author

suhr25 commented Mar 4, 2026

Hi @ksen0,
Yes ,here is a minimal sketch that reproduces the issue:

function setup() {
  createCanvas(200, 200);
}

function touchEnded() {
  console.log(touches.length);
}

On mobile browsers , lifting the last finger logs 0, even though a touch just ended. This happens because on touchend, event.touches is often empty, while the ended touch is only present in event.changedTouches.
This PR falls back to changedTouches when touches is empty so touchEnded() receives the final touch correctly.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants