Skip to content

Commit

Permalink
Add double tap support. Related #47
Browse files Browse the repository at this point in the history
  • Loading branch information
bumbu committed Jul 30, 2014
1 parent 3b868f0 commit 297df90
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/svg-pan-zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,17 @@ var Mousewheel = require('./mousewheel') // Keep it here so that mousewheel is
*/
SvgPanZoom.prototype.setupHandlers = function() {
var that = this
, prevEvt = null // use for touchstart event to detect double tap
;

// Mouse down group
this.svg.addEventListener("mousedown", function(evt) {
return that.handleMouseDown(evt);
return that.handleMouseDown(evt, null);
}, false);
this.svg.addEventListener("touchstart", function(evt) {
return that.handleMouseDown(evt);
var result = that.handleMouseDown(evt, prevEvt);
prevEvt = evt
return result;
}, false);

// Mouse up group
Expand Down Expand Up @@ -399,15 +403,18 @@ var Mousewheel = require('./mousewheel') // Keep it here so that mousewheel is
*
* @param {object} evt Event
*/
SvgPanZoom.prototype.handleMouseDown = function(evt) {
SvgPanZoom.prototype.handleMouseDown = function(evt, prevEvt) {
if (evt.preventDefault) {
evt.preventDefault()
} else {
evt.returnValue = false
}

var svg = (evt.target.tagName === 'svg' || evt.target.tagName === 'SVG') ? evt.target : evt.target.ownerSVGElement || evt.target.correspondingElement.ownerSVGElement
Utils.mouseAndTouchNormalize(evt, svg)

// Double click detection; more consistent than ondblclick
if (evt.detail === 2 && this.options.dblClickZoomEnabled){
if (this.options.dblClickZoomEnabled && Utils.isDblClick(evt, prevEvt)){
this.handleDblClick(evt)
} else {
// Pan mode
Expand Down
26 changes: 26 additions & 0 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,30 @@ module.exports = {
}
}
}

/**
* Check if an event is a double click/tap
* TODO: For touch gestures use a library (hammer.js) that takes in account other events
* (touchmove and touchend). It should take in account tap duration and traveled distance
*
* @param {object} evt Event
* @param {object} prevEvt Previous Event
* @return {Boolean} [description]
*/
, isDblClick: function(evt, prevEvt) {
// Double click detected by browser
if (evt.detail === 2) {
return true;
}
// Try to compare events
else if (prevEvt !== void 0 && prevEvt !== null) {
var timeStampDiff = evt.timeStamp - prevEvt.timeStamp // should be lower than 250 ms
, touchesDistance = Math.sqrt(Math.pow(evt.clientX - prevEvt.clientX, 2) + Math.pow(evt.clientY - prevEvt.clientY, 2))

return timeStampDiff < 250 && touchesDistance < 10
}

// Nothing found
return false;
}
}

0 comments on commit 297df90

Please sign in to comment.