Skip to content

Commit

Permalink
Fix paperjs#977: Implement unit-tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lehni committed Feb 16, 2016
1 parent 4081afb commit 6df4602
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/basic/Point.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ var Point = Base.extend(/** @lends Point# */{
* @function
* @operator
* @param {Number} number the number to multiply by
* @return {Point} the multiplication of the point and the value as a new point
* @return {Point} the multiplication of the point and the value as a new
* point
*
* @example
* var point = new Point(10, 20);
Expand Down
5 changes: 4 additions & 1 deletion src/item/Shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ new function() { // Scope for _contains() and _hitTestSelf() code.
if (hitStroke || hitFill) {
var type = this._type,
radius = this._radius,
strokeRadius = hitStroke ? style.getStrokeWidth() / 2 : 0;
strokeRadius = hitStroke ? style.getStrokeWidth() / 2 : 0,
strokePadding = options._tolerancePadding.add(
Path._getStrokePadding(strokeRadius,
!style.getStrokeScaling() && strokeMatrix));
Expand All @@ -374,6 +374,9 @@ new function() { // Scope for _contains() and _hitTestSelf() code.
hit = isOnEllipseStroke(point, radius, strokePadding);
}
}
// NOTE: The above test is only for stroke, and the added tolerance
// when testing for fill. The actual fill test happens in
// Item#_hitTestSelf(), through its call of #_contains().
return hit ? new HitResult(hitStroke ? 'stroke' : 'fill', this)
: _hitTestSelf.base.apply(this, arguments);
}
Expand Down
33 changes: 30 additions & 3 deletions test/tests/HitResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,21 +641,47 @@ test('hit-testing guides.', function() {
}, true);
});

test('hit-testing fill with tolerance', function() {
test('hit-testing fills with tolerance', function() {
var path = new Path.Rectangle({
from: [50, 50],
to: [200, 200],
fillColor: 'red'
});

var tolerance = 10;
var point = path.bounds.bottomRight.add(tolerance / Math.sqrt(2));

equals(function() {
var tolerance = 10;
var result = paper.project.hitTest(path.bounds.bottomRight.add(tolerance / Math.sqrt(2)), {
var result = paper.project.hitTest(point, {
tolerance: tolerance,
fill: true
});
return result && result.item === path;
}, true);

var point = new Point(20, 20);
var size = new Size(40, 40);
var hitPoint = new Point(10, 10);
var options = {
fill: true,
tolerance: 20
};

var shapeRect = new Shape.Rectangle(point, size);
shapeRect.fillColor = 'black';

var pathRect = new Path.Rectangle(point, size);
pathRect.fillColor = 'black';

equals(function() {
var hit = shapeRect.hitTest(hitPoint, options);
return hit && hit.type === 'fill';
}, true);

equals(function() {
var hit = pathRect.hitTest(hitPoint, options);
return hit && hit.type === 'fill';
}, true);
});

test('hit-testing compound-paths', function() {
Expand Down Expand Up @@ -833,5 +859,6 @@ test('hit-testing for all items', function() {
return result.length === 0;
}, true);
});

// TODO: project.hitTest(point, {type: AnItemType});

0 comments on commit 6df4602

Please sign in to comment.