From 9fa51a04573f295085eebeaaf5ca7a7dabf0d8c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Fri, 1 Jul 2011 12:51:18 +0200 Subject: [PATCH] Implement Item#controlBounds and Path#controlBounds, and test it in StrokeBounds example. This is a first step towards fast hit-testing. --- examples/Scripts/StrokeBounds.html | 7 ++++++- src/item/Item.js | 15 ++++++++++----- src/path/Path.js | 30 +++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/examples/Scripts/StrokeBounds.html b/examples/Scripts/StrokeBounds.html index bd4bdc5a..e2bbb7b1 100644 --- a/examples/Scripts/StrokeBounds.html +++ b/examples/Scripts/StrokeBounds.html @@ -23,7 +23,6 @@ } var path = makePath(); - path.fullySelected = true; path.strokeColor = 'black'; path.strokeCap = 'butt'; path.strokeJoin = 'round'; @@ -70,6 +69,7 @@ for (var i = 0; i < paths.length; i++) { var path = paths[i]; + path.fullySelected = true; path.scale(1.5, new Point(300, 0)); var rect = new Path.Rectangle(path.strokeBounds); rect.strokeWidth = 0.25; @@ -79,6 +79,11 @@ rect.strokeWidth = 0.25; rect.strokeColor = 'red'; rect.fillColor = null; + console.log(path.controlBounds); + var rect = new Path.Rectangle(path.controlBounds); + rect.strokeWidth = 0.25; + rect.strokeColor = 'green'; + rect.fillColor = null; } project.activeLayer.position = view.center; diff --git a/src/item/Item.js b/src/item/Item.js index 2a304e50..0a1adb26 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1077,6 +1077,16 @@ var Item = this.Item = Base.extend(/** @lends Item# */{ return this._getBounds('getStrokeBounds'); }, + /** + * The bounding rectangle of the item including handles. + * + * @type Rectangle + * @bean + */ + getControlBounds: function() { + return this._getBounds('getControlBounds'); + }, + /** * Loops through all children, gets their bounds and finds the bounds around * all of them. @@ -1113,11 +1123,6 @@ var Item = this.Item = Base.extend(/** @lends Item# */{ rect.x, rect.y, rect.width, rect.height); }, - /** - * The bounding rectangle of the item including stroke width and controls. - */ - // TODO: getControlBounds - /** * {@grouptitle Stroke Style} * diff --git a/src/path/Path.js b/src/path/Path.js index da639164..e99e5580 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1894,9 +1894,37 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ /** * The bounding rectangle of the item including handles. + * + * @type Rectangle + * @bean */ getControlBounds: function() { - // TODO: Implement! + var x1 = Infinity, + x2 = -Infinity, + y1 = x1, + y2 = x2; + + function add(point, relative) { + var x = point._x, + y = point._y; + if (relative) { + x += relative._x; + y += relative._y; + } + if (x < x1) x1 = x; + if (x > x2) x2 = x; + if (y < y1) y1 = y; + if (y > y2) y2 = y; + } + + for (var i = 0, l = this._segments.length; i < l; i++) { + var segment = this._segments[i], + point = segment._point; + add(point); + add(segment._handleIn, point); + add(segment.handleOut, point); + } + return Rectangle.create(x1, y1, x2 - x1, y2 - y1); } // TODO: intersects(item)