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,44 +228,28 @@ 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) {
ws.send("b" + POINTER_BUTTON_LEFT + ";0"); dragging = false;
} ws.send("b" + POINTER_BUTTON_LEFT + ";0");
if (ongoingTouches.length == 0 && touchReleasedCount >= 1 &&
!touchMoved && evt.timeStamp - touchStart < TOUCH_TIMEOUT) {
let button = 0;
if (touchReleasedCount == 1) {
button = POINTER_BUTTON_LEFT;
} else if (touchReleasedCount == 2) {
button = POINTER_BUTTON_RIGHT;
} else if (touchReleasedCount == 3) {
button = POINTER_BUTTON_MIDDLE;
} }
ws.send("b" + button + ";1"); if (!touchMoved && evt.timeStamp - touchStart < TOUCH_TIMEOUT) {
if (button == POINTER_BUTTON_LEFT) { let button = 0;
draggingTimeout = setTimeout(onDraggingTimeout, TOUCH_TIMEOUT); if (touchReleasedCount == 1) {
} else { button = POINTER_BUTTON_LEFT;
ws.send("b" + button + ";0"); } else if (touchReleasedCount == 2) {
} button = POINTER_BUTTON_RIGHT;
} } else if (touchReleasedCount == 3) {
} button = POINTER_BUTTON_MIDDLE;
}
function handleCancel(evt) { ws.send("b" + button + ";1");
let touches = evt.changedTouches; if (button == POINTER_BUTTON_LEFT) {
for (let i = 0; i < touches.length; i++) { draggingTimeout = setTimeout(onDraggingTimeout, TOUCH_TIMEOUT);
let idx = ongoingTouchIndexById(touches[i].identifier); } else {
if (idx < 0) { ws.send("b" + button + ";0");
continue; }
}
ongoingTouches.splice(idx, 1);
touchReleasedCount++;
touchLastEnd = evt.timeStamp;
touchMoved = true;
if (scrolling) {
ws.send("sf");
scrolling = false;
} }
touchReleasedCount = 0;
} }
} }
@ -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);
}); });