blob: bbd76f344361539965120ffafe10281ea28d4ebf [file] [log] [blame]
Kevin Lubick92c91712018-08-09 10:00:02 -04001
2
3describe('PathKit\'s Path2D API', function() {
Kevin Lubick92c91712018-08-09 10:00:02 -04004 // Note, don't try to print the PathKit object - it can cause Karma/Jasmine to lock up.
5 var PathKit = null;
Kevin Lubick97d6d982018-08-10 15:53:16 -04006 const LoadPathKit = new Promise(function(resolve, reject) {
Kevin Lubick92c91712018-08-09 10:00:02 -04007 if (PathKit) {
8 resolve();
9 } else {
10 PathKitInit({
Kevin Lubick97d6d982018-08-10 15:53:16 -040011 locateFile: (file) => '/pathkit/'+file,
Kevin Lubick92c91712018-08-09 10:00:02 -040012 }).then((_PathKit) => {
13 PathKit = _PathKit;
14 resolve();
15 });
16 }
17 });
18
Kevin Lubick97d6d982018-08-10 15:53:16 -040019 it('can do everything in the Path2D API w/o crashing', function(done) {
Kevin Lubick92c91712018-08-09 10:00:02 -040020 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 Lubick92c91712018-08-09 10:00:02 -040063 let canvasCtx = canvas.getContext('2d');
Kevin Lubicka0ba6122018-08-15 13:45:28 -040064 // 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 Lubick92c91712018-08-09 10:00:02 -040067 canvasCtx.fillStyle = 'blue';
68 canvasCtx.stroke(path.toPath2D());
69
70 path.delete();
71 secondPath.delete();
72
Kevin Lubicka0ba6122018-08-15 13:45:28 -040073 reportCanvas(canvas, 'path2D_api_example').then(() => {
74 done();
75 }).catch(reportError(done));
Kevin Lubick92c91712018-08-09 10:00:02 -040076 });
77 });
78
Kevin Lubick97d6d982018-08-10 15:53:16 -040079 it('approximates arcs (conics) with quads', function(done) {
80 LoadPathKit.then(() => {
81 let path = PathKit.NewPath();
Kevin Lubicka0ba6122018-08-15 13:45:28 -040082 path.moveTo(50, 120);
83 path.arc(50, 120, 45, 0, 1.75 * Math.PI);
84 path.lineTo(50, 120);
Kevin Lubick97d6d982018-08-10 15:53:16 -040085
86 let canvas = document.createElement('canvas');
Kevin Lubick97d6d982018-08-10 15:53:16 -040087 let canvasCtx = canvas.getContext('2d');
Kevin Lubicka0ba6122018-08-15 13:45:28 -040088 standardizedCanvasSize(canvasCtx);
89 // The and.callThrough is important to make it actually
90 // draw the quadratics
91 spyOn(canvasCtx, 'quadraticCurveTo').and.callThrough();
Kevin Lubick97d6d982018-08-10 15:53:16 -040092
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 Lubicka0ba6122018-08-15 13:45:28 -0400101 reportCanvas(canvas, 'conics_quads_approx').then(() => {
102 done();
103 }).catch(reportError(done));
Kevin Lubick97d6d982018-08-10 15:53:16 -0400104 });
105 });
106
107});