mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2024-12-29 09:22:22 -05:00
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:
parent
e6dc189310
commit
4c2e1ee5c9
1 changed files with 17 additions and 19 deletions
|
@ -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;
|
||||
};
|
Loading…
Reference in a new issue