Implement Item#controlBounds and Path#controlBounds, and test it in StrokeBounds example. This is a first step towards fast hit-testing.

This commit is contained in:
Jürg Lehni 2011-07-01 12:51:18 +02:00
parent ee23877fd1
commit 9fa51a0457
3 changed files with 45 additions and 7 deletions

View file

@ -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;

View file

@ -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}
*

View file

@ -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)