mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Remove #isStraight() in favor of #hasHandles() and implement #clearHandles()
Relates to #652
This commit is contained in:
parent
bfbe0b3147
commit
8b67d8a1dc
5 changed files with 47 additions and 51 deletions
|
@ -120,11 +120,11 @@ var Curve = Base.extend(/** @lends Curve# */{
|
|||
},
|
||||
|
||||
_serialize: function(options) {
|
||||
// If it is straight, only serialize point, otherwise handles too.
|
||||
return Base.serialize(this.isStraight()
|
||||
? [this.getPoint1(), this.getPoint2()]
|
||||
: [this.getPoint1(), this.getHandle1(), this.getHandle2(),
|
||||
this.getPoint2()],
|
||||
// If it has no handles, only serialize points, otherwise handles too.
|
||||
return Base.serialize(this.hasHandles()
|
||||
? [this.getPoint1(), this.getHandle1(), this.getHandle2(),
|
||||
this.getPoint2()]
|
||||
: [this.getPoint1(), this.getPoint2()],
|
||||
options, true);
|
||||
},
|
||||
|
||||
|
@ -329,22 +329,17 @@ var Curve = Base.extend(/** @lends Curve# */{
|
|||
* @see Path#hasHandles()
|
||||
*/
|
||||
hasHandles: function() {
|
||||
return !this.isStraight();
|
||||
return !this._segment1._handleOut.isZero()
|
||||
|| !this._segment2._handleIn.isZero();
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks whether the curve is straight, meaning it has no curve handles
|
||||
* defined and thus appears as a line.
|
||||
* Note that this is not the same as {@link #isLinear()}, which performs a
|
||||
* full linearity check that includes handles running collinear to the line
|
||||
* direction.
|
||||
*
|
||||
* @return {Boolean} {@true if the curve is straight}
|
||||
* @see Segment#isStraight()
|
||||
* Clears the curve's handles by setting their coordinates to zero,
|
||||
* turning the curve into a straight line.
|
||||
*/
|
||||
isStraight: function() {
|
||||
return this._segment1._handleOut.isZero()
|
||||
&& this._segment2._handleIn.isZero();
|
||||
clearHandles: function() {
|
||||
this._segment1._handleOut.set(0, 0);
|
||||
this._segment2._handleIn.set(0, 0);
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -402,6 +402,16 @@ var Path = PathItem.extend(/** @lends Path# */{
|
|||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
* Clears the path's handles by setting their coordinates to zero,
|
||||
* turning the path into a polygon (or a polyline if it isn't closed).
|
||||
*/
|
||||
clearHandles: function() {
|
||||
var segments = this._segments;
|
||||
for (var i = 0, l = segments.length; i < l; i++)
|
||||
segments[i].clearHandles();
|
||||
},
|
||||
|
||||
_transformContent: function(matrix) {
|
||||
var coords = new Array(6);
|
||||
for (var i = 0, l = this._segments.length; i < l; i++)
|
||||
|
|
|
@ -242,8 +242,8 @@ PathItem.inject(new function() {
|
|||
// TODO: Make public in API, since useful!
|
||||
var tMin = /*#=*/Numerical.TOLERANCE,
|
||||
tMax = 1 - tMin,
|
||||
isStraight = false,
|
||||
straightSegments = [];
|
||||
noHandles = false,
|
||||
clearSegments = [];
|
||||
|
||||
for (var i = intersections.length - 1, curve, prev; i >= 0; i--) {
|
||||
var loc = intersections[i],
|
||||
|
@ -255,7 +255,7 @@ PathItem.inject(new function() {
|
|||
t /= prev._parameter;
|
||||
} else {
|
||||
curve = loc._curve;
|
||||
isStraight = curve.isStraight();
|
||||
noHandles = !curve.hasHandles();
|
||||
}
|
||||
var segment;
|
||||
if (t < tMin) {
|
||||
|
@ -270,22 +270,18 @@ PathItem.inject(new function() {
|
|||
curve = newCurve.getPrevious();
|
||||
// Keep track of segments of once straight curves, so they can
|
||||
// be set back straight at the end.
|
||||
if (isStraight)
|
||||
straightSegments.push(segment);
|
||||
if (noHandles)
|
||||
clearSegments.push(segment);
|
||||
}
|
||||
// Link the new segment with the intersection on the other curve
|
||||
segment._intersection = loc.getIntersection();
|
||||
loc._segment = segment;
|
||||
prev = loc;
|
||||
}
|
||||
// Reset linear segments if they were part of a linear curve
|
||||
// and if we are done with the entire curve.
|
||||
for (var i = 0, l = straightSegments.length; i < l; i++) {
|
||||
var segment = straightSegments[i];
|
||||
// TODO: Implement Segment#makeStraight(),
|
||||
// or #adjustHandles({ straight: true }))
|
||||
segment._handleIn.set(0, 0);
|
||||
segment._handleOut.set(0, 0);
|
||||
// Clear segment handles if they were part of a curve with no handles,
|
||||
// once we are done with the entire curve.
|
||||
for (var i = 0, l = clearSegments.length; i < l; i++) {
|
||||
clearSegments[i].clearHandles();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -145,9 +145,10 @@ var Segment = Base.extend(/** @lends Segment# */{
|
|||
},
|
||||
|
||||
_serialize: function(options) {
|
||||
// If it is straight, only serialize point, otherwise handles too.
|
||||
return Base.serialize(this.isStraight() ? this._point
|
||||
: [this._point, this._handleIn, this._handleOut],
|
||||
// If it is has no handles, only serialize point, otherwise handles too.
|
||||
return Base.serialize(this.hasHandles()
|
||||
? [this._point, this._handleIn, this._handleOut]
|
||||
: this._point,
|
||||
options, true);
|
||||
},
|
||||
|
||||
|
@ -237,22 +238,16 @@ var Segment = Base.extend(/** @lends Segment# */{
|
|||
* @see Path#hasHandles()
|
||||
*/
|
||||
hasHandles: function() {
|
||||
return !this.isStraight();
|
||||
return !this._handleIn.isZero() || !this._handleOut.isZero();
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks whether the segment is straight, meaning it has no curve handles
|
||||
* defined. If two straight segments follow each each other in a path, the
|
||||
* curve between them will appear as a straight line.
|
||||
* Note that this is not the same as {@link #isLinear()}, which performs a
|
||||
* full linearity check that includes handles running collinear to the line
|
||||
* direction.
|
||||
*
|
||||
* @return {Boolean} {@true if the segment is straight}
|
||||
* @see Curve#isStraight()
|
||||
* Clears the segment's handles by setting their coordinates to zero,
|
||||
* turning the segment into a corner.
|
||||
*/
|
||||
isStraight: function() {
|
||||
return this._handleIn.isZero() && this._handleOut.isZero();
|
||||
clearHandles: function() {
|
||||
this._handleIn.set(0, 0);
|
||||
this._handleOut.set(0, 0);
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -579,7 +574,7 @@ var Segment = Base.extend(/** @lends Segment# */{
|
|||
},
|
||||
|
||||
isCollinear: function(seg1, seg2, seg3, seg4) {
|
||||
// TODO: This assumes isStraight(), while isLinear() allows handles!
|
||||
// TODO: This assumes !hasHandles(), while isLinear() allows handles!
|
||||
return seg1._handleOut.isZero() && seg2._handleIn.isZero()
|
||||
&& seg3._handleOut.isZero() && seg4._handleIn.isZero()
|
||||
&& seg2._point.subtract(seg1._point).isCollinear(
|
||||
|
@ -587,7 +582,7 @@ var Segment = Base.extend(/** @lends Segment# */{
|
|||
},
|
||||
|
||||
isOrthogonal: function(seg1, seg2, seg3) {
|
||||
// TODO: This assumes isStraight(), while isLinear() allows handles!
|
||||
// TODO: This assumes !hasHandles(), while isLinear() allows handles!
|
||||
return seg1._handleOut.isZero() && seg2._handleIn.isZero()
|
||||
&& seg2._handleOut.isZero() && seg3._handleIn.isZero()
|
||||
&& seg2._point.subtract(seg1._point).isOrthogonal(
|
||||
|
|
|
@ -107,10 +107,10 @@ test('Curve list after removing a segment - 2', function() {
|
|||
}, 1, 'After removing the last segment, we should be left with one curve');
|
||||
});
|
||||
|
||||
test('Splitting a straight path should produce straight segments', function() {
|
||||
var path = new Path.Line([0, 0], [50, 50]);
|
||||
var path2 = path.split(0, 0.5);
|
||||
test('Splitting a straight path should produce segments without handles', function() {
|
||||
var path1 = new Path.Line([0, 0], [50, 50]);
|
||||
var path2 = path1.split(0, 0.5);
|
||||
equals(function() {
|
||||
return path2.firstSegment.isStraight();
|
||||
return !path1.lastSegment.hasHandles() && !path2.firstSegment.hasHandles();
|
||||
}, true);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue