Kevin Lubick | d2efe52 | 2019-01-04 15:59:06 -0500 | [diff] [blame] | 1 | jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 2 | |
| 3 | describe('PathKit\'s Path Behavior', 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, |
Kevin Lubick | 275eaff | 2019-01-04 14:38:29 -0500 | [diff] [blame] | 12 | }).ready().then((_PathKit) => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 13 | PathKit = _PathKit; |
| 14 | resolve(); |
| 15 | }); |
| 16 | } |
| 17 | }); |
| 18 | |
| 19 | // see https://fiddle.skia.org/c/@discrete_path |
| 20 | function drawStar() { |
| 21 | let path = PathKit.NewPath(); |
| 22 | let R = 115.2, C = 128.0; |
| 23 | path.moveTo(C + R + 22, C); |
| 24 | for (let i = 1; i < 8; i++) { |
| 25 | let a = 2.6927937 * i; |
| 26 | path.lineTo(C + R * Math.cos(a) + 22, C + R * Math.sin(a)); |
| 27 | } |
| 28 | path.closePath(); |
| 29 | return path; |
| 30 | } |
| 31 | |
| 32 | describe('Dash Path Effect', function() { |
| 33 | it('performs dash in-place with start, stop, phase', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 34 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 35 | let orig = drawStar(); |
| 36 | let dashed = drawStar(); |
| 37 | let notACopy = dashed.dash(10, 3, 0); |
| 38 | let phased = drawStar().dash(10, 3, 2); |
| 39 | |
| 40 | expect(dashed === notACopy).toBe(true); |
| 41 | expect(dashed.equals(phased)).toBe(false); |
| 42 | expect(dashed.equals(orig)).toBe(false); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 43 | |
Kevin Lubick | c6c48aa | 2018-08-17 15:00:43 -0400 | [diff] [blame] | 44 | reportPath(dashed, 'dashed_no_phase', () => { |
| 45 | reportPath(phased, 'dashed_with_phase', done); |
| 46 | orig.delete(); |
| 47 | dashed.delete(); |
| 48 | phased.delete(); |
| 49 | }); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 50 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 51 | }); |
| 52 | }); |
| 53 | |
| 54 | describe('Trim Path Effect', function() { |
| 55 | it('performs trim in-place with start, stop, phase', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 56 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 57 | let orig = drawStar(); |
| 58 | let trimmed = drawStar(); |
| 59 | let notACopy = trimmed.trim(0.25, .8); |
| 60 | let complement = drawStar().trim(.1, .9, true); |
| 61 | |
| 62 | expect(trimmed === notACopy).toBe(true); |
| 63 | expect(trimmed.equals(complement)).toBe(false); |
| 64 | expect(trimmed.equals(orig)).toBe(false); |
| 65 | expect(complement.equals(orig)).toBe(false); |
| 66 | |
Kevin Lubick | c6c48aa | 2018-08-17 15:00:43 -0400 | [diff] [blame] | 67 | reportPath(trimmed, 'trimmed_non_complement', () => { |
| 68 | reportPath(complement, 'trimmed_complement', done); |
| 69 | orig.delete(); |
| 70 | trimmed.delete(); |
| 71 | complement.delete(); |
| 72 | }); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 73 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 74 | }); |
| 75 | }); |
| 76 | |
| 77 | describe('Transform Path Effect', function() { |
| 78 | it('performs matrix transform in-place', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 79 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 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 Lubick | c6c48aa | 2018-08-17 15:00:43 -0400 | [diff] [blame] | 94 | reportPath(scaled, 'transformed_scale', () => { |
| 95 | reportPath(scaled2, 'transformed_scale2', done); |
| 96 | orig.delete(); |
| 97 | scaled.delete(); |
| 98 | scaled2.delete(); |
| 99 | }); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 100 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 101 | }); |
| 102 | }); |
| 103 | |
| 104 | describe('Stroke Path Effect', function() { |
| 105 | it('creates a stroked path in-place', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 106 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 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 Lubick | c6c48aa | 2018-08-17 15:00:43 -0400 | [diff] [blame] | 128 | reportPath(stroked, 'stroke_bevel_butt', () => { |
| 129 | reportPath(rounded, 'stroke_round_square', done); |
| 130 | orig.delete(); |
| 131 | stroked.delete(); |
| 132 | rounded.delete(); |
| 133 | }); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 134 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 135 | }); |
| 136 | }); |
| 137 | |
| 138 | }); |