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,4 +1,4 @@
new function() {
CompoundPath = PathItem.extend(new function() {
function getCurrentPath(compoundPath) {
if (compoundPath.children.length) {
@ -8,7 +8,7 @@ new function() {
}
}
CompoundPath = PathItem.extend({
var fields = {
initialize: function(items) {
this.base();
this.children = [];
@ -65,29 +65,27 @@ new function() {
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',
var keys = ['lineTo', 'cubicCurveTo', 'quadraticCurveTo', 'curveTo',
'arcTo', 'lineBy', 'curveBy', 'arcBy'];
var props = {};
function addProp(key) {
props[key] = function() {
var curPath = getCurrentPath(this);
curPath[key].apply(curPath, arguments);
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;
};