Handle quirks of browser touch events

This commit is contained in:
Unrud 2019-10-28 01:50:10 +01:00
parent 18b8d15af0
commit 1e46a174aa
2 changed files with 33 additions and 43 deletions

File diff suppressed because one or more lines are too long

View file

@ -172,19 +172,25 @@ function updateMoveAndScroll() {
} }
function handleStart(evt) { function handleStart(evt) {
// Might get called multiple times for the same touches
if (ongoingTouches.length == 0) { if (ongoingTouches.length == 0) {
touchStart = evt.timeStamp; touchStart = evt.timeStamp;
touchMoved = false; touchMoved = false;
touchReleasedCount = 0;
dragging = false;
} }
let touches = evt.changedTouches; let touches = evt.changedTouches;
for (let i = 0; i < touches.length; i++) { for (let i = 0; i < touches.length; i++) {
if (touches[i].target !== pad && touches[i].target !== padlabel) { if (touches[i].target !== pad && touches[i].target !== padlabel &&
ongoingTouches.length == 0) {
continue; continue;
} }
evt.preventDefault(); evt.preventDefault();
ongoingTouches.push(copyTouch(touches[i], evt.timeStamp)); let touch = copyTouch(touches[i], evt.timeStamp);
let idx = ongoingTouchIndexById(touch.identifier);
if (idx < 0) {
ongoingTouches.push(touch);
} else {
ongoingTouches[idx] = touch;
}
touchLastEnd = 0; touchLastEnd = 0;
if (!dragging) { if (!dragging) {
moveXSum = Math.trunc(moveXSum); moveXSum = Math.trunc(moveXSum);
@ -222,12 +228,12 @@ function handleEnd(evt) {
if (touchReleasedCount > TOUCH_MOVE_THRESHOLD.length) { if (touchReleasedCount > TOUCH_MOVE_THRESHOLD.length) {
touchMoved = true; touchMoved = true;
} }
if (ongoingTouches.length == 0 && touchReleasedCount >= 1 && if (ongoingTouches.length == 0 && touchReleasedCount >= 1) {
dragging) { if (dragging) {
dragging = false;
ws.send("b" + POINTER_BUTTON_LEFT + ";0"); ws.send("b" + POINTER_BUTTON_LEFT + ";0");
} }
if (ongoingTouches.length == 0 && touchReleasedCount >= 1 && if (!touchMoved && evt.timeStamp - touchStart < TOUCH_TIMEOUT) {
!touchMoved && evt.timeStamp - touchStart < TOUCH_TIMEOUT) {
let button = 0; let button = 0;
if (touchReleasedCount == 1) { if (touchReleasedCount == 1) {
button = POINTER_BUTTON_LEFT; button = POINTER_BUTTON_LEFT;
@ -243,23 +249,7 @@ function handleEnd(evt) {
ws.send("b" + button + ";0"); ws.send("b" + button + ";0");
} }
} }
} touchReleasedCount = 0;
function handleCancel(evt) {
let touches = evt.changedTouches;
for (let i = 0; i < touches.length; i++) {
let idx = ongoingTouchIndexById(touches[i].identifier);
if (idx < 0) {
continue;
}
ongoingTouches.splice(idx, 1);
touchReleasedCount++;
touchLastEnd = evt.timeStamp;
touchMoved = true;
if (scrolling) {
ws.send("sf");
scrolling = false;
}
} }
} }
@ -425,6 +415,6 @@ window.addEventListener("load", function() {
}); });
pad.addEventListener("touchstart", handleStart); pad.addEventListener("touchstart", handleStart);
pad.addEventListener("touchend", handleEnd); pad.addEventListener("touchend", handleEnd);
pad.addEventListener("touchcancel", handleCancel); pad.addEventListener("touchcancel", handleEnd);
pad.addEventListener("touchmove", handleMove); pad.addEventListener("touchmove", handleMove);
}); });