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.

This commit is contained in:
Jürg Lehni 2011-02-17 22:58:56 +00:00
parent e6dc189310
commit 4c2e1ee5c9

View file

@ -1,5 +1,5 @@
new function() { CompoundPath = PathItem.extend(new function() {
function getCurrentPath(compoundPath) { function getCurrentPath(compoundPath) {
if (compoundPath.children.length) { if (compoundPath.children.length) {
return compoundPath.children[compoundPath.children.length - 1]; return compoundPath.children[compoundPath.children.length - 1];
@ -8,7 +8,7 @@ new function() {
} }
} }
CompoundPath = PathItem.extend({ var fields = {
initialize: function(items) { initialize: function(items) {
this.base(); this.base();
this.children = []; this.children = [];
@ -56,38 +56,36 @@ new function() {
this.children[i].smooth(); this.children[i].smooth();
} }
}, },
moveTo: function() { moveTo: function() {
var path = new Path(); var path = new Path();
this.appendTop(path); this.appendTop(path);
path.moveTo.apply(path, arguments); path.moveTo.apply(path, arguments);
}, },
moveBy: function() { moveBy: function() {
if (!arguments.length) { 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); this.moveTo(0, 0);
} else { } else {
var point = Point.read(arguments); var point = Point.read(arguments);
var curPath = this.getCurrentPath(this); var curPath = getCurrentPath(this);
var current = curPath.segments[curPath.segments.length - 1].point; var current = curPath.segments[curPath.segments.length - 1].point;
this.moveTo(current.add(point)); this.moveTo(current.add(point));
} }
} }
}); };
var keys = ['lineTo', 'cubicCurveTo', 'curveTo', 'quadraticCurveTo', var keys = ['lineTo', 'cubicCurveTo', 'quadraticCurveTo', 'curveTo',
'arcTo', 'lineBy', 'curveBy', 'arcBy']; 'arcTo', 'lineBy', 'curveBy', 'arcBy'];
var props = {}; for (var i = 0, l = keys.length; i < l; i++) {
fields[keys[i]] = function() {
function addProp(key) { var path = getCurrentPath(this);
props[key] = function() { path[keys[i]].apply(path, arguments);
var curPath = getCurrentPath(this);
curPath[key].apply(curPath, arguments);
}; };
} }
for (var i = 0, l = keys.length; i < l; i++) { return fields;
addProp(keys[i]);
}
CompoundPath.inject(props);
}; };