2011-02-17 17:58:56 -05:00
|
|
|
CompoundPath = PathItem.extend(new function() {
|
|
|
|
|
2011-02-17 08:33:25 -05:00
|
|
|
function getCurrentPath(compoundPath) {
|
|
|
|
if (compoundPath.children.length) {
|
|
|
|
return compoundPath.children[compoundPath.children.length - 1];
|
|
|
|
} else {
|
|
|
|
throw Error('Use a moveTo() command first');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-17 17:58:56 -05:00
|
|
|
var fields = {
|
2011-02-17 07:36:40 -05:00
|
|
|
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();
|
2011-02-17 08:33:25 -05:00
|
|
|
for (var i = 0, l = this.children.length; i < l; i++) {
|
2011-02-17 07:36:40 -05:00
|
|
|
var child = this.children[i];
|
|
|
|
child.draw(ctx, true);
|
|
|
|
}
|
|
|
|
firstChild.setCtxStyles(ctx);
|
|
|
|
if (firstChild.fillColor) ctx.fill();
|
|
|
|
if (firstChild.strokeColor) ctx.stroke();
|
|
|
|
}
|
|
|
|
},
|
2011-02-17 08:33:25 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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++) {
|
2011-02-17 08:33:25 -05:00
|
|
|
this.children[i].smooth();
|
|
|
|
}
|
|
|
|
},
|
2011-02-17 17:58:56 -05:00
|
|
|
|
2011-02-17 07:36:40 -05:00
|
|
|
moveTo: function() {
|
|
|
|
var path = new Path();
|
|
|
|
this.appendTop(path);
|
|
|
|
path.moveTo.apply(path, arguments);
|
|
|
|
},
|
2011-02-17 17:58:56 -05:00
|
|
|
|
2011-02-17 07:36:40 -05:00
|
|
|
moveBy: function() {
|
2011-02-17 08:33:25 -05:00
|
|
|
if (!arguments.length) {
|
2011-02-17 17:58:56 -05:00
|
|
|
// TODO: Shouldn't this be relative to the previous position
|
|
|
|
// in lack of an argument? This should then be corrected in
|
|
|
|
// Scriptographer too.
|
2011-02-17 07:36:40 -05:00
|
|
|
this.moveTo(0, 0);
|
|
|
|
} else {
|
|
|
|
var point = Point.read(arguments);
|
2011-02-17 17:58:56 -05:00
|
|
|
var curPath = getCurrentPath(this);
|
2011-02-17 07:36:40 -05:00
|
|
|
var current = curPath.segments[curPath.segments.length - 1].point;
|
|
|
|
this.moveTo(current.add(point));
|
|
|
|
}
|
|
|
|
}
|
2011-02-17 17:58:56 -05:00
|
|
|
};
|
2011-02-17 07:36:40 -05:00
|
|
|
|
2011-02-17 17:58:56 -05:00
|
|
|
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);
|
2011-02-17 07:36:40 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-02-17 17:58:56 -05:00
|
|
|
return fields;
|
2011-02-17 07:36:40 -05:00
|
|
|
};
|