paper.js/src/path/CompoundPath.js

91 lines
2.2 KiB
JavaScript
Raw Normal View History

CompoundPath = PathItem.extend(new function() {
function getCurrentPath(compoundPath) {
if (compoundPath.children.length) {
return compoundPath.children[compoundPath.children.length - 1];
} else {
throw Error('Use a moveTo() command first');
}
}
var fields = {
initialize: function(items) {
this.base();
this.children = [];
if (items) {
for (var i = 0, l = items.length; i < l; i++) {
this.appendTop(items[i]);
}
}
},
draw: function(ctx) {
if (this.children.length) {
var firstChild = this.children[0];
// if (!child.visible) return;
ctx.beginPath();
for (var i = 0, l = this.children.length; i < l; i++) {
var child = this.children[i];
child.draw(ctx, true);
}
firstChild.setCtxStyles(ctx);
if (firstChild.fillColor) ctx.fill();
if (firstChild.strokeColor) ctx.stroke();
}
},
/**
* If this is a compound path with only one path inside,
* the path is moved outside and the compound path is erased.
* Otherwise, the compound path is returned unmodified.
*
* @return the simplified compound path.
*/
simplify: function() {
if(this.children.length == 1) {
var child = this.children[0];
child.moveAbove(this);
this.remove();
return child;
}
return this;
},
smooth: function() {
2011-02-17 10:00:03 -05:00
for (var i = 0, l = this.children.length; i < l; i++) {
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 = getCurrentPath(this);
var current = curPath.segments[curPath.segments.length - 1].point;
this.moveTo(current.add(point));
}
}
};
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);
};
}
return fields;
};