diff --git a/src/tool/Tool.js b/src/tool/Tool.js index a4a908b7..27eff3f5 100644 --- a/src/tool/Tool.js +++ b/src/tool/Tool.js @@ -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; diff --git a/src/util/Events.js b/src/util/Events.js index 8974bc46..141913b5 100644 --- a/src/util/Events.js +++ b/src/util/Events.js @@ -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; } };