Skip to content

Commit

Permalink
Add relativeCoords average to PointerTracker (#2939)
Browse files Browse the repository at this point in the history
## Description

This PR adds missing `relativeCoords` average in `PointerTracker`.

## Test plan

Add `console.log(this.tracker.getRelativeCoordsAverage());` in `onPointerMove` callback inside `PanGestureHandler` and check that logs contain average position.
  • Loading branch information
m-bert committed Jun 12, 2024
1 parent 0fbf328 commit 8e20b37
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/web/tools/PointerTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export default class PointerTracker {

private lastMovedPointerId: number;

private cachedAverages: { x: number; y: number } = { x: 0, y: 0 };
private cachedAbsoluteAverages: { x: number; y: number } = { x: 0, y: 0 };
private cachedRelativeAverages: { x: number; y: number } = { x: 0, y: 0 };

public constructor() {
this.lastMovedPointerId = NaN;
Expand Down Expand Up @@ -50,7 +51,8 @@ export default class PointerTracker {
this.trackedPointers.set(event.pointerId, newElement);
this.mapTouchEventId(event.pointerId);

this.cachedAverages = this.getAbsoluteCoordsAverage();
this.cachedAbsoluteAverages = this.getAbsoluteCoordsAverage();
this.cachedRelativeAverages = this.getRelativeCoordsAverage();
}

public removeFromTracker(pointerId: number): void {
Expand Down Expand Up @@ -80,7 +82,8 @@ export default class PointerTracker {

this.trackedPointers.set(event.pointerId, element);

this.cachedAverages = this.getAbsoluteCoordsAverage();
this.cachedAbsoluteAverages = this.getAbsoluteCoordsAverage();
this.cachedRelativeAverages = this.getRelativeCoordsAverage();
}

//Mapping TouchEvents ID
Expand Down Expand Up @@ -160,8 +163,22 @@ export default class PointerTracker {
const avgY = coordsSum.y / this.trackedPointers.size;

const averages = {
x: isNaN(avgX) ? this.cachedAverages.x : avgX,
y: isNaN(avgY) ? this.cachedAverages.y : avgY,
x: isNaN(avgX) ? this.cachedAbsoluteAverages.x : avgX,
y: isNaN(avgY) ? this.cachedAbsoluteAverages.y : avgY,
};

return averages;
}

public getRelativeCoordsAverage() {
const coordsSum = this.getRelativeCoordsSum();

const avgX = coordsSum.x / this.trackedPointers.size;
const avgY = coordsSum.y / this.trackedPointers.size;

const averages = {
x: isNaN(avgX) ? this.cachedRelativeAverages.x : avgX,
y: isNaN(avgY) ? this.cachedRelativeAverages.y : avgY,
};

return averages;
Expand Down

0 comments on commit 8e20b37

Please sign in to comment.