From af1b2ae1e593aba5d14ac84ff41b7a95d29a661e Mon Sep 17 00:00:00 2001
From: Samuel Mannehed <samuel@cendio.se>
Date: Thu, 16 Feb 2017 10:43:32 +0100
Subject: [PATCH] Remove Util.getEventPosition()

It mostly dealt with scrolling which we don't use. It also made mistakes
in some cases. Remove it and compute the coordinates directly in the
calling code.
---
 core/input/devices.js | 31 ++++++++++++++++++++++++++++---
 core/util.js          | 35 -----------------------------------
 2 files changed, 28 insertions(+), 38 deletions(-)

diff --git a/core/input/devices.js b/core/input/devices.js
index 91a202c..d9e52e2 100644
--- a/core/input/devices.js
+++ b/core/input/devices.js
@@ -214,7 +214,7 @@
             }
 
             var evt = (e ? e : window.event);
-            var pos = Util.getEventPosition(e, this._target, this._scale);
+            var pos = this._getMousePosition(evt);
 
             var bmask;
             if (e.touches || e.changedTouches) {
@@ -286,7 +286,7 @@
             }
 
             var evt = (e ? e : window.event);
-            var pos = Util.getEventPosition(e, this._target, this._scale);
+            var pos = this._getMousePosition(evt);
 
             if (this._onMouseButton) {
                 if (evt.deltaX < 0) {
@@ -318,7 +318,7 @@
             }
 
             var evt = (e ? e : window.event);
-            var pos = Util.getEventPosition(e, this._target, this._scale);
+            var pos = this._getMousePosition(evt);
             if (this._onMouseMove) {
                 this._onMouseMove(pos.x, pos.y);
             }
@@ -345,6 +345,31 @@
             return true;
         },
 
+        // Return coordinates relative to target
+        _getMousePosition: function(e) {
+            e = Util.getPointerEvent(e);
+            var bounds = this._target.getBoundingClientRect();
+            var x, y;
+            // Clip to target bounds
+            if (e.clientX < bounds.left) {
+                x = 0;
+            } else if (e.clientX >= bounds.right) {
+                x = bounds.width - 1;
+            } else {
+                x = e.clientX - bounds.left;
+            }
+            if (e.clientY < bounds.top) {
+                y = 0;
+            } else if (e.clientY >= bounds.bottom) {
+                y = bounds.height - 1;
+            } else {
+                y = e.clientY - bounds.top;
+            }
+            x = x / this._scale;
+            y = y / this._scale;
+            return {x:x, y:y};
+        },
+
 
         // Public methods
         grab: function () {
diff --git a/core/util.js b/core/util.js
index 30f75e1..a350022 100644
--- a/core/util.js
+++ b/core/util.js
@@ -194,16 +194,6 @@ Util.decodeUTF8 = function (utf8string) {
  * Cross-browser routines
  */
 
-Util.getPosition = function(obj) {
-    "use strict";
-    // NB(sross): the Mozilla developer reference seems to indicate that
-    // getBoundingClientRect includes border and padding, so the canvas
-    // style should NOT include either.
-    var objPosition = obj.getBoundingClientRect();
-    return {'x': objPosition.left + window.pageXOffset, 'y': objPosition.top + window.pageYOffset,
-            'width': objPosition.width, 'height': objPosition.height};
-};
-
 Util.getPointerEvent = function (e) {
     var evt;
     evt = (e ? e : window.event);
@@ -211,31 +201,6 @@ Util.getPointerEvent = function (e) {
     return evt;
 };
 
-// Get mouse event position in DOM element
-Util.getEventPosition = function (e, obj, scale) {
-    "use strict";
-    var evt, docX, docY, pos;
-    evt = Util.getPointerEvent(e);
-    if (evt.pageX || evt.pageY) {
-        docX = evt.pageX;
-        docY = evt.pageY;
-    } else if (evt.clientX || evt.clientY) {
-        docX = evt.clientX + document.body.scrollLeft +
-            document.documentElement.scrollLeft;
-        docY = evt.clientY + document.body.scrollTop +
-            document.documentElement.scrollTop;
-    }
-    pos = Util.getPosition(obj);
-    if (typeof scale === "undefined") {
-        scale = 1;
-    }
-    var realx = docX - pos.x;
-    var realy = docY - pos.y;
-    var x = Math.max(Math.min(realx, pos.width - 1), 0);
-    var y = Math.max(Math.min(realy, pos.height - 1), 0);
-    return {'x': x / scale, 'y': y / scale, 'realx': realx / scale, 'realy': realy / scale};
-};
-
 Util.stopEvent = function (e) {
     e.stopPropagation();
     e.preventDefault();
-- 
GitLab