]> git.proxmox.com Git - mirror_novnc.git/commitdiff
Remove Util.getEventPosition()
authorSamuel Mannehed <samuel@cendio.se>
Thu, 16 Feb 2017 09:43:32 +0000 (10:43 +0100)
committerSamuel Mannehed <samuel@cendio.se>
Thu, 16 Feb 2017 12:59:34 +0000 (13:59 +0100)
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
core/util.js

index 91a202c586c8dad0c80cba785335cc2741d70dcc..d9e52e2e544c0b51afc7c5d0b9d8788bdae09f12 100644 (file)
             }
 
             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) {
             }
 
             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) {
             }
 
             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);
             }
             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 () {
index 30f75e1b6b51333d1a1a8a9f6f61ec37179ff174..a3500223d1a6c83bbcfb0c3569e31f2016315279 100644 (file)
@@ -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();