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

Unify scaled coordinates between web and native side. #2943

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
12 changes: 9 additions & 3 deletions src/web/tools/PointerEventManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import EventManager from './EventManager';
import { MouseButton } from '../../handlers/gestureHandlerCommon';
import { AdaptedEvent, EventTypes, Point } from '../interfaces';
import { PointerTypeMapping, isPointerInBounds } from '../utils';
import {
PointerTypeMapping,
calculateViewScale,
isPointerInBounds,
} from '../utils';
import { PointerType } from '../../PointerType';

const POINTER_CAPTURE_EXCLUDE_LIST = new Set<string>(['SELECT', 'INPUT']);
Expand Down Expand Up @@ -234,11 +238,13 @@ export default class PointerEventManager extends EventManager<HTMLElement> {
}

protected mapEvent(event: PointerEvent, eventType: EventTypes): AdaptedEvent {
const { scaleX, scaleY } = calculateViewScale(this.view);

return {
x: event.clientX,
y: event.clientY,
offsetX: event.offsetX,
offsetY: event.offsetY,
offsetX: event.offsetX / scaleX,
offsetY: event.offsetY / scaleY,
j-piasecki marked this conversation as resolved.
Show resolved Hide resolved
pointerId: event.pointerId,
eventType: eventType,
pointerType:
Expand Down
8 changes: 5 additions & 3 deletions src/web/tools/TouchEventManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AdaptedEvent, EventTypes, TouchEventType } from '../interfaces';
import EventManager from './EventManager';
import { isPointerInBounds } from '../utils';
import { calculateViewScale, isPointerInBounds } from '../utils';
import { PointerType } from '../../PointerType';

export default class TouchEventManager extends EventManager<HTMLElement> {
Expand Down Expand Up @@ -156,11 +156,13 @@ export default class TouchEventManager extends EventManager<HTMLElement> {
const clientX = event.changedTouches[index].clientX;
const clientY = event.changedTouches[index].clientY;

const { scaleX, scaleY } = calculateViewScale(this.view);

return {
x: clientX,
y: clientY,
offsetX: clientX - rect.left,
offsetY: clientY - rect.top,
offsetX: (clientX - rect.left) / scaleX,
offsetY: (clientY - rect.top) / scaleY,
pointerId: event.changedTouches[index].identifier,
eventType: eventType,
pointerType: PointerType.TOUCH,
Expand Down
33 changes: 33 additions & 0 deletions src/web/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,36 @@

export const coneToDeviation = (degrees: number) =>
Math.cos(degToRad(degrees / 2));

export function calculateViewScale(view: HTMLElement) {
const styles = getComputedStyle(view);

const resultScales = {
scaleX: 1,
scaleY: 1,
};

const scales = styles.scale.split(' ');

if (scales[0] !== 'none') {
resultScales.scaleX *= parseFloat(scales[0]);
}

if (scales[1]) {
resultScales.scaleY *= parseFloat(scales[1]);
}
latekvo marked this conversation as resolved.
Show resolved Hide resolved

const transform = styles.transform;
const matrixElements = transform.match(/matrix\((.+)\)/)?.[1];

Check failure on line 41 in src/web/utils.ts

View workflow job for this annotation

GitHub Actions / check

Use the `RegExp#exec()` method instead

if (!matrixElements) {
return resultScales;
}

const matrixElementsArray = matrixElements.split(', ');

resultScales.scaleX *= parseFloat(matrixElementsArray[0]);
resultScales.scaleY *= parseFloat(matrixElementsArray[3]);

return resultScales;
}
Loading