blob: 575c95a5598705d598b3dfc9e6103f054027947b [file] [log] [blame]
Kevin Lubick11194ab2018-08-17 13:52:56 -04001
2describe('PathKit\'s Path Behavior', function() {
Kevin Lubick11194ab2018-08-17 13:52:56 -04003 // see https://fiddle.skia.org/c/@discrete_path
4 function drawStar() {
5 let path = PathKit.NewPath();
6 let R = 115.2, C = 128.0;
7 path.moveTo(C + R + 22, C);
8 for (let i = 1; i < 8; i++) {
9 let a = 2.6927937 * i;
10 path.lineTo(C + R * Math.cos(a) + 22, C + R * Math.sin(a));
11 }
12 path.closePath();
13 return path;
14 }
15
16 describe('Dash Path Effect', function() {
17 it('performs dash in-place with start, stop, phase', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050018 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040019 let orig = drawStar();
20 let dashed = drawStar();
21 let notACopy = dashed.dash(10, 3, 0);
22 let phased = drawStar().dash(10, 3, 2);
23
24 expect(dashed === notACopy).toBe(true);
25 expect(dashed.equals(phased)).toBe(false);
26 expect(dashed.equals(orig)).toBe(false);
Kevin Lubick11194ab2018-08-17 13:52:56 -040027
Kevin Lubickc6c48aa2018-08-17 15:00:43 -040028 reportPath(dashed, 'dashed_no_phase', () => {
29 reportPath(phased, 'dashed_with_phase', done);
30 orig.delete();
31 dashed.delete();
32 phased.delete();
33 });
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050034 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -040035 });
36 });
37
38 describe('Trim Path Effect', function() {
39 it('performs trim in-place with start, stop, phase', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050040 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040041 let orig = drawStar();
42 let trimmed = drawStar();
43 let notACopy = trimmed.trim(0.25, .8);
44 let complement = drawStar().trim(.1, .9, true);
45
46 expect(trimmed === notACopy).toBe(true);
47 expect(trimmed.equals(complement)).toBe(false);
48 expect(trimmed.equals(orig)).toBe(false);
49 expect(complement.equals(orig)).toBe(false);
50
Kevin Lubickc6c48aa2018-08-17 15:00:43 -040051 reportPath(trimmed, 'trimmed_non_complement', () => {
52 reportPath(complement, 'trimmed_complement', done);
53 orig.delete();
54 trimmed.delete();
55 complement.delete();
56 });
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050057 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -040058 });
59 });
60
61 describe('Transform Path Effect', function() {
62 it('performs matrix transform in-place', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050063 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040064 let orig = drawStar();
65 let scaled = drawStar();
66 let notACopy = scaled.transform(3, 0, 0,
67 0, 3, 0,
68 0, 0, 1);
69
70 let scaled2 = drawStar().transform([3, 0, 0,
71 0, 3, 0,
72 0, 0, 1]);
73
74 expect(scaled === notACopy).toBe(true);
75 expect(scaled.equals(scaled2)).toBe(true);
76 expect(scaled.equals(orig)).toBe(false);
77
Kevin Lubickc6c48aa2018-08-17 15:00:43 -040078 reportPath(scaled, 'transformed_scale', () => {
79 reportPath(scaled2, 'transformed_scale2', done);
80 orig.delete();
81 scaled.delete();
82 scaled2.delete();
83 });
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050084 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -040085 });
86 });
87
88 describe('Stroke Path Effect', function() {
89 it('creates a stroked path in-place', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050090 LoadPathKit.then(catchException(done, () => {
Kevin Lubick11194ab2018-08-17 13:52:56 -040091 let orig = drawStar();
92 let stroked = drawStar();
93 let notACopy = stroked.stroke({
94 width: 15,
95 join: PathKit.StrokeJoin.BEVEL,
96 cap: PathKit.StrokeCap.BUTT,
97 miter_limit: 2,
98 });
99
100 // Don't have to specify all of the fields, defaults will
101 // be used instead.
102 let rounded = drawStar().stroke({
103 width: 10,
104 join: PathKit.StrokeJoin.ROUND,
105 cap:PathKit.StrokeCap.SQUARE,
106 });
107
108 expect(stroked === notACopy).toBe(true);
109 expect(stroked.equals(rounded)).toBe(false);
110 expect(stroked.equals(orig)).toBe(false);
111
Kevin Lubickc6c48aa2018-08-17 15:00:43 -0400112 reportPath(stroked, 'stroke_bevel_butt', () => {
113 reportPath(rounded, 'stroke_round_square', done);
114 orig.delete();
115 stroked.delete();
116 rounded.delete();
117 });
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500118 }));
Kevin Lubick11194ab2018-08-17 13:52:56 -0400119 });
120 });
121
122});