-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.ts
54 lines (41 loc) · 1.44 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import * as makerjs from "makerjs";
export interface options {
bezierAccuracy?: number;
joints?: number;
inside?: boolean;
outside?: boolean;
tagName?: 'path' | 'polygon' | 'polyline';
}
export default function outline (svgData: string, distance: number, opts?: options): string {
const defaultOptions: options = {
bezierAccuracy: 0.25,
joints: 0,
outside: true,
tagName: 'path'
};
var o = makerjs.extendObject(defaultOptions, opts) as options;
let closed = true;
let input: makerjs.IModel;
switch (o.tagName) {
case 'polyline':
closed = false;
//fall through
case 'polygon':
//use points.
//Need to mirror them on y axis because they are expected to be in MakerJs coordinate space
input = makerjs.model.mirror(new makerjs.models.ConnectTheDots(closed, svgData), false, true);
break;
default:
input = makerjs.importer.fromSVGPathData(svgData, { bezierAccuracy: o.bezierAccuracy });
break;
}
var result: makerjs.IModel;
if (o.inside && o.outside) {
result = makerjs.model.expandPaths(input, distance, o.joints);
} else {
result = makerjs.model.outline(input, distance, o.joints, o.inside);
}
makerjs.model.simplify(result);
return makerjs.exporter.toSVGPathData(result, false, [0, 0]) as string;
}
module.exports = outline;