Add Events.getOffset(), to return coordinates relative to the target element's position.

This commit is contained in:
Jürg Lehni 2011-03-04 23:46:16 +00:00
parent 20f3460222
commit 342c5911c1
2 changed files with 18 additions and 4 deletions

View file

@ -1,9 +1,6 @@
var Tool = this.Tool = ToolHandler.extend(new function() {
function viewToArtwork(event, document) {
var x = event.pageX || event.clientX + document.documentElement.scrollLeft;
var y = event.pageY || event.clientY + document.documentElement.scrollTop;
// TODO: Remove canvas offset
var point = Point.create(x, y);
var point = Events.getOffset(event);
// TODO: always the active view?
return document.activeView.viewToArtwork(point);
};
@ -68,6 +65,7 @@ var Tool = this.Tool = ToolHandler.extend(new function() {
},
setDocument: function(doc) {
// Remove old events first.
if (this._document)
Events.remove(this._document.canvas, this.events);
this._document = doc || paper.document;

View file

@ -24,5 +24,21 @@ var Events = {
obj.detachEvent('on' + type, func.bound);
}
}
},
getPoint: function(event) {
return Point.create(
event.pageX || event.clientX + document.documentElement.scrollLeft,
event.pageY || event.clientY + document.documentElement.scrollTop
);
},
getOffset: function(event) {
var point = Events.getPoint(event);
// Remove target offsets from page coordinates
for (var el = event.target || event.srcElement; el;
point.x -= el.offsetLeft, point.y -= el.offsetTop,
el = el.offsetParent);
return point;
}
};