From 4c2e1ee5c9939edf02bd2f511b4986a2b712ed1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 17 Feb 2011 22:58:56 +0000 Subject: [PATCH] Optimise and simplfy CompoundPath code, by moving scope into extend() call, and only compiling one fields object inside it that is return at the end. --- src/path/CompoundPath.js | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/path/CompoundPath.js b/src/path/CompoundPath.js index 50068b72..7e501eaa 100644 --- a/src/path/CompoundPath.js +++ b/src/path/CompoundPath.js @@ -1,5 +1,5 @@ -new function() { - +CompoundPath = PathItem.extend(new function() { + function getCurrentPath(compoundPath) { if (compoundPath.children.length) { return compoundPath.children[compoundPath.children.length - 1]; @@ -8,7 +8,7 @@ new function() { } } - CompoundPath = PathItem.extend({ + var fields = { initialize: function(items) { this.base(); this.children = []; @@ -56,38 +56,36 @@ new function() { this.children[i].smooth(); } }, - + moveTo: function() { var path = new Path(); this.appendTop(path); path.moveTo.apply(path, arguments); }, - + moveBy: function() { if (!arguments.length) { + // TODO: Shouldn't this be relative to the previous position + // in lack of an argument? This should then be corrected in + // Scriptographer too. this.moveTo(0, 0); } else { var point = Point.read(arguments); - var curPath = this.getCurrentPath(this); + var curPath = getCurrentPath(this); var current = curPath.segments[curPath.segments.length - 1].point; this.moveTo(current.add(point)); } } - }); + }; - var keys = ['lineTo', 'cubicCurveTo', 'curveTo', 'quadraticCurveTo', - 'arcTo', 'lineBy', 'curveBy', 'arcBy']; - var props = {}; - - function addProp(key) { - props[key] = function() { - var curPath = getCurrentPath(this); - curPath[key].apply(curPath, arguments); + var keys = ['lineTo', 'cubicCurveTo', 'quadraticCurveTo', 'curveTo', + 'arcTo', 'lineBy', 'curveBy', 'arcBy']; + for (var i = 0, l = keys.length; i < l; i++) { + fields[keys[i]] = function() { + var path = getCurrentPath(this); + path[keys[i]].apply(path, arguments); }; } - for (var i = 0, l = keys.length; i < l; i++) { - addProp(keys[i]); - } - CompoundPath.inject(props); + return fields; }; \ No newline at end of file