Implement Item#contains() and use it for hit-testing Rasters.

This commit is contained in:
Jürg Lehni 2013-04-20 20:41:52 -07:00
parent bb03c72ce8
commit db7eb65bd7
4 changed files with 31 additions and 4 deletions

View file

@ -1276,6 +1276,27 @@ var Item = this.Item = Base.extend(Callback, {
return raster;
},
// DOCS: Document Item#contains(point)
/**
* Checks wether the item's geometry contains the given point in local
* coordinates.
*
* @param {Point} point The point to check in local coordinates
*/
contains: function(point) {
point = Point.read(arguments);
if (this._children) {
for (var i = this._children.length - 1; i >= 0; i--) {
if (this._children[i].contains(point))
return true;
}
return false;
}
// We only implement it here for items with rectangular content,
// for anything else we need to override #contains()
return point.isInside(this._getBounds('getBounds'));
},
/**
* Perform a hit test on the item (and its children, if it is a
* {@link Group} or {@link Layer}) at the location of the specified point.

View file

@ -523,7 +523,7 @@ var Raster = this.Raster = Item.extend(/** @lends Raster# */{
},
_hitTest: function(point, options) {
if (point.isInside(this._getBounds())) {
if (this.contains(point)) {
var that = this;
return new HitResult('pixel', that, {
offset: point.add(that._size.divide(2)).round(),

View file

@ -65,15 +65,20 @@ var Shape = this.Shape = Item.extend(/** @lends Shape# */{
}
},
contains: function(point) {
point = Point.read(arguments);
// TODO: Implement.
},
_getBounds: function(getter, matrix) {
var rect = new Rectangle(this._size).setCenter(0, 0);
return matrix ? matrix._transformBounds(rect) : rect;
},
_hitTest: function(point, options) {
// TODO: Implement!
if (point.isInside(this._getBounds())) {
}
if (this.hasFill() && this.contains(point))
return new HitResult('fill', this);
// TODO: Implement stokre!
},
statics: {

View file

@ -1604,6 +1604,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
&& this._parent._style.getFillColor());
},
// DOCS: Document Path#contains()
contains: function(point) {
point = Point.read(arguments);
// If the path is not closed, we should not bail out in case it has a