Improve documentation for Path#split(), Curve#divide() and Curve#split().

Closes #189.
This commit is contained in:
Jürg Lehni 2013-06-27 17:18:57 -07:00
parent 9f5b543ab5
commit f9925a71ea
2 changed files with 52 additions and 22 deletions

View file

@ -313,20 +313,39 @@ var Curve = Base.extend(/** @lends Curve# */{
},
/**
* Divides the curve into two at the specified position. The curve itself is
* modified and becomes the first part, the second part is returned as a new
* curve. If the modified curve belongs to a path item, the second part is
* added to it.
* Private method that handles all types of offset / isParameter pairs and
* converts it to a curve parameter.
*/
_getParameter: function(offset, isParameter) {
return isParameter
? offset
// Accept CurveLocation objects, and objects that act like
// them:
: offset && offset.curve === this
? offset.parameter
: offset === undefined && isParameter === undefined
? 0.5 // default is in the middle
: this.getParameterAt(offset, 0);
},
/**
* Divides the curve into two curves at the given offset. The curve itself
* is modified and becomes the first part, the second part is returned as a
* new curve. If the modified curve belongs to a path item, the second part
* is also added to the path.
*
* @param parameter the position at which to split the curve as a value
* between 0 and 1 {@default 0.5}
* @name Curve#divide
* @function
* @param {Number} offset the offset on the curve at which to split, or the
* curve time parameter if {@code isParameter} is {@code true}
* {@default 0.5}
* @param {Boolean} [isParameter=false] pass {@code true} if {@code offset}
* is a curve time parameter.
* @return {Curve} the second part of the divided curve
*/
divide: function(parameter) {
var res = null;
// Accept CurveLocation objects, and objects that act like them:
if (parameter && parameter.curve === this)
parameter = parameter.parameter;
divide: function(offset, isParameter) {
var parameter = this._getParameter(offset, isParameter),
res = null;
if (parameter > 0 && parameter < 1) {
var parts = Curve.subdivide(this.getValues(), parameter),
isLinear = this.isLinear(),
@ -376,14 +395,24 @@ var Curve = Base.extend(/** @lends Curve# */{
},
/**
* Splits the path that this curve belongs to at the given parameter, using
* {@link Path#split(index, parameter)}.
* Splits the path this curve belongs to at the given offset. After
* splitting, the path will be open. If the path was open already, splitting
* will result in two paths.
*
* @return {Path} the second part of the split path
* @name Curve#split
* @function
* @param {Number} offset the offset on the curve at which to split, or the
* curve time parameter if {@code isParameter} is {@code true}
* {@default 0.5}
* @param {Boolean} [isParameter=false] pass {@code true} if {@code offset}
* is a curve time parameter.
* @return {Path} The newly created path after splitting, if any
* @see Path#split(index, parameter)
*/
split: function(parameter) {
split: function(offset, isParameter) {
return this._path
? this._path.split(this._segment1._index, parameter)
? this._path.split(this._segment1._index,
this._getParameter(offset, isParameter))
: null;
},

View file

@ -913,7 +913,7 @@ var Path = PathItem.extend(/** @lends Path# */{
* @function
* @param {Number} offset the offset at which to split the path
* as a number between 0 and {@link Path#length}
* @return {Path} The newly created path, if any.
* @return {Path} The newly created path after splitting, if any
*
* @example {@paperscript} // Splitting an open path
* var path = new Path();
@ -956,7 +956,7 @@ var Path = PathItem.extend(/** @lends Path# */{
* @function
* @param {CurveLocation} location the curve location at which to split
* the path
* @return {Path} The newly created path, if any.
* @return {Path} The newly created path after splitting, if any
*
* @example {@paperscript}
* var path = new Path.Circle({
@ -976,8 +976,9 @@ var Path = PathItem.extend(/** @lends Path# */{
* path.lastSegment.selected = true;
*/
/**
* Splits the path. After splitting, the path will be open. If the path
* was open already, splitting will result in two paths.
* Splits the path at the given curve index and parameter. After splitting,
* the path will be open. If the path was open already, splitting will
* result in two paths.
*
* @example {@paperscript} // Splitting an open path
* // Draw a V shaped path:
@ -1012,12 +1013,12 @@ var Path = PathItem.extend(/** @lends Path# */{
* @param {Number} index the index of the curve in the {@link Path#curves}
* array at which to split
* @param {Number} parameter the parameter at which the curve will be split
* @return {Path} The newly created path after splitting, if any.
* @return {Path} The newly created path after splitting, if any
*/
split: function(index, parameter) {
if (parameter === null)
return;
if (arguments.length == 1) {
if (arguments.length === 1) {
var arg = index;
// split(offset), convert offset to location
if (typeof arg === 'number')