OLD | NEW |
(Empty) | |
| 1 description("Series of tests to ensure correct behaviour of canvas.currentTransf
orm"); |
| 2 var ctx = document.createElement('canvas').getContext('2d'); |
| 3 |
| 4 var matrix = ctx.currentTransform; |
| 5 |
| 6 debug("Check initial currentTransform values"); |
| 7 shouldBe("matrix.a", "1"); |
| 8 shouldBe("matrix.b", "0"); |
| 9 shouldBe("matrix.c", "0"); |
| 10 shouldBe("matrix.d", "1"); |
| 11 shouldBe("matrix.e", "0"); |
| 12 shouldBe("matrix.f", "0"); |
| 13 |
| 14 function setCurrentTransform(ctx, a, b, c, d, e, f) |
| 15 { |
| 16 matrix.a = a; |
| 17 matrix.b = b; |
| 18 matrix.c = c; |
| 19 matrix.d = d; |
| 20 matrix.e = e; |
| 21 matrix.f = f; |
| 22 ctx.currentTransform = matrix; |
| 23 matrix.a = NaN; |
| 24 matrix.b = NaN; |
| 25 matrix.c = NaN; |
| 26 matrix.d = NaN; |
| 27 matrix.e = NaN; |
| 28 matrix.f = NaN; |
| 29 matrix = ctx.currentTransform; |
| 30 shouldBe("matrix.a", "" + a); |
| 31 shouldBe("matrix.b", "" + b); |
| 32 shouldBe("matrix.c", "" + c); |
| 33 shouldBe("matrix.d", "" + d); |
| 34 shouldBe("matrix.e", "" + e); |
| 35 shouldBe("matrix.f", "" + f); |
| 36 } |
| 37 |
| 38 matrix.a = 2; |
| 39 debug("Changing matrix should not affect the CTM"); |
| 40 shouldBe("ctx.currentTransform.a", "1"); |
| 41 shouldBe("ctx.currentTransform.b", "0"); |
| 42 shouldBe("ctx.currentTransform.c", "0"); |
| 43 shouldBe("ctx.currentTransform.d", "1"); |
| 44 shouldBe("ctx.currentTransform.e", "0"); |
| 45 shouldBe("ctx.currentTransform.f", "0"); |
| 46 |
| 47 debug("Reset the CTM to the initial matrix"); |
| 48 ctx.beginPath(); |
| 49 ctx.scale(0.5, 0.5); |
| 50 matrix = ctx.currentTransform; |
| 51 shouldBe("matrix.a", "0.5"); |
| 52 shouldBe("matrix.b", "0"); |
| 53 shouldBe("matrix.c", "0"); |
| 54 shouldBe("matrix.d", "0.5"); |
| 55 shouldBe("matrix.e", "0"); |
| 56 shouldBe("matrix.f", "0"); |
| 57 setCurrentTransform(ctx, 1, 0, 0, 1, 0, 0); |
| 58 ctx.fillStyle = 'green'; |
| 59 ctx.fillRect(0, 0, 100, 100); |
| 60 |
| 61 var imageData = ctx.getImageData(1, 1, 98, 98); |
| 62 var imgdata = imageData.data; |
| 63 shouldBe("imgdata[4]", "0"); |
| 64 shouldBe("imgdata[5]", "128"); |
| 65 shouldBe("imgdata[6]", "0"); |
| 66 |
| 67 debug("currentTransform should not affect the current path"); |
| 68 ctx.beginPath(); |
| 69 ctx.rect(0,0,100,100); |
| 70 ctx.save(); |
| 71 setCurrentTransform(ctx, 0.5, 0, 0, 0.5, 10, 10); |
| 72 ctx.fillStyle = 'red'; |
| 73 ctx.fillRect(0, 0, 100, 100); |
| 74 ctx.restore(); |
| 75 matrix = ctx.currentTransform; |
| 76 shouldBe("matrix.a", "1"); |
| 77 shouldBe("matrix.b", "0"); |
| 78 shouldBe("matrix.c", "0"); |
| 79 shouldBe("matrix.d", "1"); |
| 80 shouldBe("matrix.e", "0"); |
| 81 shouldBe("matrix.f", "0"); |
| 82 ctx.fillStyle = 'green'; |
| 83 ctx.fillRect(0, 0, 100, 100); |
| 84 |
| 85 imageData = ctx.getImageData(1, 1, 98, 98); |
| 86 imgdata = imageData.data; |
| 87 shouldBe("imgdata[4]", "0"); |
| 88 shouldBe("imgdata[5]", "128"); |
| 89 shouldBe("imgdata[6]", "0"); |
| 90 |
| 91 debug("currentTransform should not affect the CTM outside of save() and restore(
)"); |
| 92 ctx.beginPath(); |
| 93 ctx.fillStyle = 'green'; |
| 94 ctx.save(); |
| 95 setCurrentTransform(ctx, 0.5, 0, 0, 0.5, 0, 0); |
| 96 ctx.fillStyle = 'red'; |
| 97 ctx.fillRect(0, 0, 100, 100); |
| 98 ctx.restore(); |
| 99 matrix = ctx.currentTransform; |
| 100 shouldBe("matrix.a", "1"); |
| 101 shouldBe("matrix.b", "0"); |
| 102 shouldBe("matrix.c", "0"); |
| 103 shouldBe("matrix.d", "1"); |
| 104 shouldBe("matrix.e", "0"); |
| 105 shouldBe("matrix.f", "0"); |
| 106 ctx.fillRect(0, 0, 100, 100); |
| 107 |
| 108 imageData = ctx.getImageData(1, 1, 98, 98); |
| 109 imgdata = imageData.data; |
| 110 shouldBe("imgdata[4]", "0"); |
| 111 shouldBe("imgdata[5]", "128"); |
| 112 shouldBe("imgdata[6]", "0"); |
| 113 |
| 114 debug("stop drawing on not-invertible CTM"); |
| 115 ctx.beginPath(); |
| 116 ctx.fillStyle = 'green'; |
| 117 ctx.fillRect(0, 0, 100, 100); |
| 118 setCurrentTransform(ctx, 0, 0, 0, 0, 0, 0); |
| 119 ctx.fillStyle = 'red'; |
| 120 ctx.fillRect(0, 0, 100, 100); |
| 121 |
| 122 imageData = ctx.getImageData(1, 1, 98, 98); |
| 123 imgdata = imageData.data; |
| 124 shouldBe("imgdata[4]", "0"); |
| 125 shouldBe("imgdata[5]", "128"); |
| 126 shouldBe("imgdata[6]", "0"); |
| 127 |
| 128 debug("currentTransform with a not-invertible matrix should only stop the drawin
g up to the next restore()"); |
| 129 ctx.beginPath(); |
| 130 ctx.resetTransform(); |
| 131 matrix = ctx.currentTransform; |
| 132 shouldBe("matrix.a", "1"); |
| 133 shouldBe("matrix.b", "0"); |
| 134 shouldBe("matrix.c", "0"); |
| 135 shouldBe("matrix.d", "1"); |
| 136 shouldBe("matrix.e", "0"); |
| 137 shouldBe("matrix.f", "0"); |
| 138 ctx.save(); |
| 139 setCurrentTransform(ctx, 0, 0, 0, 0, 0, 0); |
| 140 ctx.fillStyle = 'red'; |
| 141 ctx.fillRect(0, 0, 100, 100); |
| 142 ctx.restore(); |
| 143 matrix = ctx.currentTransform; |
| 144 shouldBe("matrix.a", "1"); |
| 145 shouldBe("matrix.b", "0"); |
| 146 shouldBe("matrix.c", "0"); |
| 147 shouldBe("matrix.d", "1"); |
| 148 shouldBe("matrix.e", "0"); |
| 149 shouldBe("matrix.f", "0"); |
| 150 ctx.fillStyle = 'blue'; |
| 151 ctx.fillRect(0, 0, 100, 100); |
| 152 |
| 153 imageData = ctx.getImageData(1, 1, 98, 98); |
| 154 imgdata = imageData.data; |
| 155 shouldBe("imgdata[4]", "0"); |
| 156 shouldBe("imgdata[5]", "0"); |
| 157 shouldBe("imgdata[6]", "255"); |
| 158 |
| 159 debug("currentTransform should set transform although CTM is not-invertible"); |
| 160 ctx.beginPath(); |
| 161 ctx.fillStyle = 'red'; |
| 162 ctx.fillRect(0, 0, 100, 100); |
| 163 setCurrentTransform(ctx, 0, 0, 0, 0, 0, 0); |
| 164 ctx.fillStyle = 'green'; |
| 165 ctx.fillRect(0, 0, 100, 100); |
| 166 setCurrentTransform(ctx, 1, 0, 0, 1, 0, 0); |
| 167 ctx.fillStyle = 'blue'; |
| 168 ctx.fillRect(0, 0, 100, 100); |
| 169 |
| 170 imageData = ctx.getImageData(1, 1, 98, 98); |
| 171 imgdata = imageData.data; |
| 172 shouldBe("imgdata[4]", "0"); |
| 173 shouldBe("imgdata[5]", "0"); |
| 174 shouldBe("imgdata[6]", "255"); |
| 175 |
| 176 debug("Check assigning an invalid object throws exception as expected"); |
| 177 shouldThrow("ctx.currentTransform = ctx", "'TypeError: Type error'"); |
| 178 |
| 179 debug("Check handling non-finite values. see 2d.transformation.setTransform.nonf
inite.html"); |
| 180 ctx.fillStyle = 'red'; |
| 181 ctx.fillRect(0, 0, 100, 100); |
| 182 |
| 183 function setCurrentTransformToNonfinite(ctx, a, b, c, d, e, f) |
| 184 { |
| 185 matrix.a = a; |
| 186 matrix.b = b; |
| 187 matrix.c = c; |
| 188 matrix.d = d; |
| 189 matrix.e = e; |
| 190 matrix.f = f; |
| 191 ctx.currentTransform = matrix; |
| 192 matrix.a = NaN; |
| 193 matrix.b = NaN; |
| 194 matrix.c = NaN; |
| 195 matrix.d = NaN; |
| 196 matrix.e = NaN; |
| 197 matrix.f = NaN; |
| 198 matrix = ctx.currentTransform; |
| 199 shouldBe("matrix.a", "1"); |
| 200 shouldBe("matrix.b", "0"); |
| 201 shouldBe("matrix.c", "0"); |
| 202 shouldBe("matrix.d", "1"); |
| 203 shouldBe("matrix.e", "100"); |
| 204 shouldBe("matrix.f", "10"); |
| 205 } |
| 206 |
| 207 ctx.translate(100, 10); |
| 208 matrix = ctx.currentTransform; |
| 209 shouldBe("matrix.a", "1"); |
| 210 shouldBe("matrix.b", "0"); |
| 211 shouldBe("matrix.c", "0"); |
| 212 shouldBe("matrix.d", "1"); |
| 213 shouldBe("matrix.e", "100"); |
| 214 shouldBe("matrix.f", "10"); |
| 215 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, 0, 0, 0); |
| 216 setCurrentTransformToNonfinite(ctx, -Infinity, 0, 0, 0, 0, 0); |
| 217 setCurrentTransformToNonfinite(ctx, NaN, 0, 0, 0, 0, 0); |
| 218 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, 0, 0, 0); |
| 219 setCurrentTransformToNonfinite(ctx, 0, -Infinity, 0, 0, 0, 0); |
| 220 setCurrentTransformToNonfinite(ctx, 0, NaN, 0, 0, 0, 0); |
| 221 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, 0, 0, 0); |
| 222 setCurrentTransformToNonfinite(ctx, 0, 0, -Infinity, 0, 0, 0); |
| 223 setCurrentTransformToNonfinite(ctx, 0, 0, NaN, 0, 0, 0); |
| 224 setCurrentTransformToNonfinite(ctx, 0, 0, 0, Infinity, 0, 0); |
| 225 setCurrentTransformToNonfinite(ctx, 0, 0, 0, -Infinity, 0, 0); |
| 226 setCurrentTransformToNonfinite(ctx, 0, 0, 0, NaN, 0, 0); |
| 227 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, Infinity, 0); |
| 228 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, -Infinity, 0); |
| 229 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, NaN, 0); |
| 230 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, 0, Infinity); |
| 231 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, 0, -Infinity); |
| 232 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, 0, NaN); |
| 233 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, 0, 0, 0); |
| 234 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, 0, 0, 0); |
| 235 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, Infinity, 0, 0
); |
| 236 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, Infinity, Infi
nity, 0); |
| 237 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, Infinity, Infi
nity, Infinity); |
| 238 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, Infinity, 0, I
nfinity); |
| 239 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, 0, Infinity, 0
); |
| 240 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, 0, Infinity, I
nfinity); |
| 241 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, 0, 0, Infinity
); |
| 242 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, Infinity, 0, 0); |
| 243 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, Infinity, Infinity, 0
); |
| 244 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, Infinity, Infinity, I
nfinity); |
| 245 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, Infinity, 0, Infinity
); |
| 246 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, 0, Infinity, 0); |
| 247 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, 0, Infinity, Infinity
); |
| 248 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, 0, 0, Infinity); |
| 249 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, 0, 0, 0); |
| 250 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, Infinity, 0, 0); |
| 251 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, Infinity, Infinity, 0
); |
| 252 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, Infinity, Infinity, I
nfinity); |
| 253 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, Infinity, 0, Infinity
); |
| 254 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, 0, Infinity, 0); |
| 255 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, 0, Infinity, Infinity
); |
| 256 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, 0, 0, Infinity); |
| 257 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, Infinity, 0, 0); |
| 258 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, Infinity, Infinity, 0); |
| 259 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, Infinity, Infinity, Infinity
); |
| 260 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, Infinity, 0, Infinity); |
| 261 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, 0, Infinity, 0); |
| 262 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, 0, Infinity, Infinity); |
| 263 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, 0, 0, Infinity); |
| 264 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, 0, 0, 0); |
| 265 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, Infinity, 0, 0); |
| 266 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, Infinity, Infinity, 0
); |
| 267 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, Infinity, Infinity, I
nfinity); |
| 268 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, Infinity, 0, Infinity
); |
| 269 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, 0, Infinity, 0); |
| 270 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, 0, Infinity, Infinity
); |
| 271 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, 0, 0, Infinity); |
| 272 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, Infinity, 0, 0); |
| 273 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, Infinity, Infinity, 0); |
| 274 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, Infinity, Infinity, Infinity
); |
| 275 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, Infinity, 0, Infinity); |
| 276 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, 0, Infinity, 0); |
| 277 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, 0, Infinity, Infinity); |
| 278 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, 0, 0, Infinity); |
| 279 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, Infinity, 0, 0); |
| 280 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, Infinity, Infinity, 0); |
| 281 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, Infinity, Infinity, Infinity
); |
| 282 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, Infinity, 0, Infinity); |
| 283 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, 0, Infinity, 0); |
| 284 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, 0, Infinity, Infinity); |
| 285 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, 0, 0, Infinity); |
| 286 setCurrentTransformToNonfinite(ctx, 0, 0, 0, Infinity, Infinity, 0); |
| 287 setCurrentTransformToNonfinite(ctx, 0, 0, 0, Infinity, Infinity, Infinity); |
| 288 setCurrentTransformToNonfinite(ctx, 0, 0, 0, Infinity, 0, Infinity); |
| 289 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, Infinity, Infinity); |
| 290 matrix = ctx.currentTransform; |
| 291 shouldBe("matrix.a", "1"); |
| 292 shouldBe("matrix.b", "0"); |
| 293 shouldBe("matrix.c", "0"); |
| 294 shouldBe("matrix.d", "1"); |
| 295 shouldBe("matrix.e", "100"); |
| 296 shouldBe("matrix.f", "10"); |
| 297 |
| 298 ctx.fillStyle = 'green'; |
| 299 ctx.fillRect(-100, -10, 100, 100); |
| 300 |
| 301 imageData = ctx.getImageData(1, 1, 98, 98); |
| 302 imgdata = imageData.data; |
| 303 shouldBe("imgdata[4]", "0"); |
| 304 shouldBe("imgdata[5]", "128"); |
| 305 shouldBe("imgdata[6]", "0"); |
OLD | NEW |