Skip to content

Commit

Permalink
Implement Path#splitAt(offset)
Browse files Browse the repository at this point in the history
Also make sure the deprecated Path#split(offset) works as it used to. Relates to paperjs#563
  • Loading branch information
lehni committed Mar 14, 2016
1 parent 7300260 commit da7d0d8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/path/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -1032,8 +1032,10 @@ var Path = PathItem.extend(/** @lends Path# */{
* path.firstSegment.selected = true;
*/
splitAt: function(location) {
var index = location && location.index,
time = location && location.time,
var loc = typeof location === 'number'
? this.getLocationAt(location) : location,
index = loc && loc.index,
time = loc && loc.time,
tMin = /*#=*/Numerical.CURVETIME_EPSILON,
tMax = 1 - tMin;
if (time >= tMax) {
Expand Down Expand Up @@ -1087,7 +1089,7 @@ var Path = PathItem.extend(/** @lends Path# */{
location = time === undefined ? index
: (curve = this.getCurves()[index])
&& curve.getLocationAtTime(time);
return location ? this.splitAt(location) : null;
return location != null ? this.splitAt(location) : null;
},

// DOCS: document Path#join(path) in more detail.
Expand Down
5 changes: 3 additions & 2 deletions test/tests/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,16 @@ test('Curve list after removing a segment - 2', function() {

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);
var path2 = path1.splitAt(path1.length / 2);
equals(function() {
return !path1.lastSegment.hasHandles() && !path2.firstSegment.hasHandles();
}, true);
});

test('Splitting a path with one curve in the middle result in two paths of the same length with one curve each', function() {
var path1 = new Path.Line([0, 0], [100, 100]);
var path2 = path1.split(path1.getLocationAt(path1.length / 2));
var loc = path1.getLocationAt(path1.length / 2);
var path2 = path1.splitAt(loc);
equals(function() {
return path1.curves.length;
}, 1);
Expand Down

0 comments on commit da7d0d8

Please sign in to comment.