From 906faf7956bcd79ee17cc2c267a07f8f7d39b993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Fri, 23 Nov 2012 12:41:00 -0800 Subject: [PATCH] Improve handling of nested matrices in hit-testing. Closes #134. --- src/item/HitResult.js | 3 +-- src/item/Item.js | 7 ++++--- src/item/PlacedItem.js | 3 +-- src/project/Project.js | 6 ++++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/item/HitResult.js b/src/item/HitResult.js index ef56d46d..fdc5de2d 100644 --- a/src/item/HitResult.js +++ b/src/item/HitResult.js @@ -106,10 +106,9 @@ HitResult = Base.extend(/** @lends HitResult# */{ * * @private */ - getOptions: function(point, options) { + getOptions: function(options) { // Use _merged property to not repeatetly call merge in recursion. return options && options._merged ? options : Base.merge({ - point: Point.read([point]), // Type of item, for instanceof check: PathItem, TexItem, etc type: null, // Tolerance diff --git a/src/item/Item.js b/src/item/Item.js index 05dd46bc..a0713e7c 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1062,16 +1062,17 @@ function(name) { * hit. */ hitTest: function(point, options) { - options = HitResult.getOptions(point, options); + point = Point.read(arguments); + options = HitResult.getOptions(Base.readValue(arguments)); // Check if the point is withing roughBounds + tolerance, but only if // this item does not have children, since we'd have to travel up the // chain already to determine the rough bounds. if (!this._children && !this.getRoughBounds() - .expand(options.tolerance)._containsPoint(options.point)) + .expand(options.tolerance)._containsPoint(point)) return null; // Transform point to local coordinates but use untransformed point // for bounds check above. - point = options.point = this._matrix._inverseTransform(options.point); + point = this._matrix._inverseTransform(point); if ((options.center || options.bounds) && // Ignore top level layers: !(this instanceof Layer && !this._parent)) { diff --git a/src/item/PlacedItem.js b/src/item/PlacedItem.js index 0242de8a..aa3755be 100644 --- a/src/item/PlacedItem.js +++ b/src/item/PlacedItem.js @@ -28,8 +28,7 @@ var PlacedItem = this.PlacedItem = Item.extend(/** @lends PlacedItem# */{ _boundsType: { bounds: 'strokeBounds' }, _hitTest: function(point, options, matrix) { - var hitResult = this._symbol._definition.hitTest( - this.matrix._transformPoint(point), options, matrix); + var hitResult = this._symbol._definition._hitTest(point, options, matrix); // TODO: When the symbol's definition is a path, should hitResult contain // information like HitResult#curve? if (hitResult) diff --git a/src/project/Project.js b/src/project/Project.js index cf275096..f05c499a 100644 --- a/src/project/Project.js +++ b/src/project/Project.js @@ -223,8 +223,10 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{ * hit. */ hitTest: function(point, options) { - options = HitResult.getOptions(point, options); - point = options.point; + // We don't need to do this here, but it speeds up things since we won't + // repeatetly convert in Item#hitTest() then. + point = Point.read(arguments); + options = HitResult.getOptions(Base.readValue(arguments)); // Loop backwards, so layers that get drawn last are tested first for (var i = this.layers.length - 1; i >= 0; i--) { var res = this.layers[i].hitTest(point, options);