1 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, 12 }).ready().then((_PathKit) => { 13 PathKit = _PathKit; 14 resolve(); 15 }); 16 } 17 }); 18 19 function drawPath() { 20 let path = PathKit.NewPath(); 21 path.moveTo(20, 5); 22 path.lineTo(30, 20); 23 path.lineTo(40, 10); 24 path.lineTo(50, 20); 25 path.lineTo(60, 0); 26 path.lineTo(20, 5); 27 28 path.moveTo(20, 80); 29 path.bezierCurveTo(90, 10, 160, 150, 190, 10); 30 31 path.moveTo(36, 148); 32 path.quadraticCurveTo(66, 188, 120, 136); 33 path.lineTo(36, 148); 34 35 path.rect(5, 170, 20, 20); 36 37 path.moveTo(150, 180); 38 path.arcTo(150, 100, 50, 200, 20); 39 path.lineTo(160, 160); 40 41 path.moveTo(20, 120); 42 path.arc(20, 120, 18, 0, 1.75 * Math.PI); 43 path.lineTo(20, 120); 44 45 let secondPath = PathKit.NewPath(); 46 secondPath.ellipse(130, 25, 30, 10, -1*Math.PI/8, Math.PI/6, 1.5*Math.PI, false); 47 48 path.addPath(secondPath); 49 50 let m = document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGMatrix(); 51 m.a = 1; m.b = 0; 52 m.c = 0; m.d = 1; 53 m.e = 0; m.f = 20.5; 54 55 path.addPath(secondPath, m); 56 secondPath.delete(); 57 return path; 58 } 59 60 it('path_path2dapi', function(done) { 61 function setup(ctx) { } 62 63 function test(ctx) { 64 path = drawPath(); 65 path.delete(); 66 } 67 68 function teardown(ctx) { } 69 70 LoadPathKit.then(() => { 71 benchmarkAndReport('path_path2dapi', setup, test, teardown).then(() => { 72 done(); 73 }).catch(reportError(done)); 74 }); 75 }); 76 77 describe('import options', function() { 78 it('path_copy', function(done) { 79 function setup(ctx) { 80 ctx.path = PathKit.FromSVGString('M 205,5 L 795,5 L 595,295 L 5,295 L 205,5 z'); 81 } 82 83 function test(ctx) { 84 let p = ctx.path.copy(); 85 p.delete(); 86 } 87 88 function teardown(ctx) { 89 ctx.path.delete(); 90 } 91 92 LoadPathKit.then(() => { 93 benchmarkAndReport('path_copy', setup, test, teardown).then(() => { 94 done(); 95 }).catch(reportError(done)); 96 }); 97 }); 98 99 it('path_from_api_calls', function(done) { 100 function setup(ctx) { } 101 102 function test(ctx) { 103 let p = PathKit.NewPath() 104 .moveTo(205, 5) 105 .lineTo(795, 5) 106 .lineTo(595, 295) 107 .lineTo(5, 295) 108 .lineTo(205, 5) 109 .close(); 110 p.delete(); 111 } 112 113 function teardown(ctx) { } 114 115 LoadPathKit.then(() => { 116 benchmarkAndReport('path_from_api_calls', setup, test, teardown).then(() => { 117 done(); 118 }).catch(reportError(done)); 119 }); 120 }); 121 122 it('path_fromCmds', function(done) { 123 function setup(ctx) { } 124 125 function test(ctx) { 126 let p = PathKit.FromCmds( 127 [[PathKit.MOVE_VERB, 205, 5], 128 [PathKit.LINE_VERB, 795, 5], 129 [PathKit.LINE_VERB, 595, 295], 130 [PathKit.LINE_VERB, 5, 295], 131 [PathKit.LINE_VERB, 205, 5], 132 [PathKit.CLOSE_VERB]]); 133 p.delete(); 134 } 135 136 function teardown(ctx) { } 137 138 LoadPathKit.then(() => { 139 benchmarkAndReport('path_fromCmds', setup, test, teardown).then(() => { 140 done(); 141 }).catch(reportError(done)); 142 }); 143 }); 144 145 it('path_fromSVGString', function(done) { 146 function setup(ctx) {} 147 148 function test(ctx) { 149 // https://upload.wikimedia.org/wikipedia/commons/e/e7/Simple_parallelogram.svg 150 let p = PathKit.FromSVGString('M 205,5 L 795,5 L 595,295 L 5,295 L 205,5 z'); 151 p.delete(); 152 } 153 154 function teardown(ctx) { } 155 156 LoadPathKit.then(() => { 157 benchmarkAndReport('path_fromSVGString', setup, test, teardown).then(() => { 158 done(); 159 }).catch(reportError(done)); 160 }); 161 }); 162 }); 163 164 describe('export options', function() { 165 it('path_toCmds', function(done) { 166 function setup(ctx) { 167 ctx.path = drawPath(); 168 } 169 170 function test(ctx) { 171 ctx.path.toCmds(); 172 } 173 174 function teardown(ctx) { 175 ctx.path.delete(); 176 } 177 178 LoadPathKit.then(() => { 179 benchmarkAndReport('path_toCmds', setup, test, teardown).then(() => { 180 done(); 181 }).catch(reportError(done)); 182 }); 183 }); 184 185 it('path_toPath2D', function(done) { 186 function setup(ctx) { 187 ctx.path = drawPath(); 188 } 189 190 function test(ctx) { 191 ctx.path.toPath2D(); 192 } 193 194 function teardown(ctx) { 195 ctx.path.delete(); 196 } 197 198 LoadPathKit.then(() => { 199 benchmarkAndReport('path_toPath2D', setup, test, teardown).then(() => { 200 done(); 201 }).catch(reportError(done)); 202 }); 203 }); 204 205 it('path_toSVGString', function(done) { 206 function setup(ctx) { 207 ctx.path = drawPath(); 208 } 209 210 function test(ctx) { 211 ctx.path.toSVGString(); 212 } 213 214 function teardown(ctx) { 215 ctx.path.delete(); 216 } 217 218 LoadPathKit.then(() => { 219 benchmarkAndReport('path_toSVGString', setup, test, teardown).then(() => { 220 done(); 221 }).catch(reportError(done)); 222 }); 223 }); 224 }); 225 226 describe('matrix options', function() { 227 function drawTriangle() { 228 let path = PathKit.NewPath(); 229 path.moveTo(0, 0); 230 path.lineTo(10, 0); 231 path.lineTo(10, 10); 232 path.close(); 233 return path; 234 } 235 236 it('path_add_path_svgmatrix', function(done) { 237 function setup(ctx) { 238 ctx.path = drawTriangle(); 239 } 240 241 function test(ctx) { 242 let path = PathKit.NewPath(); 243 let m = document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGMatrix(); 244 m.a = 1; m.b = 0; 245 m.c = 0; m.d = 1; 246 m.e = 0; m.f = 20.5; 247 path.addPath(ctx.path, m); 248 path.delete(); 249 } 250 251 function teardown(ctx) { 252 ctx.path.delete(); 253 } 254 255 LoadPathKit.then(() => { 256 benchmarkAndReport('path_add_path_svgmatrix', setup, test, teardown).then(() => { 257 done(); 258 }).catch(reportError(done)); 259 }); 260 }); 261 262 it('path_add_path_svgmatrix_reuse', function(done) { 263 function setup(ctx) { 264 ctx.path = drawTriangle(); 265 let m = document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGMatrix(); 266 ctx.matrix = m; 267 } 268 269 function test(ctx) { 270 let path = PathKit.NewPath(); 271 let m = ctx.matrix 272 m.a = 1; m.b = 0; 273 m.c = 0; m.d = 1; 274 m.e = 0; m.f = 20.5; 275 path.addPath(ctx.path, m); 276 path.delete(); 277 } 278 279 function teardown(ctx) { 280 ctx.path.delete(); 281 } 282 283 LoadPathKit.then(() => { 284 benchmarkAndReport('path_add_path_svgmatrix_reuse', setup, test, teardown).then(() => { 285 done(); 286 }).catch(reportError(done)); 287 }); 288 }); 289 290 it('path_add_path_svgmatrix_bare', function(done) { 291 function setup(ctx) { 292 ctx.path = drawTriangle(); 293 } 294 295 function test(ctx) { 296 let path = PathKit.NewPath(); 297 path.addPath(ctx.path, 1, 0, 0, 1, 0, 20.5); 298 path.delete(); 299 } 300 301 function teardown(ctx) { 302 ctx.path.delete(); 303 } 304 305 LoadPathKit.then(() => { 306 benchmarkAndReport('path_add_path_svgmatrix_bare', setup, test, teardown).then(() => { 307 done(); 308 }).catch(reportError(done)); 309 }); 310 }); 311 }); 312 313 });