mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-20 22:39:50 -05:00
Improve CompoundPath#reduce() to properly reduce suppaths.
Relates to #779
This commit is contained in:
parent
fb5f8c0115
commit
3da921a0b0
3 changed files with 13 additions and 7 deletions
|
@ -131,16 +131,23 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
|
|||
this._children[i].smooth();
|
||||
},
|
||||
|
||||
// DOCS: reduce()
|
||||
// TEST: reduce()
|
||||
reduce: function reduce() {
|
||||
if (this._children.length === 0) { // Replace with a simple empty Path
|
||||
var children = this._children;
|
||||
for (var i = children.length - 1; i >= 0; i--) {
|
||||
var path = children[i].reduce();
|
||||
if (path.isEmpty())
|
||||
children.splice(i, 1);
|
||||
}
|
||||
if (children.length === 0) { // Replace with a simple empty Path
|
||||
var path = new Path(Item.NO_INSERT);
|
||||
path.insertAbove(this);
|
||||
path.setStyle(this._style);
|
||||
this.remove();
|
||||
return path;
|
||||
} else {
|
||||
return reduce.base.call(this);
|
||||
}
|
||||
return reduce.base.call(this);
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -885,7 +885,7 @@ statics: {
|
|||
* @return {Boolean} {@true if the two lines are collinear}
|
||||
*/
|
||||
isCollinear: function(curve) {
|
||||
return this.isStraight() && curve.isStraight()
|
||||
return curve && this.isStraight() && curve.isStraight()
|
||||
&& this.getVector().isCollinear(curve.getVector());
|
||||
}
|
||||
}), /** @lends Curve# */{
|
||||
|
|
|
@ -1001,10 +1001,9 @@ var Path = PathItem.extend(/** @lends Path# */{
|
|||
reduce: function() {
|
||||
var curves = this.getCurves();
|
||||
for (var i = curves.length - 1; i >= 0; i--) {
|
||||
var curve = curves[i],
|
||||
next;
|
||||
var curve = curves[i];
|
||||
if (!curve.hasHandles() && (curve.getLength() === 0
|
||||
|| (next = curve.getNext()) && curve.isCollinear(next)))
|
||||
|| curve.isCollinear(curve.getNext())))
|
||||
curve.remove();
|
||||
}
|
||||
return this;
|
||||
|
|
Loading…
Reference in a new issue