Skip to content

Commit

Permalink
Merge pull request #656 from Conifer-Point/wiggle-room-for-touch-clicks
Browse files Browse the repository at this point in the history
Add tolerance for touch clicks
  • Loading branch information
dkoes authored Mar 14, 2023
2 parents f5ff1cd + 0413b80 commit bc342bd
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/GLViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,24 @@ export class GLViewer {
}
};

/**
* Determine if a positioned event is "close enough" to mouseStart to be considered a click.
* With a mouse, the position should be exact, but allow a slight delta for a touch interface.
* @param {Event} event
* @param {{ allowTolerance, tolerance: number }} options
*/
private closeEnoughForClick(event, { allowTolerance=event.targetTouches, tolerance=5}={}) {
const x = this.getX(event);
const y = this.getY(event);
if (allowTolerance) {
const deltaX = Math.abs(x - this.mouseStartX);
const deltaY = Math.abs(y - this.mouseStartY);
return deltaX <= tolerance && deltaY <= tolerance;
} else {
return x === this.mouseStartX && y === this.mouseStartY;
}
}

private calcTouchDistance(ev) { // distance between first two
// fingers
var xdiff = ev.targetTouches[0].pageX -
Expand Down Expand Up @@ -883,7 +901,7 @@ export class GLViewer {
if (this.isDragging && this.scene) { //saw mousedown, haven't moved
var x = this.getX(ev);
var y = this.getY(ev);
if (x == this.mouseStartX && y == this.mouseStartY) {
if (this.closeEnoughForClick(ev)) {
var offset = this.canvasOffset();
var mouseX = ((x - offset.left) / this.WIDTH) * 2 - 1;
var mouseY = -((y - offset.top) / this.HEIGHT) * 2 + 1;
Expand Down

0 comments on commit bc342bd

Please sign in to comment.