Split Item#hitTest() into separate private function that does need to do arguments reading, for improved performance also when iterating over children.

This commit is contained in:
Jürg Lehni 2014-04-29 20:06:48 +02:00
parent fce31599a5
commit 8317aacc1b
8 changed files with 22 additions and 22 deletions

View file

@ -282,7 +282,7 @@ Base.exports.PaperScript = (function() {
// end of the code execution, so we can retrieve their values from the
// function call.
handlers = Base.each(handlers, function(key) {
// Check for each handler explicitely and only return them if they
// Check for each handler explicitly and only return them if they
// seem to exist.
if (new RegExp('\\s+' + key + '\\b').test(code)) {
params.push(key);

View file

@ -106,9 +106,7 @@ var HitResult = Base.extend(/** @lends HitResult# */{
* @private
*/
getOptions: function(options) {
// Use _merged property to not repeatetly merge using new Base in
// recursion.
return options && options._merged ? options : new Base({
return new Base({
// Type of item, for instanceof check: Group, Layer, Path,
// CompoundPath, Shape, Raster, PlacedSymbol, ...
type: null,
@ -129,14 +127,12 @@ var HitResult = Base.extend(/** @lends HitResult# */{
ends: false,
// Hit test the center of the bounds
center: false,
// Hit test the corners and side-centers of the boudning box
// Hit test the corners and side-centers of the bounding box
bounds: false,
// Hit items that are marked as guides
guides: false,
// Only hit selected objects
selected: false,
// Mark as merged
_merged: true
selected: false
}, options);
}
}

View file

@ -1671,9 +1671,13 @@ var Item = Base.extend(Callback, /** @lends Item# */{
* information about what exactly was hit or {@code null} if nothing was
* hit
*/
hitTest: function(point, options) {
point = Point.read(arguments);
options = HitResult.getOptions(Base.read(arguments));
hitTest: function(/* point, options */) {
return this._hitTest(
Point.read(arguments),
HitResult.getOptions(Base.read(arguments)));
},
_hitTest: function(point, options) {
if (this._locked || !this._visible || this._guide && !options.guides
|| this.isEmpty())
return null;
@ -1747,10 +1751,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{
var opts = this._getChildHitTestOptions(options);
// Loop backwards, so items that get drawn last are tested first
for (var i = children.length - 1; i >= 0 && !res; i--)
res = children[i].hitTest(point, opts);
res = children[i]._hitTest(point, opts);
}
if (!res && checkSelf)
res = this._hitTest(point, options);
res = this._hitTestSelf(point, options);
// Transform the point back to the outer coordinate system.
if (res && res.point)
res.point = matrix.transform(res.point);
@ -1764,7 +1768,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{
return options;
},
_hitTest: function(point, options) {
_hitTestSelf: function(point, options) {
// The default implementation honly handles 'fill' through #_contains()
if (options.fill && this.hasFill() && this._contains(point))
return new HitResult('fill', this);

View file

@ -115,8 +115,8 @@ var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{
cacheItem);
},
_hitTest: function(point, options) {
var res = this._symbol._definition.hitTest(point, options);
_hitTestSelf: function(point, options) {
var res = this._symbol._definition._hitTest(point, options);
// TODO: When the symbol's definition is a path, should hitResult
// contain information like HitResult#curve?
if (res)

View file

@ -618,7 +618,7 @@ var Raster = Item.extend(/** @lends Raster# */{
return matrix ? matrix._transformBounds(rect) : rect;
},
_hitTest: function(point) {
_hitTestSelf: function(point) {
if (this._contains(point)) {
var that = this;
return new HitResult('pixel', that, {

View file

@ -250,7 +250,7 @@ var Shape = Item.extend(/** @lends Shape# */{
return matrix ? matrix._transformBounds(rect) : rect;
}
},
new function() { // Scope for _contains() and _hitTest() code.
new function() { // Scope for _contains() and _hitTestSelf() code.
// Returns the center of the quarter corner ellipse for rounded rectangle,
// if the point lies within its bounding box.
@ -296,7 +296,7 @@ new function() { // Scope for _contains() and _hitTest() code.
}
},
_hitTest: function _hitTest(point, options) {
_hitTestSelf: function _hitTestSelf(point, options) {
var hit = false;
if (this.hasStroke()) {
var type = this._type,
@ -326,7 +326,7 @@ new function() { // Scope for _contains() and _hitTest() code.
}
return hit
? new HitResult('stroke', this)
: _hitTest.base.apply(this, arguments);
: _hitTestSelf.base.apply(this, arguments);
}
};
}, {

View file

@ -1409,7 +1409,7 @@ var Path = PathItem.extend(/** @lends Path# */{
return null;
},
_hitTest: function(point, options) {
_hitTestSelf: function(point, options) {
var that = this,
style = this.getStyle(),
segments = this._segments,

View file

@ -307,7 +307,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
options = HitResult.getOptions(Base.read(arguments));
// Loop backwards, so layers that get drawn last are tested first
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) return res;
}
return null;