mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Start implementing proper hit-testing for Shapes with strokes.
Support for Shape.Rectangle is still missing.
This commit is contained in:
parent
40dbe7ec74
commit
48fa889e0d
1 changed files with 34 additions and 6 deletions
|
@ -67,6 +67,13 @@ var Shape = Item.extend(/** @lends Shape# */{
|
|||
}
|
||||
},
|
||||
|
||||
_getBounds: function(getter, matrix) {
|
||||
var rect = new Rectangle(this._size).setCenter(0, 0);
|
||||
if (this.hasStroke())
|
||||
rect = rect.expand(this.getStrokeWidth());
|
||||
return matrix ? matrix._transformBounds(rect) : rect;
|
||||
},
|
||||
|
||||
_contains: function _contains(point) {
|
||||
switch (this._type) {
|
||||
case 'rect':
|
||||
|
@ -77,13 +84,34 @@ var Shape = Item.extend(/** @lends Shape# */{
|
|||
}
|
||||
},
|
||||
|
||||
_getBounds: function(getter, matrix) {
|
||||
var rect = new Rectangle(this._size).setCenter(0, 0);
|
||||
return matrix ? matrix._transformBounds(rect) : rect;
|
||||
},
|
||||
|
||||
_hitTest: function _hitTest(point, options) {
|
||||
// TODO: Implement stroke!
|
||||
if (this.hasStroke()) {
|
||||
var type = this._type,
|
||||
strokeWidth = this.getStrokeWidth();
|
||||
switch (type) {
|
||||
case 'rect':
|
||||
// TODO: Implement stroke!
|
||||
break;
|
||||
case 'circle':
|
||||
case 'ellipse':
|
||||
var size = this._size,
|
||||
width = size.width,
|
||||
height = size.height,
|
||||
radius;
|
||||
if (type === 'ellipse') {
|
||||
// Calculate ellipse radius at angle
|
||||
var angle = point.getAngleInRadians(),
|
||||
x = width * Math.sin(angle),
|
||||
y = height * Math.cos(angle);
|
||||
radius = width * height / (2 * Math.sqrt(x * x + y * y));
|
||||
} else {
|
||||
// Average half of width & height for radius...
|
||||
radius = (width + height) / 4;
|
||||
}
|
||||
if (2 * Math.abs(point.getLength() - radius) <= strokeWidth)
|
||||
return new HitResult('stroke', this);
|
||||
}
|
||||
}
|
||||
return _hitTest.base.apply(this, arguments);
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in a new issue