Skip to content

Commit

Permalink
fix(core): make raycaster compatible with thouch screens
Browse files Browse the repository at this point in the history
  • Loading branch information
agviegas committed Oct 13, 2024
1 parent 2b23f80 commit 4e6676b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@thatopen/components",
"description": "Collection of core functionalities to author BIM apps.",
"version": "2.3.6",
"version": "2.3.7",
"author": "That Open Company",
"contributors": [
"Antonio Gonzalez Viegas (https://github.com/agviegas)",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/Components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Components implements Disposable {
/**
* The version of the @thatopen/components library.
*/
static readonly release = "2.3.6";
static readonly release = "2.3.7";

/** {@link Disposable.onDisposed} */
readonly onDisposed = new Event<void>();
Expand Down
20 changes: 14 additions & 6 deletions packages/core/src/core/Raycasters/src/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Disposable, Event } from "../../Types";
* A helper to easily get the real position of the mouse in the Three.js canvas to work with tools like the [raycaster](https://threejs.org/docs/#api/en/core/Raycaster), even if it has been transformed through CSS or doesn't occupy the whole screen.
*/
export class Mouse implements Disposable {
private _event?: MouseEvent;
private _event?: MouseEvent | TouchEvent;
private _position = new THREE.Vector2();

/** {@link Disposable.onDisposed} */
Expand Down Expand Up @@ -34,23 +34,31 @@ export class Mouse implements Disposable {
this.onDisposed.reset();
}

private getPositionY(bound: DOMRect, event: MouseEvent) {
return -((event.clientY - bound.top) / (bound.bottom - bound.top)) * 2 + 1;
private getPositionY(bound: DOMRect, event: MouseEvent | TouchEvent) {
const data = this.getDataObject(event);
return -((data.clientY - bound.top) / (bound.bottom - bound.top)) * 2 + 1;
}

private getPositionX(bound: DOMRect, event: MouseEvent) {
return ((event.clientX - bound.left) / (bound.right - bound.left)) * 2 - 1;
private getPositionX(bound: DOMRect, event: MouseEvent | TouchEvent) {
const data = this.getDataObject(event);
return ((data.clientX - bound.left) / (bound.right - bound.left)) * 2 - 1;
}

private updateMouseInfo = (event: MouseEvent) => {
private updateMouseInfo = (event: MouseEvent | TouchEvent) => {
this._event = event;
};

private getDataObject(event: MouseEvent | TouchEvent) {
return event instanceof MouseEvent ? event : event.touches[0];
}

private setupEvents(active: boolean) {
if (active) {
this.dom.addEventListener("pointermove", this.updateMouseInfo);
this.dom.addEventListener("touchstart", this.updateMouseInfo);
} else {
this.dom.removeEventListener("pointermove", this.updateMouseInfo);
this.dom.removeEventListener("touchstart", this.updateMouseInfo);
}
}
}

0 comments on commit 4e6676b

Please sign in to comment.