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