From cff2a4023ddb94496e80d23e9acfb4498b864e1a Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Tue, 7 Aug 2018 07:40:23 +0200 Subject: [PATCH] :sparkles: Path.join() and more robust offset --- src/path.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/path.js b/src/path.js index c678f3348d2..28bde6f7160 100644 --- a/src/path.js +++ b/src/path.js @@ -179,6 +179,11 @@ Path.prototype.clone = function() { return clone; }; +/** Joins this with that path, closes them if wanted */ +Path.prototype.join = function(that, closed) { + return joinPaths([this, that], closed); +}; + /** Offsets a path by distance */ function pathOffset(path, distance) { let offset = []; @@ -188,7 +193,8 @@ function pathOffset(path, distance) { for (let i in path.ops) { let op = path.ops[i]; if (op.type === "line") { - offset.push(offsetLine(current, op.to, distance)); + let segment = offsetLine(current, op.to, distance); + if (segment) offset.push(segment); } else if (op.type === "curve") { // We need to avoid a control point sitting on top of start or end // because that will break the offset in bezier-js @@ -222,9 +228,8 @@ function pathOffset(path, distance) { /** Offsets a line by distance */ function offsetLine(from, to, distance) { - if (from.x === to.x && from.y === to.y) { - throw "Cannot offset line that starts and ends in the same point"; - } + // Cannot offset line that starts and ends in the same point + if (from.x === to.x && from.y === to.y) return false; let angle = from.angle(to) - 90; return new Path()