Skip to content

Commit

Permalink
fix touch event when target - is not an element (#6881)
Browse files Browse the repository at this point in the history
* fix touch event when target - is not an element

* PR comments

* linter
# Conflicts:
#	src/framework/input/element-input.js
  • Loading branch information
Maksims authored and Martin Valigursky committed Aug 21, 2024
1 parent b15e751 commit 591831c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
29 changes: 4 additions & 25 deletions src/framework/input/element-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Vec3 } from '../../core/math/vec3.js';
import { Vec4 } from '../../core/math/vec4.js';
import { Ray } from '../../core/shape/ray.js';
import { Mouse } from '../../platform/input/mouse.js';
import { getTouchTargetCoords } from '../../platform/input/touch-event.js';
import { getApplication } from '../globals.js';

/**
Expand Down Expand Up @@ -582,7 +583,7 @@ class ElementInput {
continue;
}

const coords = this._calcTouchCoords(event.changedTouches[j]);
const coords = getTouchTargetCoords(event.changedTouches[j]);

const element = this._getTargetElementByCoords(camera, coords.x, coords.y);
if (element) {
Expand Down Expand Up @@ -655,7 +656,7 @@ class ElementInput {

// check if touch was released over previously touch
// element in order to fire click event
const coords = this._calcTouchCoords(touch);
const coords = getTouchTargetCoords(touch);

for (let c = cameras.length - 1; c >= 0; c--) {
const hovered = this._getTargetElementByCoords(cameras[c], coords.x, coords.y);
Expand Down Expand Up @@ -688,7 +689,7 @@ class ElementInput {
const oldTouchInfo = this._touchedElements[touch.identifier];

if (oldTouchInfo) {
const coords = this._calcTouchCoords(touch);
const coords = getTouchTargetCoords(touch);

// Fire touchleave if we've left the previously touched element
if ((!newTouchInfo || newTouchInfo.element !== oldTouchInfo.element) && !this._touchesForWhichTouchLeaveHasFired[touch.identifier]) {
Expand Down Expand Up @@ -918,28 +919,6 @@ class ElementInput {
targetY = (event.clientY - top);
}

_calcTouchCoords(touch) {
let totalOffsetX = 0;
let totalOffsetY = 0;
let target = touch.target;
while (!(target instanceof HTMLElement)) {
target = target.parentNode;
}
let currentElement = target;

do {
totalOffsetX += currentElement.offsetLeft - currentElement.scrollLeft;
totalOffsetY += currentElement.offsetTop - currentElement.scrollTop;
currentElement = currentElement.offsetParent;
} while (currentElement);

// calculate coords and scale them to the graphicsDevice size
return {
x: (touch.pageX - totalOffsetX),
y: (touch.pageY - totalOffsetY)
};
}

_sortElements(a, b) {
const layerOrder = this.app.scene.layers.sortTransparentLayers(a.layers, b.layers);
if (layerOrder !== 0) return layerOrder;
Expand Down
14 changes: 7 additions & 7 deletions src/platform/input/touch-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ function getTouchTargetCoords(touch) {
let totalOffsetX = 0;
let totalOffsetY = 0;
let target = touch.target;
while (!(target instanceof HTMLElement)) {

while (!(target instanceof HTMLElement) && target) {
target = target.parentNode;
}
let currentElement = target;

do {
totalOffsetX += currentElement.offsetLeft - currentElement.scrollLeft;
totalOffsetY += currentElement.offsetTop - currentElement.scrollTop;
currentElement = currentElement.offsetParent;
} while (currentElement);
while (target) {
totalOffsetX += target.offsetLeft - target.scrollLeft;
totalOffsetY += target.offsetTop - target.scrollTop;
target = target.offsetParent;
}

return {
x: touch.pageX - totalOffsetX,
Expand Down

0 comments on commit 591831c

Please sign in to comment.