Skip to content

Commit 304ecbc

Browse files
committed
Define compoundChildren hitTest option to get matching children instead of parent.
1 parent f8106ae commit 304ecbc

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

src/path/CompoundPath.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,34 +205,33 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
205205
children[i]._draw(ctx, param);
206206
var res = ctx.isPointInPath(point.x, point.y, this.getWindingRule());
207207
CanvasProvider.release(ctx);
208-
return res && children;
209-
/*#*/ } // options.nativeContains
210-
208+
return res;
209+
/*#*/ } else { // !options.nativeContains
211210
// Compound paths are a little complex: In order to determine whether a
212211
// point is inside a path or not due to the winding rule, we need to
213212
// check all the children and count how many intersect. If it's an odd
214213
// number, the point is inside the path. Once we know it's inside the
215214
// path, _hitTest also needs access to the first intersecting element,
216215
// for the HitResult, so we return it here.
217-
var total = 0,
218-
first = null,
219-
evenOdd = this.getWindingRule() === 'evenodd';
220-
for (var i = 0, l = this._children.length; i < l; i++) {
221-
var child = this._children[i],
222-
winding = child._getWinding(point);
223-
total += winding;
224-
if (!first && (evenOdd ? winding & 1 : winding))
225-
first = child;
226-
}
227-
return (evenOdd ? total & 1 : total) && first;
216+
var children = this._children,
217+
winding = 0;
218+
for (var i = 0, l = children.length; i < l; i++)
219+
winding += children[i]._getWinding(point);
220+
return !!(this.getWindingRule() === 'evenodd' ? winding & 1 : winding);
221+
/*#*/ } // !options.nativeContains
228222
},
229223

230224
_hitTest: function _hitTest(point, options) {
231225
var res = _hitTest.base.call(this, point,
232226
Base.merge(options, { fill: false }));
233227
if (!res && options.fill && this.hasFill()) {
234-
res = this._contains(point);
235-
res = res ? new HitResult('fill', res) : null;
228+
if (options.compoundChildren) {
229+
var children = this._children;
230+
for (var i = children.length - 1; i >= 0 && !res; i--)
231+
res = children[i]._hitTest(point, options);
232+
} else if (this._contains(point)) {
233+
res = new HitResult('fill', this);
234+
}
236235
}
237236
return res;
238237
},

src/path/Path.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1735,9 +1735,10 @@ var Path = PathItem.extend(/** @lends Path# */{
17351735
var res = ctx.isPointInPath(point.x, point.y, this.getWindingRule());
17361736
CanvasProvider.release(ctx);
17371737
return res;
1738-
/*#*/ } // options.nativeContains
1738+
/*#*/ } else { // !options.nativeContains
17391739
var winding = this._getWinding(point);
17401740
return !!(this.getWindingRule() == 'evenodd' ? winding & 1 : winding);
1741+
/*#*/ } // !options.nativeContains
17411742
},
17421743

17431744
_hitTest: function(point, options) {

0 commit comments

Comments
 (0)