Skip to content

Commit fe10c99

Browse files
committed
Implement standard behavior of #_hitTest() for items without children, based on #_contains().
This should cover the minimum of what's needed for PointText.
1 parent 463a00b commit fe10c99

5 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/item/Item.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{
13221322
}
13231323
// We only implement it here for items with rectangular content,
13241324
// for anything else we need to override #contains()
1325+
// TODO: There currently is no caching for the results of direct calls
1326+
// to this._getBounds('getBounds') (without the application of the
1327+
// internal matrix). Performance improvements could be achieved if
1328+
// these were cached too. See #_getCachedBounds().
13251329
return point.isInside(this._getBounds('getBounds'));
13261330
},
13271331

@@ -1420,6 +1424,8 @@ var Item = Base.extend(Callback, /** @lends Item# */{
14201424
var res = this._children[i].hitTest(point, options);
14211425
if (res) return res;
14221426
}
1427+
} else if (this.hasFill() && this._contains(point)) {
1428+
return new HitResult('fill', this);
14231429
}
14241430
},
14251431

src/item/Shape.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ var Shape = Item.extend(/** @lends Shape# */{
8181
return matrix ? matrix._transformBounds(rect) : rect;
8282
},
8383

84-
_hitTest: function(point, options) {
85-
if (this.hasFill() && this.contains(point))
86-
return new HitResult('fill', this);
84+
_hitTest: function _hitTest(point, options) {
8785
// TODO: Implement stroke!
86+
return _hitTest.base.apply(this, arguments);
8887
},
8988

9089
statics: {

src/project/Project.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
331331
if (item._boundsSelected) {
332332
// We need to call the internal _getBounds, to get non-
333333
// transformed bounds.
334+
// TODO: Implement caching for these too!
334335
var coords = mx._transformCorners(
335336
item._getBounds('getBounds'));
336337
// Now draw a rectangle that connects the transformed

test/lib/helpers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
// Register a jsDump parser for Base.
1414
QUnit.jsDump.setParser('Base', function (obj, stack) {
15+
// Just compare the string representation of classes inheriting from Base,
16+
// since they hide the internal values.
1517
return obj.toString();
1618
});
1719

test/tests/TextItem.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ module('TextItem');
1414

1515
test('PointText', function() {
1616
var text = new PointText({
17+
font: 'Arial',
18+
fontSize: 14,
1719
point: [100, 100],
1820
content: 'Hello World!'
1921
});
20-
equals(text.point, { x: 100, y: 100 });
2122
equals(text.fillColor, { red: 0, green: 0, blue: 0 }, 'text.fillColor should be black by default');
23+
equals(text.point, { x: 100, y: 100 });
24+
equals(text.bounds.point, { x: 100, y: 87.4 });
25+
equals(function() {
26+
return text.hitTest(text.bounds.center) != null;
27+
}, true);
2228
});

0 commit comments

Comments
 (0)