1
0
Fork 0

Path.join() and more robust offset

This commit is contained in:
Joost De Cock 2018-08-07 07:40:23 +02:00
parent 8051a48880
commit cff2a4023d

View file

@ -179,6 +179,11 @@ Path.prototype.clone = function() {
return clone; 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 */ /** Offsets a path by distance */
function pathOffset(path, distance) { function pathOffset(path, distance) {
let offset = []; let offset = [];
@ -188,7 +193,8 @@ function pathOffset(path, distance) {
for (let i in path.ops) { for (let i in path.ops) {
let op = path.ops[i]; let op = path.ops[i];
if (op.type === "line") { 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") { } else if (op.type === "curve") {
// We need to avoid a control point sitting on top of start or end // We need to avoid a control point sitting on top of start or end
// because that will break the offset in bezier-js // because that will break the offset in bezier-js
@ -222,9 +228,8 @@ function pathOffset(path, distance) {
/** Offsets a line by distance */ /** Offsets a line by distance */
function offsetLine(from, to, distance) { function offsetLine(from, to, distance) {
if (from.x === to.x && from.y === to.y) { // Cannot offset line that starts and ends in the same point
throw "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; let angle = from.angle(to) - 90;
return new Path() return new Path()