diff --git a/src/GLViewer.ts b/src/GLViewer.ts index 31d408d31..643236a47 100644 --- a/src/GLViewer.ts +++ b/src/GLViewer.ts @@ -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 - @@ -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;