From fabf05977a7bd025ba68f53aa1a41dcdabe06925 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Thu, 27 Nov 2014 10:17:09 +0100 Subject: [PATCH] Use offsetX and offsetY if available --- src/ol/map.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/ol/map.js b/src/ol/map.js index f7621f3ca8..6905e65eae 100644 --- a/src/ol/map.js +++ b/src/ol/map.js @@ -592,18 +592,25 @@ ol.Map.prototype.getEventCoordinate = function(event) { /** - * Returns the map pixel position for a browser event. + * Returns the map pixel position for a browser event relative to the viewport. * @param {Event} event Event. * @return {ol.Pixel} Pixel. * @api stable */ ol.Map.prototype.getEventPixel = function(event) { - // goog.style.getRelativePosition is based on event.targetTouches, - // but touchend and touchcancel events have no targetTouches when - // the last finger is removed from the screen. - // So we ourselves compute the position of touch events. - // See https://github.com/google/closure-library/pull/323 - if (goog.isDef(event.changedTouches)) { + // Use the offsetX and offsetY values if available. + // See http://www.w3.org/TR/cssom-view/#dom-mouseevent-offsetx and + // http://www.w3.org/TR/cssom-view/#dom-mouseevent-offsety + if (goog.isDef(event.offsetX) && goog.isDef(event.offsetY)) { + return [event.offsetX, event.offsetY]; + } else if (goog.isDef(event.changedTouches)) { + // offsetX and offsetY are not defined for Touch Event + // + // goog.style.getRelativePosition is based on event.targetTouches, + // but touchend and touchcancel events have no targetTouches when + // the last finger is removed from the screen. + // So we ourselves compute the position of touch events. + // See https://github.com/google/closure-library/pull/323 var touch = event.changedTouches[0]; var viewportPosition = goog.style.getClientPosition(this.viewport_); return [ @@ -611,6 +618,8 @@ ol.Map.prototype.getEventPixel = function(event) { touch.clientY - viewportPosition.y ]; } else { + // Compute offsetX and offsetY values for browsers that don't implement + // cssom-view specification var eventPosition = goog.style.getRelativePosition(event, this.viewport_); return [eventPosition.x, eventPosition.y]; }