Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 1 | |
| 2 | |
| 3 | describe('PathKit\'s Path2D API', function() { |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 4 | // Note, don't try to print the PathKit object - it can cause Karma/Jasmine to lock up. |
| 5 | var PathKit = null; |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 6 | const LoadPathKit = new Promise(function(resolve, reject) { |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 7 | if (PathKit) { |
| 8 | resolve(); |
| 9 | } else { |
| 10 | PathKitInit({ |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 11 | locateFile: (file) => '/pathkit/'+file, |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 12 | }).then((_PathKit) => { |
| 13 | PathKit = _PathKit; |
| 14 | resolve(); |
| 15 | }); |
| 16 | } |
| 17 | }); |
| 18 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 19 | it('can do everything in the Path2D API w/o crashing', function(done) { |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 20 | LoadPathKit.then(() => { |
| 21 | // This is taken from example.html |
| 22 | let path = PathKit.NewPath(); |
| 23 | |
| 24 | path.moveTo(20, 5); |
| 25 | path.lineTo(30, 20); |
| 26 | path.lineTo(40, 10); |
| 27 | path.lineTo(50, 20); |
| 28 | path.lineTo(60, 0); |
| 29 | path.lineTo(20, 5); |
| 30 | |
| 31 | path.moveTo(20, 80); |
| 32 | path.bezierCurveTo(90, 10, 160, 150, 190, 10); |
| 33 | |
| 34 | path.moveTo(36, 148); |
| 35 | path.quadraticCurveTo(66, 188, 120, 136); |
| 36 | path.lineTo(36, 148); |
| 37 | |
| 38 | path.rect(5, 170, 20, 20); |
| 39 | |
| 40 | path.moveTo(150, 180); |
| 41 | path.arcTo(150, 100, 50, 200, 20); |
| 42 | path.lineTo(160, 160); |
| 43 | |
| 44 | path.moveTo(20, 120); |
| 45 | path.arc(20, 120, 18, 0, 1.75 * Math.PI); |
| 46 | path.lineTo(20, 120); |
| 47 | |
| 48 | let secondPath = PathKit.NewPath(); |
| 49 | secondPath.ellipse(130, 25, 30, 10, -1*Math.PI/8, Math.PI/6, 1.5*Math.PI, false); |
| 50 | |
| 51 | path.addPath(secondPath); |
| 52 | |
| 53 | let m = document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGMatrix(); |
| 54 | m.a = 1; m.b = 0; |
| 55 | m.c = 0; m.d = 1; |
| 56 | m.e = 0; m.f = 20.5; |
| 57 | |
| 58 | path.addPath(secondPath, m); |
| 59 | // An alternate API that avoids having to make the SVG matrix |
| 60 | path.addPath(secondPath, 1, 0, 0, 1, 0, 20.5); |
| 61 | |
| 62 | let canvas = document.createElement('canvas'); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 63 | let canvasCtx = canvas.getContext('2d'); |
Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame^] | 64 | // Set canvas size and make it a bit bigger to zoom in on the lines |
| 65 | standardizedCanvasSize(canvasCtx); |
| 66 | canvasCtx.scale(3.0, 3.0); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 67 | canvasCtx.fillStyle = 'blue'; |
| 68 | canvasCtx.stroke(path.toPath2D()); |
| 69 | |
| 70 | path.delete(); |
| 71 | secondPath.delete(); |
| 72 | |
Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame^] | 73 | reportCanvas(canvas, 'path2D_api_example').then(() => { |
| 74 | done(); |
| 75 | }).catch(reportError(done)); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 76 | }); |
| 77 | }); |
| 78 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 79 | it('approximates arcs (conics) with quads', function(done) { |
| 80 | LoadPathKit.then(() => { |
| 81 | let path = PathKit.NewPath(); |
Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame^] | 82 | path.moveTo(50, 120); |
| 83 | path.arc(50, 120, 45, 0, 1.75 * Math.PI); |
| 84 | path.lineTo(50, 120); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 85 | |
| 86 | let canvas = document.createElement('canvas'); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 87 | let canvasCtx = canvas.getContext('2d'); |
Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame^] | 88 | standardizedCanvasSize(canvasCtx); |
| 89 | // The and.callThrough is important to make it actually |
| 90 | // draw the quadratics |
| 91 | spyOn(canvasCtx, 'quadraticCurveTo').and.callThrough(); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 92 | |
| 93 | canvasCtx.beginPath(); |
| 94 | path.toCanvas(canvasCtx); |
| 95 | canvasCtx.stroke(); |
| 96 | // No need to check the whole path, as that's more what the |
| 97 | // gold correctness tests are for (can account for changes we make |
| 98 | // to the approximation algorithms). |
| 99 | expect(canvasCtx.quadraticCurveTo).toHaveBeenCalled(); |
| 100 | path.delete(); |
Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame^] | 101 | reportCanvas(canvas, 'conics_quads_approx').then(() => { |
| 102 | done(); |
| 103 | }).catch(reportError(done)); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 104 | }); |
| 105 | }); |
| 106 | |
| 107 | }); |