Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mobile touch coordinates #4397

Merged
merged 2 commits into from
Sep 28, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/systems/userinput/devices/app-aware-touchscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ function shouldMoveCursor(touch, raycaster) {
return true;
}
const rawIntersections = [];
const canvas = document.querySelector(".a-canvas").getBoundingClientRect();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only gets called in touchStart anyway so its not a huge deal, but querySelectors can be really slow so we try to avoid them when we can. Also the use of left and top on the next lines threw me because of the variable name, didn't notice the getBoundingClientRect() at first

Suggested change
const canvas = document.querySelector(".a-canvas").getBoundingClientRect();
const canvasRect = AFRAME.scense[0].getBoundingClientRect();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to use AFRAME.scene[0] but for some reason it includes the toolbar as well, whereas the canvas doesn't.
image
But we can store a ref to canvas to avoid querying it every time.

Copy link
Contributor Author

@yakyouk yakyouk Jul 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed some changes:
We actually have a ref to canvas in this.canvas, and I store canvas rect in this.canvasRect and update it on resize using ResizeObserver.
Since shouldMoveCursor is outside of the class and does not see this.canvasRect, I pass rect to the function as well.

raycaster.setFromCamera(
{
x: (touch.clientX / window.innerWidth) * 2 - 1,
y: -(touch.clientY / window.innerHeight) * 2 + 1
x: ((touch.clientX - canvas.left) / canvas.width) * 2 - 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x: ((touch.clientX - canvas.left) / canvas.width) * 2 - 1,
x: ((touch.clientX - canvasRect.left) / canvasRect.width) * 2 - 1,

y: -((touch.clientY - canvas.top) / canvas.height) * 2 + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
y: -((touch.clientY - canvas.top) / canvas.height) * 2 + 1
y: -((touch.clientY - canvasRect.top) / canvasRect.height) * 2 + 1

},
getPlayerCamera()
);
Expand Down