Fix CompoundPath hit-testing.

This commit is contained in:
Jürg Lehni 2013-02-24 19:01:29 -08:00
parent 326d8db4db
commit cd9184a6f0

View file

@ -140,21 +140,37 @@ var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath#
return last && last.getFirstCurve(); return last && last.getFirstCurve();
}, },
contains: function(point) { /**
* A private method to help with both #contains() and #_hitTest().
*/
_contains: function(point) {
// Compound-paths are a little complex: Due to the even-odd rule, in
// order to determine wether a point is inside a path or not, we need to
// check all the children and count how many intersect. If it's an odd
// number, the point is inside the path. Once we know we're inside the
// path, _hitTest also needs access to the first intersecting element,
// so we return a list here.
point = Point.read(arguments); point = Point.read(arguments);
var count = 0; var children = [];
for (var i = 0, l = this._children.length; i < l; i++) { for (var i = 0, l = this._children.length; i < l; i++) {
if (this._children[i].contains(point)) var child = this._children[i];
count++; if (child.contains(point))
children.push(child);
} }
return (count & 1) == 1; return (children.length & 1) == 1 && children;
},
contains: function(point) {
return !!this._contains(point);
}, },
_hitTest: function(point, options) { _hitTest: function(point, options) {
return this.base(point, Base.merge(options, { fill: false })) var res = this.base(point, Base.merge(options, { fill: false }));
|| (options.fill && this._style._fillColor && this.contains(point) if (!res && options.fill && this._style._fillColor) {
? new HitResult('fill', this) res = this._contains(point);
: null); res = res ? new HitResult('fill', res[0]) : null;
}
return res;
}, },
draw: function(ctx, param) { draw: function(ctx, param) {