Define compoundChildren hitTest option to get matching children instead of parent.

This commit is contained in:
Jürg Lehni 2013-10-18 15:40:41 +02:00
parent f8106ae18f
commit 304ecbc3be
2 changed files with 17 additions and 17 deletions

View file

@ -205,34 +205,33 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
children[i]._draw(ctx, param); children[i]._draw(ctx, param);
var res = ctx.isPointInPath(point.x, point.y, this.getWindingRule()); var res = ctx.isPointInPath(point.x, point.y, this.getWindingRule());
CanvasProvider.release(ctx); CanvasProvider.release(ctx);
return res && children; return res;
/*#*/ } // options.nativeContains /*#*/ } else { // !options.nativeContains
// Compound paths are a little complex: In order to determine whether a // Compound paths are a little complex: In order to determine whether a
// point is inside a path or not due to the winding rule, we need to // point is inside a path or not due to the winding rule, we need to
// check all the children and count how many intersect. If it's an odd // check all the children and count how many intersect. If it's an odd
// number, the point is inside the path. Once we know it's inside the // number, the point is inside the path. Once we know it's inside the
// path, _hitTest also needs access to the first intersecting element, // path, _hitTest also needs access to the first intersecting element,
// for the HitResult, so we return it here. // for the HitResult, so we return it here.
var total = 0, var children = this._children,
first = null, winding = 0;
evenOdd = this.getWindingRule() === 'evenodd'; for (var i = 0, l = children.length; i < l; i++)
for (var i = 0, l = this._children.length; i < l; i++) { winding += children[i]._getWinding(point);
var child = this._children[i], return !!(this.getWindingRule() === 'evenodd' ? winding & 1 : winding);
winding = child._getWinding(point); /*#*/ } // !options.nativeContains
total += winding;
if (!first && (evenOdd ? winding & 1 : winding))
first = child;
}
return (evenOdd ? total & 1 : total) && first;
}, },
_hitTest: function _hitTest(point, options) { _hitTest: function _hitTest(point, options) {
var res = _hitTest.base.call(this, point, var res = _hitTest.base.call(this, point,
Base.merge(options, { fill: false })); Base.merge(options, { fill: false }));
if (!res && options.fill && this.hasFill()) { if (!res && options.fill && this.hasFill()) {
res = this._contains(point); if (options.compoundChildren) {
res = res ? new HitResult('fill', res) : null; var children = this._children;
for (var i = children.length - 1; i >= 0 && !res; i--)
res = children[i]._hitTest(point, options);
} else if (this._contains(point)) {
res = new HitResult('fill', this);
}
} }
return res; return res;
}, },

View file

@ -1735,9 +1735,10 @@ var Path = PathItem.extend(/** @lends Path# */{
var res = ctx.isPointInPath(point.x, point.y, this.getWindingRule()); var res = ctx.isPointInPath(point.x, point.y, this.getWindingRule());
CanvasProvider.release(ctx); CanvasProvider.release(ctx);
return res; return res;
/*#*/ } // options.nativeContains /*#*/ } else { // !options.nativeContains
var winding = this._getWinding(point); var winding = this._getWinding(point);
return !!(this.getWindingRule() == 'evenodd' ? winding & 1 : winding); return !!(this.getWindingRule() == 'evenodd' ? winding & 1 : winding);
/*#*/ } // !options.nativeContains
}, },
_hitTest: function(point, options) { _hitTest: function(point, options) {