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.
This commit is contained in:
Samuel Mannehed 2017-02-16 10:43:32 +01:00
parent b69dda9b19
commit af1b2ae1e5
2 changed files with 28 additions and 38 deletions

View File

@ -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 () {

View 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();