2011-03-06 19:50:44 -05:00
|
|
|
/*
|
|
|
|
* Paper.js
|
|
|
|
*
|
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-03-04 08:34:31 -05:00
|
|
|
var CompoundPath = this.CompoundPath = PathItem.extend({
|
2011-05-15 13:05:47 -04:00
|
|
|
initialize: function(paths) {
|
2011-03-03 07:23:46 -05:00
|
|
|
this.base();
|
2011-05-14 13:07:10 -04:00
|
|
|
this._children = [];
|
2011-05-15 14:14:09 -04:00
|
|
|
// Do not reassign to paths, since arguments would get modified, which
|
|
|
|
// we potentially use as array, depending on what is passed.
|
|
|
|
var items = !paths || !Array.isArray(paths)
|
|
|
|
|| typeof paths[0] !== 'object' ? arguments : paths;
|
|
|
|
for (var i = 0, l = items.length; i < l; i++) {
|
|
|
|
var path = items[i];
|
|
|
|
// All paths except for the top one (last one in list) are set to
|
|
|
|
// clockwise orientation when creating a compound path, so that they
|
|
|
|
// appear as holes, but only if their orientation was not already
|
|
|
|
// specified before (= _clockwise is defined).
|
|
|
|
if (path._clockwise === undefined)
|
|
|
|
path.setClockwise(i < l - 1);
|
|
|
|
this.appendTop(path);
|
2011-03-03 07:23:46 -05:00
|
|
|
}
|
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-03-03 07:23:46 -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() {
|
2011-05-14 13:07:10 -04:00
|
|
|
if (this._children.length == 1) {
|
|
|
|
var child = this._children[0];
|
2011-03-03 07:23:46 -05:00
|
|
|
child.moveAbove(this);
|
|
|
|
this.remove();
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-03-03 07:23:46 -05:00
|
|
|
smooth: function() {
|
2011-05-14 13:07:10 -04:00
|
|
|
for (var i = 0, l = this._children.length; i < l; i++)
|
|
|
|
this._children[i].smooth();
|
2011-03-03 07:23:46 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
draw: function(ctx, param) {
|
2011-05-14 13:07:10 -04:00
|
|
|
var firstChild = this._children[0];
|
2011-03-03 07:23:46 -05:00
|
|
|
ctx.beginPath();
|
|
|
|
param.compound = true;
|
2011-05-14 13:07:10 -04:00
|
|
|
for (var i = 0, l = this._children.length; i < l; i++)
|
|
|
|
Item.draw(this._children[i], ctx, param);
|
2011-04-28 08:04:12 -04:00
|
|
|
firstChild._setStyles(ctx);
|
2011-03-04 20:36:27 -05:00
|
|
|
var fillColor = firstChild.getFillColor(),
|
|
|
|
strokeColor = firstChild.getStrokeColor();
|
|
|
|
if (fillColor) {
|
2011-05-12 09:30:56 -04:00
|
|
|
ctx.fillStyle = fillColor.getCanvasStyle(ctx);
|
2011-03-03 07:23:46 -05:00
|
|
|
ctx.fill();
|
|
|
|
}
|
2011-03-04 20:36:27 -05:00
|
|
|
if (strokeColor) {
|
2011-05-12 09:30:56 -04:00
|
|
|
ctx.strokeStyle = strokeColor.getCanvasStyle(ctx);
|
2011-03-03 07:23:46 -05:00
|
|
|
ctx.stroke();
|
|
|
|
}
|
2011-05-14 14:36:58 -04:00
|
|
|
param.compound = false;
|
2011-03-03 07:23:46 -05:00
|
|
|
}
|
2011-05-07 12:11:06 -04:00
|
|
|
}, new function() { // Injection scope for PostScript-like drawing functions
|
2011-03-03 07:25:41 -05:00
|
|
|
function getCurrentPath(that) {
|
2011-05-15 14:31:25 -04:00
|
|
|
if (!that._children.length)
|
2011-03-03 12:29:40 -05:00
|
|
|
throw new Error('Use a moveTo() command first');
|
2011-05-15 14:31:25 -04:00
|
|
|
return that._children[that._children.length - 1];
|
2011-02-17 08:33:25 -05:00
|
|
|
}
|
2011-02-17 17:58:56 -05:00
|
|
|
|
2011-03-03 07:23:46 -05:00
|
|
|
var fields = {
|
2011-05-15 14:57:48 -04:00
|
|
|
moveTo: function(point) {
|
2011-05-07 12:11:06 -04:00
|
|
|
var path = new Path();
|
|
|
|
this.appendTop(path);
|
|
|
|
path.moveTo.apply(path, arguments);
|
|
|
|
},
|
|
|
|
|
2011-02-17 07:36:40 -05:00
|
|
|
moveBy: function() {
|
2011-04-21 15:12:48 -04:00
|
|
|
var point = arguments.length ? Point.read(arguments) : new Point(),
|
|
|
|
path = getCurrentPath(this),
|
|
|
|
current = path.segments[path.segments.length - 1]._point;
|
2011-02-24 11:13:41 -05:00
|
|
|
this.moveTo(current.add(point));
|
2011-02-17 18:01:18 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
closePath: function() {
|
2011-04-30 18:22:29 -04:00
|
|
|
getCurrentPath(this).setClosed(true);
|
2011-02-17 07:36:40 -05:00
|
|
|
}
|
2011-02-17 17:58:56 -05:00
|
|
|
};
|
2011-02-17 18:01:18 -05:00
|
|
|
|
2011-05-07 12:11:06 -04:00
|
|
|
// Redirect all other drawing commands to the current path
|
2011-02-17 18:34:03 -05:00
|
|
|
Base.each(['lineTo', 'cubicCurveTo', 'quadraticCurveTo', 'curveTo',
|
|
|
|
'arcTo', 'lineBy', 'curveBy', 'arcBy'], function(key) {
|
|
|
|
fields[key] = function() {
|
2011-02-17 17:58:56 -05:00
|
|
|
var path = getCurrentPath(this);
|
2011-02-17 18:34:03 -05:00
|
|
|
path[key].apply(path, arguments);
|
2011-02-17 07:36:40 -05:00
|
|
|
};
|
2011-02-17 18:34:03 -05:00
|
|
|
});
|
2011-02-17 07:36:40 -05:00
|
|
|
|
2011-02-17 17:58:56 -05:00
|
|
|
return fields;
|
2011-03-03 07:19:43 -05:00
|
|
|
});
|