blob: 1ba372b283badcc993204a29a4abb83718b538df [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,
Kevin Lubick275eaff2019-01-04 14:38:29 -050011 }).ready().then((_PathKit) => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040012 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) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050033 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040034 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 Lubicke71e9ef2018-11-05 07:51:40 -050049 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -040050 });
51 });
52
53 describe('Trim Path Effect', function() {
54 it('performs trim in-place with start, stop, phase', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050055 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040056 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 Lubicke71e9ef2018-11-05 07:51:40 -050072 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -040073 });
74 });
75
76 describe('Transform Path Effect', function() {
77 it('performs matrix transform in-place', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050078 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040079 let orig = drawStar();
80 let scaled = drawStar();
81 let notACopy = scaled.transform(3, 0, 0,
82 0, 3, 0,
83 0, 0, 1);
84
85 let scaled2 = drawStar().transform([3, 0, 0,
86 0, 3, 0,
87 0, 0, 1]);
88
89 expect(scaled === notACopy).toBe(true);
90 expect(scaled.equals(scaled2)).toBe(true);
91 expect(scaled.equals(orig)).toBe(false);
92
Kevin Lubickc6c48aa2018-08-17 15:00:43 -040093 reportPath(scaled, 'transformed_scale', () => {
94 reportPath(scaled2, 'transformed_scale2', done);
95 orig.delete();
96 scaled.delete();
97 scaled2.delete();
98 });
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050099 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400100 });
101 });
102
103 describe('Stroke Path Effect', function() {
104 it('creates a stroked path in-place', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500105 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -0400106 let orig = drawStar();
107 let stroked = drawStar();
108 let notACopy = stroked.stroke({
109 width: 15,
110 join: PathKit.StrokeJoin.BEVEL,
111 cap: PathKit.StrokeCap.BUTT,
112 miter_limit: 2,
113 });
114
115 // Don't have to specify all of the fields, defaults will
116 // be used instead.
117 let rounded = drawStar().stroke({
118 width: 10,
119 join: PathKit.StrokeJoin.ROUND,
120 cap:PathKit.StrokeCap.SQUARE,
121 });
122
123 expect(stroked === notACopy).toBe(true);
124 expect(stroked.equals(rounded)).toBe(false);
125 expect(stroked.equals(orig)).toBe(false);
126
Kevin Lubickc6c48aa2018-08-17 15:00:43 -0400127 reportPath(stroked, 'stroke_bevel_butt', () => {
128 reportPath(rounded, 'stroke_round_square', done);
129 orig.delete();
130 stroked.delete();
131 rounded.delete();
132 });
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500133 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400134 });
135 });
136
137});