From dc35fdbd02fcf93b98cfdc65d75236f0e06399cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Fri, 8 Feb 2013 18:23:33 -0800 Subject: [PATCH] Move named inner functions out of nested scopes, to prepare for 'use strict;' transition. --- src/browser/DomElement.js | 1 - src/core/Base.js | 13 ++++----- src/item/Item.js | 17 ++++++------ src/path/Path.js | 29 ++++++++++----------- src/project/Project.js | 55 ++++++++++++++++++++------------------- 5 files changed, 58 insertions(+), 57 deletions(-) diff --git a/src/browser/DomElement.js b/src/browser/DomElement.js index c14fa2d1..a1121b95 100644 --- a/src/browser/DomElement.js +++ b/src/browser/DomElement.js @@ -16,7 +16,6 @@ * @private */ var DomElement = new function() { - // We use a mix of Bootstrap.js legacy and Bonzo.js magic, ported over and // furhter simplified to a subset actually required by Paper.js diff --git a/src/core/Base.js b/src/core/Base.js index b6fc8c34..d182ae3b 100644 --- a/src/core/Base.js +++ b/src/core/Base.js @@ -69,6 +69,13 @@ this.Base = Base.inject(/** @lends Base# */{ * arrays and properties of objects. */ equals: function(obj1, obj2) { + function checkKeys(o1, o2) { + for (var i in o1) + if (o1.hasOwnProperty(i) && typeof o2[i] === 'undefined') + return false; + return true; + } + if (obj1 == obj2) return true; // Call #equals() on both obj1 and obj2 @@ -88,12 +95,6 @@ this.Base = Base.inject(/** @lends Base# */{ } // Compare objects if (typeof obj1 === 'object' && typeof obj2 === 'object') { - function checkKeys(o1, o2) { - for (var i in o1) - if (o1.hasOwnProperty(i) && typeof o2[i] === 'undefined') - return false; - return true; - } if (!checkKeys(obj1, obj2) || !checkKeys(obj2, obj1)) return false; for (var i in obj1) { diff --git a/src/item/Item.js b/src/item/Item.js index f05f5589..a76b07f9 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1131,6 +1131,15 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{ * hit. */ hitTest: function(point, options) { + function checkBounds(type, part) { + var pt = bounds['get' + part](); + // TODO: We need to transform the point back to the coordinate + // system of the DOM level on which the inquiry was started! + if (point.getDistance(pt) < options.tolerance) + return new HitResult(type, that, + { name: Base.hyphenate(part), point: pt }); + } + point = Point.read(arguments); options = HitResult.getOptions(Base.read(arguments)); // Check if the point is withing roughBounds + tolerance, but only if @@ -1153,14 +1162,6 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{ points = ['TopLeft', 'TopRight', 'BottomLeft', 'BottomRight', 'LeftCenter', 'TopCenter', 'RightCenter', 'BottomCenter'], res; - function checkBounds(type, part) { - var pt = bounds['get' + part](); - // TODO: We need to transform the point back to the coordinate - // system of the DOM level on which the inquiry was started! - if (point.getDistance(pt) < options.tolerance) - return new HitResult(type, that, - { name: Base.hyphenate(part), point: pt }); - } if (options.center && (res = checkBounds('center', 'Center'))) return res; if (options.bounds) { diff --git a/src/path/Path.js b/src/path/Path.js index 50cd86b5..8b5a4c0f 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1442,6 +1442,20 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ // performance. function drawHandles(ctx, segments, matrix) { + function drawHandle(index) { + var hX = coords[index], + hY = coords[index + 1]; + if (pX != hX || pY != hY) { + ctx.beginPath(); + ctx.moveTo(pX, pY); + ctx.lineTo(hX, hY); + ctx.stroke(); + ctx.beginPath(); + ctx.arc(hX, hY, 1.75, 0, Math.PI * 2, true); + ctx.fill(); + } + } + var coords = new Array(6); for (var i = 0, l = segments.length; i < l; i++) { var segment = segments[i]; @@ -1450,21 +1464,6 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{ selected = state & /*#=*/ SelectionState.POINT, pX = coords[0], pY = coords[1]; - - function drawHandle(index) { - var hX = coords[index], - hY = coords[index + 1]; - if (pX != hX || pY != hY) { - ctx.beginPath(); - ctx.moveTo(pX, pY); - ctx.lineTo(hX, hY); - ctx.stroke(); - ctx.beginPath(); - ctx.arc(hX, hY, 1.75, 0, Math.PI * 2, true); - ctx.fill(); - } - } - if (selected || (state & /*#=*/ SelectionState.HANDLE_IN)) drawHandle(2); if (selected || (state & /*#=*/ SelectionState.HANDLE_OUT)) diff --git a/src/project/Project.js b/src/project/Project.js index 16e37eb3..3a77ac53 100644 --- a/src/project/Project.js +++ b/src/project/Project.js @@ -265,6 +265,34 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{ */ draw: function(ctx, matrix) { + // Create a local lookup table for hierarchically concatenated matrices + // by item id, to speed up drawing by eliminating repeated concatenation + // of parent's matrices through caching. + var matrices = {}; + // Description of the paramters to getGlobalMatrix(): + // mx is the container for the final concatenated matrix, passed to + // getGlobalMatrix() on the initial call. + // cached defines wether the result of the concatenation should be + // cached, only used for parents of items that this is called for. + function getGlobalMatrix(item, mx, cached) { + var cache = cached && matrices[item._id]; + // Found a cached version? Return a clone of it. + if (cache) + return cache.clone(); + // Get concatenated matrix from all the parents, using + // local caching (passing true for cached): + if (item._parent) + mx = getGlobalMatrix(item._parent, mx, true); + // No need to concatenate if it's the identity matrix + if (!item._matrix.isIdentity()) + mx.concatenate(item._matrix); + // If the result needs to be cached, create a copy since matrix + // might be further modified through recursive calls + if (cached) + matrices[item._id] = mx.clone(); + return mx; + } + this._drawCount++; ctx.save(); if (!matrix.isIdentity()) @@ -280,33 +308,6 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{ ctx.strokeWidth = 1; // TODO: use Layer#color ctx.strokeStyle = ctx.fillStyle = '#009dec'; - // Create a local lookup table for hierarchically concatenated - // matrices by item id, to speed up drawing by eliminating repeated - // concatenation of parent's matrices through caching. - var matrices = {}; - // Description of the paramters to getGlobalMatrix(): - // mx is the container for the final concatenated matrix, passed - // to getGlobalMatrix() on the initial call. - // cached defines wether the result of the concatenation should be - // cached, only used for parents of items that this is called for. - function getGlobalMatrix(item, mx, cached) { - var cache = cached && matrices[item._id]; - // Found a cached version? Return a clone of it. - if (cache) - return cache.clone(); - // Get concatenated matrix from all the parents, using - // local caching (passing true for cached): - if (item._parent) - mx = getGlobalMatrix(item._parent, mx, true); - // No need to concatenate if it's the identity matrix - if (!item._matrix.isIdentity()) - mx.concatenate(item._matrix); - // If the result needs to be cached, create a copy since matrix - // might be further modified through recursive calls - if (cached) - matrices[item._id] = mx.clone(); - return mx; - } for (var id in this._selectedItems) { var item = this._selectedItems[id]; if (item._drawCount === this._drawCount)