Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 1 | |
| 2 | describe('PathKit\'s Path Behavior', function() { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 3 | // 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 Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 18 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 19 | 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 Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 27 | |
Kevin Lubick | c6c48aa | 2018-08-17 15:00:43 -0400 | [diff] [blame] | 28 | reportPath(dashed, 'dashed_no_phase', () => { |
| 29 | reportPath(phased, 'dashed_with_phase', done); |
| 30 | orig.delete(); |
| 31 | dashed.delete(); |
| 32 | phased.delete(); |
| 33 | }); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 34 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 35 | }); |
| 36 | }); |
| 37 | |
| 38 | describe('Trim Path Effect', function() { |
| 39 | it('performs trim in-place with start, stop, phase', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 40 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 41 | 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 Lubick | c6c48aa | 2018-08-17 15:00:43 -0400 | [diff] [blame] | 51 | reportPath(trimmed, 'trimmed_non_complement', () => { |
| 52 | reportPath(complement, 'trimmed_complement', done); |
| 53 | orig.delete(); |
| 54 | trimmed.delete(); |
| 55 | complement.delete(); |
| 56 | }); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 57 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 58 | }); |
| 59 | }); |
| 60 | |
| 61 | describe('Transform Path Effect', function() { |
| 62 | it('performs matrix transform in-place', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 63 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 64 | 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 Lubick | c6c48aa | 2018-08-17 15:00:43 -0400 | [diff] [blame] | 78 | reportPath(scaled, 'transformed_scale', () => { |
| 79 | reportPath(scaled2, 'transformed_scale2', done); |
| 80 | orig.delete(); |
| 81 | scaled.delete(); |
| 82 | scaled2.delete(); |
| 83 | }); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 84 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 85 | }); |
| 86 | }); |
| 87 | |
| 88 | describe('Stroke Path Effect', function() { |
| 89 | it('creates a stroked path in-place', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 90 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 91 | 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 Lubick | c6c48aa | 2018-08-17 15:00:43 -0400 | [diff] [blame] | 112 | reportPath(stroked, 'stroke_bevel_butt', () => { |
| 113 | reportPath(rounded, 'stroke_round_square', done); |
| 114 | orig.delete(); |
| 115 | stroked.delete(); |
| 116 | rounded.delete(); |
| 117 | }); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 118 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 119 | }); |
| 120 | }); |
| 121 | |
| 122 | }); |