Skip to content

Commit

Permalink
fix null path error on offsetStroke
Browse files Browse the repository at this point in the history
  • Loading branch information
glenzli committed Apr 26, 2020
1 parent 7f57473 commit 1f847e7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
3 changes: 3 additions & 0 deletions demo/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
cc.translate(new paper.Point(-100, -100));

PaperOffset.offset(cc, 24);

const c = new paper.Path({pathData: "M4,11L5,13", strokeColor: 'rgba(156, 104, 193, 0.5)', strokeWidth: 4});
PaperOffset.offsetStroke(c, 10, {cap: "round", join: "round"});
}

window.onload = DebugCase;
Expand Down
34 changes: 21 additions & 13 deletions demo/paperjs-offset.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,19 @@
}
/** Remove self intersection when offset is negative by point direction dectection. */
function removeIntersection(path) {
var newPath = path.unite(path, { insert: false });
if (newPath instanceof paper.CompoundPath) {
newPath.children.filter(function (c) {
if (c.segments.length > 1) {
return !isSameDirection(c, path);
}
else {
return true;
}
}).forEach(function (c) { return c.remove(); });
return reduceSingleChildCompoundPath(newPath);
if (path.closed) {
var newPath = path.unite(path, { insert: false });
if (newPath instanceof paper.CompoundPath) {
newPath.children.filter(function (c) {
if (c.segments.length > 1) {
return !isSameDirection(c, path);
}
else {
return true;
}
}).forEach(function (c) { return c.remove(); });
return reduceSingleChildCompoundPath(newPath);
}
}
return path;
}
Expand Down Expand Up @@ -292,8 +294,14 @@
return final;
}
}
function getNonSelfItersectionPath(path) {
if (path.closed) {
return path.unite(path, { insert: false });
}
return path;
}
function offsetPath(path, offset, join, limit) {
var nonSIPath = path.unite(path, { insert: false });
var nonSIPath = getNonSelfItersectionPath(path);
var result = nonSIPath;
if (nonSIPath instanceof paper.Path) {
result = offsetSimpleShape(nonSIPath, offset, join, limit);
Expand Down Expand Up @@ -329,7 +337,7 @@
return result;
}
function offsetStroke(path, offset, join, cap, limit) {
var nonSIPath = path.unite(path, { insert: false });
var nonSIPath = getNonSelfItersectionPath(path);
var result = nonSIPath;
if (nonSIPath instanceof paper.Path) {
result = offsetSimpleStroke(nonSIPath, offset, join, cap, limit);
Expand Down
2 changes: 1 addition & 1 deletion demo/paperjs-offset.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import paper from 'paper';
import { StrokeJoinType, PathType, StrokeCapType, offsetPath, offsetStroke } from './offset';
import { StrokeJoinType, PathType, StrokeCapType, offsetPath, offsetStroke } from './offset_core';

export interface OffsetOptions {
join?: StrokeJoinType;
Expand Down
35 changes: 22 additions & 13 deletions src/Offset.ts → src/offset_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,18 @@ function isSameDirection(partialPath: paper.Path, fullPath: PathType) {

/** Remove self intersection when offset is negative by point direction dectection. */
function removeIntersection(path: PathType) {
const newPath = path.unite(path, { insert: false }) as PathType;
if (newPath instanceof paper.CompoundPath) {
(newPath.children as paper.Path[]).filter((c) => {
if (c.segments.length > 1) {
return !isSameDirection(c, path);
} else {
return true;
}
}).forEach((c) => c.remove());
return reduceSingleChildCompoundPath(newPath);
if (path.closed) {
const newPath = path.unite(path, { insert: false }) as PathType;
if (newPath instanceof paper.CompoundPath) {
(newPath.children as paper.Path[]).filter((c) => {
if (c.segments.length > 1) {
return !isSameDirection(c, path);
} else {
return true;
}
}).forEach((c) => c.remove());
return reduceSingleChildCompoundPath(newPath);
}
}
return path;
}
Expand Down Expand Up @@ -306,8 +308,15 @@ function offsetSimpleStroke(path: paper.Path, offset: number, join: StrokeJoinTy
}
}

function getNonSelfItersectionPath(path: PathType) {
if (path.closed) {
return path.unite(path, { insert: false }) as PathType;
}
return path;
}

export function offsetPath(path: PathType, offset: number, join: StrokeJoinType, limit: number): PathType {
const nonSIPath = path.unite(path, { insert: false }) as PathType;
const nonSIPath = getNonSelfItersectionPath(path);
let result = nonSIPath;
if (nonSIPath instanceof paper.Path) {
result = offsetSimpleShape(nonSIPath, offset, join, limit);
Expand Down Expand Up @@ -341,8 +350,8 @@ export function offsetPath(path: PathType, offset: number, join: StrokeJoinType,
}

export function offsetStroke(path: PathType, offset: number, join: StrokeJoinType, cap: StrokeCapType, limit: number): PathType {
const nonSIPath = path.unite(path, { insert: false }) as PathType;
let result = nonSIPath as PathType;
const nonSIPath = getNonSelfItersectionPath(path);
let result = nonSIPath;
if (nonSIPath instanceof paper.Path) {
result = offsetSimpleStroke(nonSIPath, offset, join, cap, limit);
} else {
Expand Down

0 comments on commit 1f847e7

Please sign in to comment.