✨ Path.join() and more robust offset
This commit is contained in:
parent
8051a48880
commit
cff2a4023d
1 changed files with 9 additions and 4 deletions
13
src/path.js
13
src/path.js
|
@ -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()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue