mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
Implement Curve#remove() and Path#reduce().
Also have Item#reduce() recursively call #reduce() on reduced children.
This commit is contained in:
parent
4453f68cfa
commit
86e4d2a6b9
3 changed files with 47 additions and 13 deletions
|
@ -2064,7 +2064,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{
|
||||||
*/
|
*/
|
||||||
reduce: function() {
|
reduce: function() {
|
||||||
if (this._children && this._children.length === 1) {
|
if (this._children && this._children.length === 1) {
|
||||||
var child = this._children[0];
|
var child = this._children[0].reduce();
|
||||||
child.insertAbove(this);
|
child.insertAbove(this);
|
||||||
child.setStyle(this._style);
|
child.setStyle(this._style);
|
||||||
this.remove();
|
this.remove();
|
||||||
|
|
|
@ -267,8 +267,12 @@ var Curve = Base.extend(/** @lends Curve# */{
|
||||||
* @bean
|
* @bean
|
||||||
*/
|
*/
|
||||||
getLength: function() {
|
getLength: function() {
|
||||||
if (this._length == null)
|
if (this._length == null) {
|
||||||
this._length = Curve.getLength(this.getValues(), 0, 1);
|
// Use simple point distance for linear curves
|
||||||
|
this._length = this.isLinear()
|
||||||
|
? this._segment2._point.getDistance(this._segment1._point)
|
||||||
|
: Curve.getLength(this.getValues(), 0, 1);
|
||||||
|
}
|
||||||
return this._length;
|
return this._length;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -303,16 +307,6 @@ var Curve = Base.extend(/** @lends Curve# */{
|
||||||
|
|
||||||
// TODO: adjustThroughPoint
|
// TODO: adjustThroughPoint
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a reversed version of the curve, without modifying the curve
|
|
||||||
* itself.
|
|
||||||
*
|
|
||||||
* @return {Curve} a reversed version of the curve
|
|
||||||
*/
|
|
||||||
reverse: function() {
|
|
||||||
return new Curve(this._segment2.reverse(), this._segment1.reverse());
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private method that handles all types of offset / isParameter pairs and
|
* Private method that handles all types of offset / isParameter pairs and
|
||||||
* converts it to a curve parameter.
|
* converts it to a curve parameter.
|
||||||
|
@ -418,6 +412,33 @@ var Curve = Base.extend(/** @lends Curve# */{
|
||||||
: null;
|
: null;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a reversed version of the curve, without modifying the curve
|
||||||
|
* itself.
|
||||||
|
*
|
||||||
|
* @return {Curve} a reversed version of the curve
|
||||||
|
*/
|
||||||
|
reverse: function() {
|
||||||
|
return new Curve(this._segment2.reverse(), this._segment1.reverse());
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the curve from the path that it belongs to, by merging its two
|
||||||
|
* path segments.
|
||||||
|
* @return {Boolean} {@true if the curve was removed}
|
||||||
|
*/
|
||||||
|
remove: function() {
|
||||||
|
var removed = false;
|
||||||
|
if (this._path) {
|
||||||
|
var segment2 = this._segment2,
|
||||||
|
handleOut = segment2._handleOut;
|
||||||
|
removed = segment2.remove();
|
||||||
|
if (removed)
|
||||||
|
this._segment1._handleOut.set(handleOut.x, handleOut.y);
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a copy of the curve.
|
* Returns a copy of the curve.
|
||||||
*
|
*
|
||||||
|
|
|
@ -900,6 +900,19 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
this.setSegments(segments);
|
this.setSegments(segments);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reduces the path by removing curves that have a lenght of 0.
|
||||||
|
*/
|
||||||
|
reduce: function() {
|
||||||
|
var curves = this.getCurves();
|
||||||
|
for (var i = curves.length - 1; i >= 0; i--) {
|
||||||
|
var curve = curves[i];
|
||||||
|
if (curve.isLinear() && curve.getLength() === 0)
|
||||||
|
curve.remove();
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Smooths a path by simplifying it. The {@link Path#segments} array is
|
* Smooths a path by simplifying it. The {@link Path#segments} array is
|
||||||
* analyzed and replaced by a more optimal set of segments, reducing memory
|
* analyzed and replaced by a more optimal set of segments, reducing memory
|
||||||
|
|
Loading…
Reference in a new issue