fix: mobile touch differentiates drag and tap
This commit is contained in:
parent
60c598c17d
commit
056daa6460
1 changed files with 38 additions and 14 deletions
|
|
@ -989,6 +989,11 @@ export function initWebGLBackground() {
|
|||
pointer.active = true;
|
||||
};
|
||||
|
||||
const TOUCH_DRAG_THRESHOLD = 8;
|
||||
let touchDragActive = false;
|
||||
let touchStartX = 0;
|
||||
let touchStartY = 0;
|
||||
|
||||
const renderEmitters = () => {
|
||||
if (!emitterCtx || !emitterTarget) {
|
||||
return;
|
||||
|
|
@ -1449,31 +1454,50 @@ export function initWebGLBackground() {
|
|||
pointer.active = false;
|
||||
});
|
||||
|
||||
document.addEventListener(
|
||||
"touchstart",
|
||||
(event) => {
|
||||
event.preventDefault();
|
||||
document.addEventListener("touchstart", (event) => {
|
||||
const touch = event.touches.item(0);
|
||||
if (touch) {
|
||||
touchDragActive = false;
|
||||
touchStartX = touch.clientX;
|
||||
touchStartY = touch.clientY;
|
||||
updatePointer(touch.clientX, touch.clientY);
|
||||
}
|
||||
},
|
||||
{ passive: false },
|
||||
);
|
||||
});
|
||||
|
||||
document.addEventListener(
|
||||
"touchmove",
|
||||
(event) => {
|
||||
event.preventDefault();
|
||||
const touch = event.touches.item(0);
|
||||
if (touch) {
|
||||
updatePointer(touch.clientX, touch.clientY);
|
||||
if (!touch) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!touchDragActive) {
|
||||
const distance = Math.hypot(
|
||||
touch.clientX - touchStartX,
|
||||
touch.clientY - touchStartY,
|
||||
);
|
||||
|
||||
if (distance < TOUCH_DRAG_THRESHOLD) {
|
||||
return;
|
||||
}
|
||||
|
||||
touchDragActive = true;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
updatePointer(touch.clientX, touch.clientY);
|
||||
},
|
||||
{ passive: false },
|
||||
);
|
||||
|
||||
document.addEventListener("touchend", () => {
|
||||
touchDragActive = false;
|
||||
pointer.active = false;
|
||||
});
|
||||
|
||||
document.addEventListener("touchcancel", () => {
|
||||
touchDragActive = false;
|
||||
pointer.active = false;
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue