mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-07-27 22:29:11 -04:00
Implement constructors for Path and CompoundPath that can handle SVG-style path-data.
This commit is contained in:
parent
41fa3b24e7
commit
a50adb1020
2 changed files with 69 additions and 5 deletions
src/path
|
@ -49,12 +49,56 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
|
||||||
* // Move the inner circle 5pt to the right:
|
* // Move the inner circle 5pt to the right:
|
||||||
* compoundPath.children[1].position.x += 5;
|
* compoundPath.children[1].position.x += 5;
|
||||||
*/
|
*/
|
||||||
|
/**
|
||||||
|
* Creates a new compound path item from an object description and places it
|
||||||
|
* at the top of the active layer.
|
||||||
|
*
|
||||||
|
* @name CompoundPath#initialize
|
||||||
|
* @param {Object} object an object literal containing properties to
|
||||||
|
* be set on the path
|
||||||
|
* @return {CompoundPath} the newly created path
|
||||||
|
*
|
||||||
|
* @example {@paperscript}
|
||||||
|
* var path = new CompoundPath({
|
||||||
|
* children: [
|
||||||
|
* new Path.Circle({
|
||||||
|
* center: new Point(50, 50),
|
||||||
|
* radius: 30
|
||||||
|
* }),
|
||||||
|
* new Path.Circle({
|
||||||
|
* center: new Point(50, 50),
|
||||||
|
* radius: 10
|
||||||
|
* })
|
||||||
|
* ],
|
||||||
|
* fillColor: 'black',
|
||||||
|
* selected: true
|
||||||
|
* });
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Creates a new compound path item from SVG path-data and places it at the
|
||||||
|
* top of the active layer.
|
||||||
|
*
|
||||||
|
* @name CompoundPath#initialize
|
||||||
|
* @param {String} pathData the SVG path-data that describes the geometry
|
||||||
|
* of this path.
|
||||||
|
* @return {CompoundPath} the newly created path
|
||||||
|
*
|
||||||
|
* @example {@paperscript}
|
||||||
|
* var pathData = 'M20,50c0,-16.56854 13.43146,-30 30,-30c16.56854,0 30,13.43146 30,30c0,16.56854 -13.43146,30 -30,30c-16.56854,0 -30,-13.43146 -30,-30z M50,60c5.52285,0 10,-4.47715 10,-10c0,-5.52285 -4.47715,-10 -10,-10c-5.52285,0 -10,4.47715 -10,10c0,5.52285 4.47715,10 10,10z';
|
||||||
|
* var path = new CompoundPath(pathData);
|
||||||
|
* path.fillColor = 'black';
|
||||||
|
*/
|
||||||
initialize: function CompoundPath(arg) {
|
initialize: function CompoundPath(arg) {
|
||||||
// CompoundPath has children and supports named children.
|
// CompoundPath has children and supports named children.
|
||||||
this._children = [];
|
this._children = [];
|
||||||
this._namedChildren = {};
|
this._namedChildren = {};
|
||||||
if (!this._initialize(arg))
|
if (!this._initialize(arg)) {
|
||||||
this.addChildren(Array.isArray(arg) ? arg : arguments);
|
if (typeof arg === 'string') {
|
||||||
|
this.setPathData(arg);
|
||||||
|
} else {
|
||||||
|
this.addChildren(Array.isArray(arg) ? arg : arguments);
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_changed: function _changed(flags) {
|
_changed: function _changed(flags) {
|
||||||
|
|
|
@ -47,7 +47,8 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
* path.strokeColor = 'black';
|
* path.strokeColor = 'black';
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
* Creates a new path item and places it at the top of the active layer.
|
* Creates a new path item from an object description and places it at the
|
||||||
|
* top of the active layer.
|
||||||
*
|
*
|
||||||
* @name Path#initialize
|
* @name Path#initialize
|
||||||
* @param {Object} object an object literal containing properties to
|
* @param {Object} object an object literal containing properties to
|
||||||
|
@ -70,6 +71,20 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
* selected: true
|
* selected: true
|
||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
|
/**
|
||||||
|
* Creates a new path item from SVG path-data and places it at the top of
|
||||||
|
* the active layer.
|
||||||
|
*
|
||||||
|
* @name Path#initialize
|
||||||
|
* @param {String} pathData the SVG path-data that describes the geometry
|
||||||
|
* of this path.
|
||||||
|
* @return {Path} the newly created path
|
||||||
|
*
|
||||||
|
* @example {@paperscript}
|
||||||
|
* var pathData = 'M100,50c0,27.614-22.386,50-50,50S0,77.614,0,50S22.386,0,50,0S100,22.386,100,50';
|
||||||
|
* var path = new Path(pathData);
|
||||||
|
* path.fillColor = 'red';
|
||||||
|
*/
|
||||||
initialize: function Path(arg) {
|
initialize: function Path(arg) {
|
||||||
this._closed = false;
|
this._closed = false;
|
||||||
this._segments = [];
|
this._segments = [];
|
||||||
|
@ -86,12 +101,17 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
: arguments
|
: arguments
|
||||||
// See if it behaves like a segment or a point, but filter out
|
// See if it behaves like a segment or a point, but filter out
|
||||||
// rectangles, as accepted by some Path.Constructor constructors.
|
// rectangles, as accepted by some Path.Constructor constructors.
|
||||||
: arg && (arg.point !== undefined && arg.size === undefined
|
: arg && (arg.size === undefined && (arg.x !== undefined
|
||||||
|| arg.x !== undefined)
|
|| arg.point !== undefined))
|
||||||
? arguments
|
? arguments
|
||||||
: null;
|
: null;
|
||||||
// Always call setSegments() to initialize a few related variables.
|
// Always call setSegments() to initialize a few related variables.
|
||||||
this.setSegments(segments || []);
|
this.setSegments(segments || []);
|
||||||
|
if (!segments && typeof arg === 'string') {
|
||||||
|
this.setPathData(arg);
|
||||||
|
// Erase for _initialize() call below.
|
||||||
|
arg = null;
|
||||||
|
}
|
||||||
// Only pass on arg as props if it wasn't consumed for segments already.
|
// Only pass on arg as props if it wasn't consumed for segments already.
|
||||||
this._initialize(!segments && arg);
|
this._initialize(!segments && arg);
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue