1
2
3describe('PathKit\'s Pathops', function() {
4    // Note, don't try to print the PathKit object - it can cause Karma/Jasmine to lock up.
5    var PathKit = null;
6    const LoadPathKit = new Promise(function(resolve, reject) {
7        if (PathKit) {
8            resolve();
9        } else {
10            PathKitInit({
11                locateFile: (file) => '/pathkit/'+file,
12            }).then((_PathKit) => {
13                PathKit = _PathKit;
14                resolve();
15            });
16        }
17    });
18
19    // see https://fiddle.skia.org/c/@discrete_path
20    function drawStar(X=128, Y=128, R=116) {
21        let p = PathKit.NewPath();
22        p.moveTo(X + R, Y);
23        for (let i = 1; i < 8; i++) {
24          let a = 2.6927937 * i;
25          p.lineTo(X + R * Math.cos(a), Y + R * Math.sin(a));
26        }
27        p.closePath();
28        return p;
29    }
30
31    it('pathops_simplify', function(done) {
32        function setup(ctx) {
33            ctx.path = drawStar();
34        }
35
36        function test(ctx) {
37            let path = ctx.path.copy().simplify();
38            path.delete();
39        }
40
41        function teardown(ctx) {
42            ctx.path.delete();
43        }
44
45        LoadPathKit.then(() => {
46            benchmarkAndReport('pathops_simplify', setup, test, teardown).then(() => {
47                done();
48            }).catch(reportError(done));
49        });
50    });
51
52    it('pathops_diff', function(done) {
53        function setup(ctx) {
54            // Values chosen abitrarily to have some overlap and some not.
55            ctx.path1 = drawStar(X=120, Y=120);
56            ctx.path2 = drawStar(X=140, Y=145);
57        }
58
59        function test(ctx) {
60            let path = PathKit.MakeFromOp(ctx.path1, ctx.path2, PathKit.PathOp.DIFFERENCE);
61            path.delete();
62        }
63
64        function teardown(ctx) {
65            ctx.path1.delete();
66            ctx.path2.delete();
67        }
68
69        LoadPathKit.then(() => {
70            benchmarkAndReport('pathops_diff', setup, test, teardown).then(() => {
71                done();
72            }).catch(reportError(done));
73        });
74    });
75
76    it('pathops_intersect', function(done) {
77        function setup(ctx) {
78            // Values chosen abitrarily to have some overlap and some not.
79            ctx.path1 = drawStar(X=120, Y=120);
80            ctx.path2 = drawStar(X=140, Y=145);
81        }
82
83        function test(ctx) {
84            let path = PathKit.MakeFromOp(ctx.path1, ctx.path2, PathKit.PathOp.INTERSECT);
85            path.delete();
86        }
87
88        function teardown(ctx) {
89            ctx.path1.delete();
90            ctx.path2.delete();
91        }
92
93        LoadPathKit.then(() => {
94            benchmarkAndReport('pathops_intersect', setup, test, teardown).then(() => {
95                done();
96            }).catch(reportError(done));
97        });
98    });
99
100    it('pathops_union', function(done) {
101        function setup(ctx) {
102            // Values chosen abitrarily to have some overlap and some not.
103            ctx.path1 = drawStar(X=120, Y=120);
104            ctx.path2 = drawStar(X=140, Y=145);
105        }
106
107        function test(ctx) {
108            let path = PathKit.MakeFromOp(ctx.path1, ctx.path2, PathKit.PathOp.UNION);
109            path.delete();
110        }
111
112        function teardown(ctx) {
113            ctx.path1.delete();
114            ctx.path2.delete();
115        }
116
117        LoadPathKit.then(() => {
118            benchmarkAndReport('pathops_union', setup, test, teardown).then(() => {
119                done();
120            }).catch(reportError(done));
121        });
122    });
123
124    it('pathops_xor', function(done) {
125        function setup(ctx) {
126            // Values chosen abitrarily to have some overlap and some not.
127            ctx.path1 = drawStar(X=120, Y=120);
128            ctx.path2 = drawStar(X=140, Y=145);
129        }
130
131        function test(ctx) {
132            let path = PathKit.MakeFromOp(ctx.path1, ctx.path2, PathKit.PathOp.XOR);
133            path.delete();
134        }
135
136        function teardown(ctx) {
137            ctx.path1.delete();
138            ctx.path2.delete();
139        }
140
141        LoadPathKit.then(() => {
142            benchmarkAndReport('pathops_xor', setup, test, teardown).then(() => {
143                done();
144            }).catch(reportError(done));
145        });
146    });
147
148    it('pathops_reverse_diff', function(done) {
149        function setup(ctx) {
150            // Values chosen abitrarily to have some overlap and some not.
151            ctx.path1 = drawStar(X=120, Y=120);
152            ctx.path2 = drawStar(X=140, Y=145);
153        }
154
155        function test(ctx) {
156            let path = PathKit.MakeFromOp(ctx.path1, ctx.path2, PathKit.PathOp.REVERSE_DIFFERENCE);
157            path.delete();
158        }
159
160        function teardown(ctx) {
161            ctx.path1.delete();
162            ctx.path2.delete();
163        }
164
165        LoadPathKit.then(() => {
166            benchmarkAndReport('pathops_reverse_diff', setup, test, teardown).then(() => {
167                done();
168            }).catch(reportError(done));
169        });
170    });
171
172});