blob: cb1087a8dba602c99da604f5315ea6ff04dd3d1e [file] [log] [blame]
Kevin Lubick11194ab2018-08-17 13:52:56 -04001
2describe('PathKit\'s Path Behavior', function() {
3 // Note, don't try to print the PathKit object - it can cause Karma/Jasmine to lock up.
4 var PathKit = null;
5 const LoadPathKit = new Promise(function(resolve, reject) {
6 if (PathKit) {
7 resolve();
8 } else {
9 PathKitInit({
10 locateFile: (file) => '/pathkit/'+file,
11 }).then((_PathKit) => {
12 PathKit = _PathKit;
13 resolve();
14 });
15 }
16 });
17
18 // see https://fiddle.skia.org/c/@discrete_path
19 function drawStar() {
20 let path = PathKit.NewPath();
21 let R = 115.2, C = 128.0;
22 path.moveTo(C + R + 22, C);
23 for (let i = 1; i < 8; i++) {
24 let a = 2.6927937 * i;
25 path.lineTo(C + R * Math.cos(a) + 22, C + R * Math.sin(a));
26 }
27 path.closePath();
28 return path;
29 }
30
31 describe('Dash Path Effect', function() {
32 it('performs dash in-place with start, stop, phase', function(done) {
33 LoadPathKit.then(() => {
34 let orig = drawStar();
35 let dashed = drawStar();
36 let notACopy = dashed.dash(10, 3, 0);
37 let phased = drawStar().dash(10, 3, 2);
38
39 expect(dashed === notACopy).toBe(true);
40 expect(dashed.equals(phased)).toBe(false);
41 expect(dashed.equals(orig)).toBe(false);
Kevin Lubick11194ab2018-08-17 13:52:56 -040042
Kevin Lubickc6c48aa2018-08-17 15:00:43 -040043 reportPath(dashed, 'dashed_no_phase', () => {
44 reportPath(phased, 'dashed_with_phase', done);
45 orig.delete();
46 dashed.delete();
47 phased.delete();
48 });
Kevin Lubick11194ab2018-08-17 13:52:56 -040049 });
50 });
51 });
52
53 describe('Trim Path Effect', function() {
54 it('performs trim in-place with start, stop, phase', function(done) {
55 LoadPathKit.then(() => {
56 let orig = drawStar();
57 let trimmed = drawStar();
58 let notACopy = trimmed.trim(0.25, .8);
59 let complement = drawStar().trim(.1, .9, true);
60
61 expect(trimmed === notACopy).toBe(true);
62 expect(trimmed.equals(complement)).toBe(false);
63 expect(trimmed.equals(orig)).toBe(false);
64 expect(complement.equals(orig)).toBe(false);
65
Kevin Lubickc6c48aa2018-08-17 15:00:43 -040066 reportPath(trimmed, 'trimmed_non_complement', () => {
67 reportPath(complement, 'trimmed_complement', done);
68 orig.delete();
69 trimmed.delete();
70 complement.delete();
71 });
Kevin Lubick11194ab2018-08-17 13:52:56 -040072
Kevin Lubick11194ab2018-08-17 13:52:56 -040073 });
74 });
75 });
76
77 describe('Transform Path Effect', function() {
78 it('performs matrix transform in-place', function(done) {
79 LoadPathKit.then(() => {
80 let orig = drawStar();
81 let scaled = drawStar();
82 let notACopy = scaled.transform(3, 0, 0,
83 0, 3, 0,
84 0, 0, 1);
85
86 let scaled2 = drawStar().transform([3, 0, 0,
87 0, 3, 0,
88 0, 0, 1]);
89
90 expect(scaled === notACopy).toBe(true);
91 expect(scaled.equals(scaled2)).toBe(true);
92 expect(scaled.equals(orig)).toBe(false);
93
Kevin Lubickc6c48aa2018-08-17 15:00:43 -040094 reportPath(scaled, 'transformed_scale', () => {
95 reportPath(scaled2, 'transformed_scale2', done);
96 orig.delete();
97 scaled.delete();
98 scaled2.delete();
99 });
Kevin Lubick11194ab2018-08-17 13:52:56 -0400100 });
101 });
102 });
103
104 describe('Stroke Path Effect', function() {
105 it('creates a stroked path in-place', function(done) {
106 LoadPathKit.then(() => {
107 let orig = drawStar();
108 let stroked = drawStar();
109 let notACopy = stroked.stroke({
110 width: 15,
111 join: PathKit.StrokeJoin.BEVEL,
112 cap: PathKit.StrokeCap.BUTT,
113 miter_limit: 2,
114 });
115
116 // Don't have to specify all of the fields, defaults will
117 // be used instead.
118 let rounded = drawStar().stroke({
119 width: 10,
120 join: PathKit.StrokeJoin.ROUND,
121 cap:PathKit.StrokeCap.SQUARE,
122 });
123
124 expect(stroked === notACopy).toBe(true);
125 expect(stroked.equals(rounded)).toBe(false);
126 expect(stroked.equals(orig)).toBe(false);
127
Kevin Lubickc6c48aa2018-08-17 15:00:43 -0400128 reportPath(stroked, 'stroke_bevel_butt', () => {
129 reportPath(rounded, 'stroke_round_square', done);
130 orig.delete();
131 stroked.delete();
132 rounded.delete();
133 });
Kevin Lubick11194ab2018-08-17 13:52:56 -0400134 });
135 });
136 });
137
138});