1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
- name: 2d.transformation.order
desc: Transformations are applied in the right order
testing:
- 2d.transformation.order
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.scale(2, 1);
ctx.rotate(Math.PI / 2);
ctx.fillStyle = '#0f0';
ctx.fillRect(0, -50, 50, 50);
@assert pixel 75,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.scale.basic
desc: scale() works
testing:
- 2d.transformation.scale
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.scale(2, 4);
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 50, 12.5);
@assert pixel 90,40 == 0,255,0,255;
expected: green
- name: 2d.transformation.scale.zero
desc: scale() with a scale factor of zero works
testing:
- 2d.transformation.scale
code: |
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
ctx.save();
ctx.translate(50, 0);
ctx.scale(0, 1);
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.restore();
ctx.save();
ctx.translate(0, 25);
ctx.scale(1, 0);
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.restore();
canvas.toDataURL();
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.scale.negative
desc: scale() with negative scale factors works
testing:
- 2d.transformation.scale
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.save();
ctx.scale(-1, 1);
ctx.fillStyle = '#0f0';
ctx.fillRect(-50, 0, 50, 50);
ctx.restore();
ctx.save();
ctx.scale(1, -1);
ctx.fillStyle = '#0f0';
ctx.fillRect(50, -50, 50, 50);
ctx.restore();
@assert pixel 25,25 == 0,255,0,255;
@assert pixel 75,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.scale.large
desc: scale() with large scale factors works
notes: Not really that large at all, but it hits the limits in Firefox.
testing:
- 2d.transformation.scale
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.scale(1e5, 1e5);
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 1, 1);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.scale.nonfinite
desc: scale() with Infinity/NaN is ignored
testing:
- 2d.nonfinite
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.translate(100, 10);
@nonfinite ctx.scale(<0.1 Infinity -Infinity NaN>, <0.1 Infinity -Infinity NaN>);
ctx.fillStyle = '#0f0';
ctx.fillRect(-100, -10, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.scale.multiple
desc: Multiple scale()s combine
testing:
- 2d.transformation.scale.multiple
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.scale(Math.sqrt(2), Math.sqrt(2));
ctx.scale(Math.sqrt(2), Math.sqrt(2));
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 50, 25);
@assert pixel 90,40 == 0,255,0,255;
expected: green
- name: 2d.transformation.rotate.zero
desc: rotate() by 0 does nothing
testing:
- 2d.transformation.rotate
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.rotate(0);
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.rotate.radians
desc: rotate() uses radians
testing:
- 2d.transformation.rotate.radians
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.rotate(Math.PI); // should fail obviously if this is 3.1 degrees
ctx.fillStyle = '#0f0';
ctx.fillRect(-100, -50, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.rotate.direction
desc: rotate() is clockwise
testing:
- 2d.transformation.rotate.direction
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.rotate(Math.PI / 2);
ctx.fillStyle = '#0f0';
ctx.fillRect(0, -100, 50, 100);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.rotate.wrap
desc: rotate() wraps large positive values correctly
testing:
- 2d.transformation.rotate
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.rotate(Math.PI * (1 + 4096)); // == pi (mod 2*pi)
// We need about pi +/- 0.001 in order to get correct-looking results
// 32-bit floats can store pi*4097 with precision 2^-10, so that should
// be safe enough on reasonable implementations
ctx.fillStyle = '#0f0';
ctx.fillRect(-100, -50, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
@assert pixel 98,2 == 0,255,0,255;
@assert pixel 98,47 == 0,255,0,255;
expected: green
- name: 2d.transformation.rotate.wrapnegative
desc: rotate() wraps large negative values correctly
testing:
- 2d.transformation.rotate
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.rotate(-Math.PI * (1 + 4096));
ctx.fillStyle = '#0f0';
ctx.fillRect(-100, -50, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
@assert pixel 98,2 == 0,255,0,255;
@assert pixel 98,47 == 0,255,0,255;
expected: green
- name: 2d.transformation.rotate.nonfinite
desc: rotate() with Infinity/NaN is ignored
testing:
- 2d.nonfinite
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.translate(100, 10);
@nonfinite ctx.rotate(<0.1 Infinity -Infinity NaN>);
ctx.fillStyle = '#0f0';
ctx.fillRect(-100, -10, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.translate.basic
desc: translate() works
testing:
- 2d.transformation.translate
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.translate(100, 50);
ctx.fillStyle = '#0f0';
ctx.fillRect(-100, -50, 100, 50);
@assert pixel 90,40 == 0,255,0,255;
expected: green
- name: 2d.transformation.translate.nonfinite
desc: translate() with Infinity/NaN is ignored
testing:
- 2d.nonfinite
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.translate(100, 10);
@nonfinite ctx.translate(<0.1 Infinity -Infinity NaN>, <0.1 Infinity -Infinity NaN>);
ctx.fillStyle = '#0f0';
ctx.fillRect(-100, -10, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.transform.identity
desc: transform() with the identity matrix does nothing
testing:
- 2d.transformation.transform
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.transform(1,0, 0,1, 0,0);
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.transform.skewed
desc: transform() with skewy matrix transforms correctly
testing:
- 2d.transformation.transform
code: |
// Create green with a red square ring inside it
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
ctx.fillStyle = '#f00';
ctx.fillRect(20, 10, 60, 30);
ctx.fillStyle = '#0f0';
ctx.fillRect(40, 20, 20, 10);
// Draw a skewed shape to fill that gap, to make sure it is aligned correctly
ctx.transform(1,4, 2,3, 5,6);
// Post-transform coordinates:
// [[20,10],[80,10],[80,40],[20,40],[20,10],[40,20],[40,30],[60,30],[60,20],[40,20],[20,10]];
// Hence pre-transform coordinates:
var pts=[[-7.4,11.2],[-43.4,59.2],[-31.4,53.2],[4.6,5.2],[-7.4,11.2],
[-15.4,25.2],[-11.4,23.2],[-23.4,39.2],[-27.4,41.2],[-15.4,25.2],
[-7.4,11.2]];
ctx.beginPath();
ctx.moveTo(pts[0][0], pts[0][1]);
for (var i = 0; i < pts.length; ++i)
ctx.lineTo(pts[i][0], pts[i][1]);
ctx.fill();
@assert pixel 21,11 == 0,255,0,255;
@assert pixel 79,11 == 0,255,0,255;
@assert pixel 21,39 == 0,255,0,255;
@assert pixel 79,39 == 0,255,0,255;
@assert pixel 39,19 == 0,255,0,255;
@assert pixel 61,19 == 0,255,0,255;
@assert pixel 39,31 == 0,255,0,255;
@assert pixel 61,31 == 0,255,0,255;
expected: green
- name: 2d.transformation.transform.multiply
desc: transform() multiplies the CTM
testing:
- 2d.transformation.transform.multiply
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.transform(1,2, 3,4, 5,6);
ctx.transform(-2,1, 3/2,-1/2, 1,-2);
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.transform.nonfinite
desc: transform() with Infinity/NaN is ignored
testing:
- 2d.nonfinite
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.translate(100, 10);
@nonfinite ctx.transform(<0 Infinity -Infinity NaN>, <0 Infinity -Infinity NaN>, <0 Infinity -Infinity NaN>, <0 Infinity -Infinity NaN>, <0 Infinity -Infinity NaN>, <0 Infinity -Infinity NaN>);
ctx.fillStyle = '#0f0';
ctx.fillRect(-100, -10, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
- name: 2d.transformation.setTransform.skewed
testing:
- 2d.transformation.setTransform
code: |
// Create green with a red square ring inside it
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 100, 50);
ctx.fillStyle = '#f00';
ctx.fillRect(20, 10, 60, 30);
ctx.fillStyle = '#0f0';
ctx.fillRect(40, 20, 20, 10);
// Draw a skewed shape to fill that gap, to make sure it is aligned correctly
ctx.setTransform(1,4, 2,3, 5,6);
// Post-transform coordinates:
// [[20,10],[80,10],[80,40],[20,40],[20,10],[40,20],[40,30],[60,30],[60,20],[40,20],[20,10]];
// Hence pre-transform coordinates:
var pts=[[-7.4,11.2],[-43.4,59.2],[-31.4,53.2],[4.6,5.2],[-7.4,11.2],
[-15.4,25.2],[-11.4,23.2],[-23.4,39.2],[-27.4,41.2],[-15.4,25.2],
[-7.4,11.2]];
ctx.beginPath();
ctx.moveTo(pts[0][0], pts[0][1]);
for (var i = 0; i < pts.length; ++i)
ctx.lineTo(pts[i][0], pts[i][1]);
ctx.fill();
@assert pixel 21,11 == 0,255,0,255;
@assert pixel 79,11 == 0,255,0,255;
@assert pixel 21,39 == 0,255,0,255;
@assert pixel 79,39 == 0,255,0,255;
@assert pixel 39,19 == 0,255,0,255;
@assert pixel 61,19 == 0,255,0,255;
@assert pixel 39,31 == 0,255,0,255;
@assert pixel 61,31 == 0,255,0,255;
expected: green
- name: 2d.transformation.setTransform.multiple
testing:
- 2d.transformation.setTransform.identity
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.setTransform(1/2,0, 0,1/2, 0,0);
ctx.setTransform();
ctx.setTransform(2,0, 0,2, 0,0);
ctx.fillStyle = '#0f0';
ctx.fillRect(0, 0, 50, 25);
@assert pixel 75,35 == 0,255,0,255;
expected: green
- name: 2d.transformation.setTransform.nonfinite
desc: setTransform() with Infinity/NaN is ignored
testing:
- 2d.nonfinite
code: |
ctx.fillStyle = '#f00';
ctx.fillRect(0, 0, 100, 50);
ctx.translate(100, 10);
@nonfinite ctx.setTransform(<0 Infinity -Infinity NaN>, <0 Infinity -Infinity NaN>, <0 Infinity -Infinity NaN>, <0 Infinity -Infinity NaN>, <0 Infinity -Infinity NaN>, <0 Infinity -Infinity NaN>);
ctx.fillStyle = '#0f0';
ctx.fillRect(-100, -10, 100, 50);
@assert pixel 50,25 == 0,255,0,255;
expected: green
|