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 that non-invertible transforms are reflected in currentTransform"); | |
177 setCurrentTransform(ctx, 1, 0, 0, 1, 1, 2); | |
178 ctx.scale(0, 0); | |
179 matrix = ctx.currentTransform; | |
180 shouldBe("matrix.a", "0"); | |
181 shouldBe("matrix.b", "0"); | |
182 shouldBe("matrix.c", "0"); | |
183 shouldBe("matrix.d", "0"); | |
184 shouldBe("matrix.e", "1"); | |
185 shouldBe("matrix.f", "2"); | |
186 setCurrentTransform(ctx, 1, 0, 0, 1, 0, 0); | |
187 | |
188 debug("Check assigning an invalid object throws exception as expected"); | |
189 shouldThrow("ctx.currentTransform = ctx", '"TypeError: Failed to set the \'curre
ntTransform\' property on \'CanvasRenderingContext2D\': The provided value is no
t of type \'SVGMatrix\'."'); | |
190 shouldThrow("ctx.currentTransform = undefined", '"TypeError: Failed to set the \
'currentTransform\' property on \'CanvasRenderingContext2D\': The provided value
is not of type \'SVGMatrix\'."'); | |
191 shouldThrow("ctx.currentTransform = null", '"TypeError: Failed to set the \'curr
entTransform\' property on \'CanvasRenderingContext2D\': The provided value is n
ot of type \'SVGMatrix\'."'); | |
192 | |
193 debug("Check handling non-finite values. see 2d.transformation.setTransform.nonf
inite.html"); | |
194 ctx.fillStyle = 'red'; | |
195 ctx.fillRect(0, 0, 100, 100); | |
196 | |
197 function setCurrentTransformToNonfinite(ctx, a, b, c, d, e, f) | |
198 { | |
199 matrix.a = a; | |
200 matrix.b = b; | |
201 matrix.c = c; | |
202 matrix.d = d; | |
203 matrix.e = e; | |
204 matrix.f = f; | |
205 ctx.currentTransform = matrix; | |
206 matrix.a = NaN; | |
207 matrix.b = NaN; | |
208 matrix.c = NaN; | |
209 matrix.d = NaN; | |
210 matrix.e = NaN; | |
211 matrix.f = NaN; | |
212 matrix = ctx.currentTransform; | |
213 shouldBe("matrix.a", "1"); | |
214 shouldBe("matrix.b", "0"); | |
215 shouldBe("matrix.c", "0"); | |
216 shouldBe("matrix.d", "1"); | |
217 shouldBe("matrix.e", "100"); | |
218 shouldBe("matrix.f", "10"); | |
219 } | |
220 | |
221 ctx.translate(100, 10); | |
222 matrix = ctx.currentTransform; | |
223 shouldBe("matrix.a", "1"); | |
224 shouldBe("matrix.b", "0"); | |
225 shouldBe("matrix.c", "0"); | |
226 shouldBe("matrix.d", "1"); | |
227 shouldBe("matrix.e", "100"); | |
228 shouldBe("matrix.f", "10"); | |
229 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, 0, 0, 0); | |
230 setCurrentTransformToNonfinite(ctx, -Infinity, 0, 0, 0, 0, 0); | |
231 setCurrentTransformToNonfinite(ctx, NaN, 0, 0, 0, 0, 0); | |
232 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, 0, 0, 0); | |
233 setCurrentTransformToNonfinite(ctx, 0, -Infinity, 0, 0, 0, 0); | |
234 setCurrentTransformToNonfinite(ctx, 0, NaN, 0, 0, 0, 0); | |
235 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, 0, 0, 0); | |
236 setCurrentTransformToNonfinite(ctx, 0, 0, -Infinity, 0, 0, 0); | |
237 setCurrentTransformToNonfinite(ctx, 0, 0, NaN, 0, 0, 0); | |
238 setCurrentTransformToNonfinite(ctx, 0, 0, 0, Infinity, 0, 0); | |
239 setCurrentTransformToNonfinite(ctx, 0, 0, 0, -Infinity, 0, 0); | |
240 setCurrentTransformToNonfinite(ctx, 0, 0, 0, NaN, 0, 0); | |
241 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, Infinity, 0); | |
242 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, -Infinity, 0); | |
243 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, NaN, 0); | |
244 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, 0, Infinity); | |
245 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, 0, -Infinity); | |
246 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, 0, NaN); | |
247 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, 0, 0, 0); | |
248 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, 0, 0, 0); | |
249 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, Infinity, 0, 0
); | |
250 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, Infinity, Infi
nity, 0); | |
251 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, Infinity, Infi
nity, Infinity); | |
252 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, Infinity, 0, I
nfinity); | |
253 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, 0, Infinity, 0
); | |
254 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, 0, Infinity, I
nfinity); | |
255 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, Infinity, 0, 0, Infinity
); | |
256 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, Infinity, 0, 0); | |
257 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, Infinity, Infinity, 0
); | |
258 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, Infinity, Infinity, I
nfinity); | |
259 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, Infinity, 0, Infinity
); | |
260 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, 0, Infinity, 0); | |
261 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, 0, Infinity, Infinity
); | |
262 setCurrentTransformToNonfinite(ctx, Infinity, Infinity, 0, 0, 0, Infinity); | |
263 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, 0, 0, 0); | |
264 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, Infinity, 0, 0); | |
265 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, Infinity, Infinity, 0
); | |
266 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, Infinity, Infinity, I
nfinity); | |
267 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, Infinity, 0, Infinity
); | |
268 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, 0, Infinity, 0); | |
269 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, 0, Infinity, Infinity
); | |
270 setCurrentTransformToNonfinite(ctx, Infinity, 0, Infinity, 0, 0, Infinity); | |
271 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, Infinity, 0, 0); | |
272 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, Infinity, Infinity, 0); | |
273 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, Infinity, Infinity, Infinity
); | |
274 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, Infinity, 0, Infinity); | |
275 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, 0, Infinity, 0); | |
276 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, 0, Infinity, Infinity); | |
277 setCurrentTransformToNonfinite(ctx, Infinity, 0, 0, 0, 0, Infinity); | |
278 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, 0, 0, 0); | |
279 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, Infinity, 0, 0); | |
280 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, Infinity, Infinity, 0
); | |
281 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, Infinity, Infinity, I
nfinity); | |
282 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, Infinity, 0, Infinity
); | |
283 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, 0, Infinity, 0); | |
284 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, 0, Infinity, Infinity
); | |
285 setCurrentTransformToNonfinite(ctx, 0, Infinity, Infinity, 0, 0, Infinity); | |
286 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, Infinity, 0, 0); | |
287 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, Infinity, Infinity, 0); | |
288 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, Infinity, Infinity, Infinity
); | |
289 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, Infinity, 0, Infinity); | |
290 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, 0, Infinity, 0); | |
291 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, 0, Infinity, Infinity); | |
292 setCurrentTransformToNonfinite(ctx, 0, Infinity, 0, 0, 0, Infinity); | |
293 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, Infinity, 0, 0); | |
294 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, Infinity, Infinity, 0); | |
295 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, Infinity, Infinity, Infinity
); | |
296 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, Infinity, 0, Infinity); | |
297 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, 0, Infinity, 0); | |
298 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, 0, Infinity, Infinity); | |
299 setCurrentTransformToNonfinite(ctx, 0, 0, Infinity, 0, 0, Infinity); | |
300 setCurrentTransformToNonfinite(ctx, 0, 0, 0, Infinity, Infinity, 0); | |
301 setCurrentTransformToNonfinite(ctx, 0, 0, 0, Infinity, Infinity, Infinity); | |
302 setCurrentTransformToNonfinite(ctx, 0, 0, 0, Infinity, 0, Infinity); | |
303 setCurrentTransformToNonfinite(ctx, 0, 0, 0, 0, Infinity, Infinity); | |
304 matrix = ctx.currentTransform; | |
305 shouldBe("matrix.a", "1"); | |
306 shouldBe("matrix.b", "0"); | |
307 shouldBe("matrix.c", "0"); | |
308 shouldBe("matrix.d", "1"); | |
309 shouldBe("matrix.e", "100"); | |
310 shouldBe("matrix.f", "10"); | |
311 | |
312 ctx.fillStyle = 'green'; | |
313 ctx.fillRect(-100, -10, 100, 100); | |
314 | |
315 imageData = ctx.getImageData(1, 1, 98, 98); | |
316 imgdata = imageData.data; | |
317 shouldBe("imgdata[4]", "0"); | |
318 shouldBe("imgdata[5]", "128"); | |
319 shouldBe("imgdata[6]", "0"); | |
OLD | NEW |