From 48fa889e0dfa2d3c240245b7c17d7c56fa54a618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 13 Jun 2013 10:31:21 -0700 Subject: [PATCH] Start implementing proper hit-testing for Shapes with strokes. Support for Shape.Rectangle is still missing. --- src/item/Shape.js | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/item/Shape.js b/src/item/Shape.js index 352c0a4a..d6252e96 100644 --- a/src/item/Shape.js +++ b/src/item/Shape.js @@ -67,6 +67,13 @@ var Shape = Item.extend(/** @lends Shape# */{ } }, + _getBounds: function(getter, matrix) { + var rect = new Rectangle(this._size).setCenter(0, 0); + if (this.hasStroke()) + rect = rect.expand(this.getStrokeWidth()); + return matrix ? matrix._transformBounds(rect) : rect; + }, + _contains: function _contains(point) { switch (this._type) { case 'rect': @@ -77,13 +84,34 @@ var Shape = Item.extend(/** @lends Shape# */{ } }, - _getBounds: function(getter, matrix) { - var rect = new Rectangle(this._size).setCenter(0, 0); - return matrix ? matrix._transformBounds(rect) : rect; - }, - _hitTest: function _hitTest(point, options) { - // TODO: Implement stroke! + if (this.hasStroke()) { + var type = this._type, + strokeWidth = this.getStrokeWidth(); + switch (type) { + case 'rect': + // TODO: Implement stroke! + break; + case 'circle': + case 'ellipse': + var size = this._size, + width = size.width, + height = size.height, + radius; + if (type === 'ellipse') { + // Calculate ellipse radius at angle + var angle = point.getAngleInRadians(), + x = width * Math.sin(angle), + y = height * Math.cos(angle); + radius = width * height / (2 * Math.sqrt(x * x + y * y)); + } else { + // Average half of width & height for radius... + radius = (width + height) / 4; + } + if (2 * Math.abs(point.getLength() - radius) <= strokeWidth) + return new HitResult('stroke', this); + } + } return _hitTest.base.apply(this, arguments); },