Skip to content

Commit

Permalink
flick should not trigger click
Browse files Browse the repository at this point in the history
  • Loading branch information
anvaka committed Feb 6, 2022
1 parent 91a3768 commit d086e15
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions demo/click-tap.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

// and forward it it to panzoom.
var pz = panzoom(element, {
autocenter: true,
onClick(e) {
document.getElementById('log').innerText = 'Clicked! Event target is: ' + e.target;
}
Expand Down
15 changes: 13 additions & 2 deletions dist/panzoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ function createPanZoom(domElement, options) {
var mouseX;
var mouseY;

// Where the first click has happened, so that we can differentiate
// between pan and click
var clickX;
var clickY;

var pinchZoomLength;

var smoothScroll;
Expand Down Expand Up @@ -633,6 +638,8 @@ function createPanZoom(domElement, options) {
var point = transformToScreen(offset.x, offset.y);
mouseX = point.x;
mouseY = point.y;
clickX = mouseX;
clickY = mouseY;

smoothScroll.cancel();
startTouchListenerIfNeeded();
Expand Down Expand Up @@ -709,6 +716,10 @@ function createPanZoom(domElement, options) {
// and then notify:
if (!options.onClick) return;
clearPendingClickEventTimeout();
var dx = mouseX - clickX;
var dy = mouseY - clickY;
var l = Math.sqrt(dx * dx + dy * dy);
if (l > 5) return; // probably they are panning, ignore it

pendingClickEventTimeout = setTimeout(function() {
pendingClickEventTimeout = 0;
Expand Down Expand Up @@ -787,8 +798,8 @@ function createPanZoom(domElement, options) {

var offset = getOffsetXY(e);
var point = transformToScreen(offset.x, offset.y);
mouseX = point.x;
mouseY = point.y;
clickX = mouseX = point.x;
clickY = mouseY = point.y;

// We need to listen on document itself, since mouse can go outside of the
// window, and we will loose it
Expand Down
2 changes: 1 addition & 1 deletion dist/panzoom.min.js

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ function createPanZoom(domElement, options) {
var mouseX;
var mouseY;

// Where the first click has happened, so that we can differentiate
// between pan and click
var clickX;
var clickY;

var pinchZoomLength;

var smoothScroll;
Expand Down Expand Up @@ -632,6 +637,8 @@ function createPanZoom(domElement, options) {
var point = transformToScreen(offset.x, offset.y);
mouseX = point.x;
mouseY = point.y;
clickX = mouseX;
clickY = mouseY;

smoothScroll.cancel();
startTouchListenerIfNeeded();
Expand Down Expand Up @@ -708,6 +715,10 @@ function createPanZoom(domElement, options) {
// and then notify:
if (!options.onClick) return;
clearPendingClickEventTimeout();
var dx = mouseX - clickX;
var dy = mouseY - clickY;
var l = Math.sqrt(dx * dx + dy * dy);
if (l > 5) return; // probably they are panning, ignore it

pendingClickEventTimeout = setTimeout(function() {
pendingClickEventTimeout = 0;
Expand Down Expand Up @@ -786,8 +797,8 @@ function createPanZoom(domElement, options) {

var offset = getOffsetXY(e);
var point = transformToScreen(offset.x, offset.y);
mouseX = point.x;
mouseY = point.y;
clickX = mouseX = point.x;
clickY = mouseY = point.y;

// We need to listen on document itself, since mouse can go outside of the
// window, and we will loose it
Expand Down

0 comments on commit d086e15

Please sign in to comment.