summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Mannehed <samuel@cendio.se>2017-02-16 10:43:32 +0100
committerSamuel Mannehed <samuel@cendio.se>2017-02-16 13:59:34 +0100
commitaf1b2ae1e593aba5d14ac84ff41b7a95d29a661e (patch)
tree794ccb50d0f6ef1e871d7fd186612810acee2d79
parentb69dda9b19f538c648cb11faddc50f30b93bac47 (diff)
downloadnovnc-af1b2ae1e593aba5d14ac84ff41b7a95d29a661e.tar.gz
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.
-rw-r--r--core/input/devices.js31
-rw-r--r--core/util.js35
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();