mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-03-15 09:19:55 -04:00
Implement hit-test options: center and bounds.
This commit is contained in:
parent
c033a5a7bc
commit
62ba983432
3 changed files with 30 additions and 5 deletions
|
@ -393,7 +393,7 @@ var Point = this.Point = Base.extend(/** @lends Point# */{
|
||||||
* @return {Point} the transformed point
|
* @return {Point} the transformed point
|
||||||
*/
|
*/
|
||||||
transform: function(matrix) {
|
transform: function(matrix) {
|
||||||
return matrix._transformPoint(this);
|
return matrix ? matrix._transformPoint(this) : this;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -677,6 +677,33 @@ var Item = this.Item = Base.extend(/** @lends Item# */{
|
||||||
if (!this._children && !this.getRoughBounds(matrix)
|
if (!this._children && !this.getRoughBounds(matrix)
|
||||||
.expand(options.tolerance).contains(point))
|
.expand(options.tolerance).contains(point))
|
||||||
return null;
|
return null;
|
||||||
|
if (options.center || options.bounds) {
|
||||||
|
// Don't get the transformed bounds, check against transformed
|
||||||
|
// points instead
|
||||||
|
var bounds = this.getBounds(),
|
||||||
|
that = this,
|
||||||
|
// TODO: Move these into a private scope
|
||||||
|
points = ['TopLeft', 'TopRight', 'BottomLeft', 'BottomRight',
|
||||||
|
'LeftCenter', 'TopCenter', 'RightCenter', 'BottomCenter'],
|
||||||
|
res;
|
||||||
|
function checkBounds(part) {
|
||||||
|
var pt = bounds['get' + part]().transform(matrix);
|
||||||
|
if (point.getDistance(pt) < options.tolerance)
|
||||||
|
return new HitResult(Base.hyphenate(part), that,
|
||||||
|
{ point: pt });
|
||||||
|
}
|
||||||
|
// Push the getter name parts onto _parts for the bounds properties
|
||||||
|
// that we are supposed to test. This is performed only once, even
|
||||||
|
// when testing many items.
|
||||||
|
if (options.center && (res = checkBounds('Center')))
|
||||||
|
return res;
|
||||||
|
if (options.bounds) {
|
||||||
|
for (var i = 0; i < 8; i++)
|
||||||
|
if (res = checkBounds(points[i]))
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Support option.type even for things like CompoundPath where
|
// TODO: Support option.type even for things like CompoundPath where
|
||||||
// children are matched but the parent is returned.
|
// children are matched but the parent is returned.
|
||||||
|
|
||||||
|
@ -691,8 +718,7 @@ var Item = this.Item = Base.extend(/** @lends Item# */{
|
||||||
// Loop backwards, so items that get drawn last are tested first
|
// Loop backwards, so items that get drawn last are tested first
|
||||||
for (var i = this._children.length - 1; i >= 0; i--) {
|
for (var i = this._children.length - 1; i >= 0; i--) {
|
||||||
var res = this._children[i].hitTest(point, options, matrix);
|
var res = this._children[i].hitTest(point, options, matrix);
|
||||||
if (res)
|
if (res) return res;
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -187,8 +187,7 @@ var Project = this.Project = Base.extend(/** @lends Project# */{
|
||||||
// Loop backwards, so layers that get drawn last are tested first
|
// Loop backwards, so layers that get drawn last are tested first
|
||||||
for (var i = this.layers.length - 1; i >= 0; i--) {
|
for (var i = this.layers.length - 1; i >= 0; i--) {
|
||||||
var res = this.layers[i].hitTest(point, options);
|
var res = this.layers[i].hitTest(point, options);
|
||||||
if (res)
|
if (res) return res;
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue