}
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 () {
* 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);
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();