mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Implement Path#divideAt(), similar to Curve#divideAt()
This commit is contained in:
parent
d405f45d38
commit
aa2e1d753b
2 changed files with 49 additions and 21 deletions
|
@ -436,15 +436,16 @@ var Curve = Base.extend(/** @lends Curve# */{
|
||||||
// TODO: adjustThroughPoint
|
// TODO: adjustThroughPoint
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Divides the curve into two curves at the given offset. The curve itself
|
* Divides the curve into two curves at the given offset or location. The
|
||||||
* is modified and becomes the first part, the second part is returned as a
|
* curve itself is modified and becomes the first part, the second part is
|
||||||
* new curve. If the modified curve belongs to a path item, the second part
|
* returned as a new curve. If the curve belongs to a path item, a new
|
||||||
* is also added to the path.
|
* segment is inserted into the path at the given location, and the second
|
||||||
|
* part becomes a part of the path as well.
|
||||||
*
|
*
|
||||||
* @param {Number|CurveLocation} location the offset or location on the
|
* @param {Number|CurveLocation} location the offset or location on the
|
||||||
* curve at which to divide
|
* curve at which to divide
|
||||||
* @return {Curve} the second part of the divided curve, if the offset is
|
* @return {Curve} the second part of the divided curve if the location is
|
||||||
* within the valid range, {code null} otherwise.
|
* valid, {code null} otherwise
|
||||||
* @see #divideAtTime(time)
|
* @see #divideAtTime(time)
|
||||||
*/
|
*/
|
||||||
divideAt: function(location) {
|
divideAt: function(location) {
|
||||||
|
|
|
@ -962,6 +962,27 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
this.setSelected(true);
|
this.setSelected(true);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Divides the path on the curve at the given offset or location into two
|
||||||
|
* curves, by inserting a new segment at the given location.
|
||||||
|
*
|
||||||
|
* @param {Number|CurveLocation} location the offset or location on the
|
||||||
|
* path at which to divide the existing curve by inserting a new segment
|
||||||
|
* @return {Segment} the newly inserted segment if the location is valid,
|
||||||
|
* {code null} otherwise
|
||||||
|
* @see Curve#divideAt(location)
|
||||||
|
*/
|
||||||
|
divideAt: function(location) {
|
||||||
|
var loc = this.getLocationAt(location);
|
||||||
|
ret = null;
|
||||||
|
if (loc) {
|
||||||
|
var curve = loc.getCurve().divideAt(loc.getCurveOffset());
|
||||||
|
if (curve)
|
||||||
|
ret = curve._segment1;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splits the path at the given offset or location. After splitting, the
|
* Splits the path at the given offset or location. After splitting, the
|
||||||
* path will be open. If the path was open already, splitting will result in
|
* path will be open. If the path was open already, splitting will result in
|
||||||
|
@ -1019,8 +1040,8 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
* path.firstSegment.selected = true;
|
* path.firstSegment.selected = true;
|
||||||
*/
|
*/
|
||||||
splitAt: function(location) {
|
splitAt: function(location) {
|
||||||
var loc = typeof location === 'number'
|
// NOTE: getLocationAt() handles both offset and location:
|
||||||
? this.getLocationAt(location) : location,
|
var loc = this.getLocationAt(location),
|
||||||
index = loc && loc.index,
|
index = loc && loc.index,
|
||||||
time = loc && loc.time,
|
time = loc && loc.time,
|
||||||
tMin = /*#=*/Numerical.CURVETIME_EPSILON,
|
tMin = /*#=*/Numerical.CURVETIME_EPSILON,
|
||||||
|
@ -1855,21 +1876,27 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
* @return {CurveLocation} the curve location at the specified offset
|
* @return {CurveLocation} the curve location at the specified offset
|
||||||
*/
|
*/
|
||||||
getLocationAt: function(offset) {
|
getLocationAt: function(offset) {
|
||||||
var curves = this.getCurves(),
|
if (typeof offset === 'number') {
|
||||||
length = 0;
|
var curves = this.getCurves(),
|
||||||
for (var i = 0, l = curves.length; i < l; i++) {
|
length = 0;
|
||||||
var start = length,
|
for (var i = 0, l = curves.length; i < l; i++) {
|
||||||
curve = curves[i];
|
var start = length,
|
||||||
length += curve.getLength();
|
curve = curves[i];
|
||||||
if (length > offset) {
|
length += curve.getLength();
|
||||||
// Found the segment within which the length lies
|
if (length > offset) {
|
||||||
return curve.getLocationAt(offset - start);
|
// Found the segment within which the length lies
|
||||||
|
return curve.getLocationAt(offset - start);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// It may be that through imprecision of getLength, that the end of
|
||||||
|
// the last curve was missed:
|
||||||
|
if (curves.length > 0 && offset <= this.getLength()) {
|
||||||
|
return new CurveLocation(curves[curves.length - 1], 1);
|
||||||
|
}
|
||||||
|
} else if (offset && offset.getPath && offset.getPath() === this) {
|
||||||
|
// offset is already a CurveLocation on this path, just return it.
|
||||||
|
return offset;
|
||||||
}
|
}
|
||||||
// It may be that through imprecision of getLength, that the end of the
|
|
||||||
// last curve was missed:
|
|
||||||
if (curves.length > 0 && offset <= this.getLength())
|
|
||||||
return new CurveLocation(curves[curves.length - 1], 1);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue