2 <title>Canvas test:
2d.drawImage.outsidesource
</title>
3 <script src=
"/MochiKit/MochiKit.js"></script>
4 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
5 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css">
7 <canvas id=
"c" width=
"100" height=
"50"><p class=
"fallback">FAIL (fallback content)
</p></canvas>
9 function isPixel(ctx
, x
,y
, r
,g
,b
,a
, pos
, colour
, d
) {
10 var pixel
= ctx
.getImageData(x
, y
, 1, 1);
11 var pr
= pixel
.data
[0],
15 ok(r
-d
<= pr
&& pr
<= r
+d
&&
16 g
-d
<= pg
&& pg
<= g
+d
&&
17 b
-d
<= pb
&& pb
<= b
+d
&&
18 a
-d
<= pa
&& pa
<= a
+d
,
19 "pixel "+pos
+" is "+pr
+","+pg
+","+pb
+","+pa
+"; expected "+colour
+" +/- "+d
);
21 function todo_isPixel(ctx
, x
,y
, r
,g
,b
,a
, pos
, colour
, d
) {
22 var pixel
= ctx
.getImageData(x
, y
, 1, 1);
23 var pr
= pixel
.data
[0],
27 todo(r
-d
<= pr
&& pr
<= r
+d
&&
28 g
-d
<= pg
&& pg
<= g
+d
&&
29 b
-d
<= pb
&& pb
<= b
+d
&&
30 a
-d
<= pa
&& pa
<= a
+d
,
31 "pixel "+pos
+" is "+pr
+","+pg
+","+pb
+","+pa
+"; expected "+colour
+" +/- "+d
);
34 SimpleTest
.waitForExplicitFinish();
35 MochiKit
.DOM
.addLoadEvent(function () {
37 var canvas
= document
.getElementById('c');
38 var ctx
= canvas
.getContext('2d');
40 var _thrown_outer
= false;
43 ctx
.drawImage(document
.getElementById('green.png'), 10.5, 10.5, 89.5, 39.5, 0, 0, 100, 50);
44 ctx
.drawImage(document
.getElementById('green.png'), 5.5, 5.5, -5.5, -5.5, 0, 0, 100, 50);
45 ctx
.drawImage(document
.getElementById('green.png'), 100, 50, -5, -5, 0, 0, 100, 50);
46 var _thrown
= undefined; try {
47 ctx
.drawImage(document
.getElementById('red.png'), -0.001, 0, 100, 50, 0, 0, 100, 50);
48 } catch (e
) { _thrown
= e
}; ok(_thrown
&& _thrown
.code
== DOMException
.INDEX_SIZE_ERR
, "should throw INDEX_SIZE_ERR");
49 var _thrown
= undefined; try {
50 ctx
.drawImage(document
.getElementById('red.png'), 0, -0.001, 100, 50, 0, 0, 100, 50);
51 } catch (e
) { _thrown
= e
}; ok(_thrown
&& _thrown
.code
== DOMException
.INDEX_SIZE_ERR
, "should throw INDEX_SIZE_ERR");
52 var _thrown
= undefined; try {
53 ctx
.drawImage(document
.getElementById('red.png'), 0, 0, 100.001, 50, 0, 0, 100, 50);
54 } catch (e
) { _thrown
= e
}; ok(_thrown
&& _thrown
.code
== DOMException
.INDEX_SIZE_ERR
, "should throw INDEX_SIZE_ERR");
55 var _thrown
= undefined; try {
56 ctx
.drawImage(document
.getElementById('red.png'), 0, 0, 100, 50.001, 0, 0, 100, 50);
57 } catch (e
) { _thrown
= e
}; ok(_thrown
&& _thrown
.code
== DOMException
.INDEX_SIZE_ERR
, "should throw INDEX_SIZE_ERR");
58 var _thrown
= undefined; try {
59 ctx
.drawImage(document
.getElementById('red.png'), 50, 0, 50.001, 50, 0, 0, 100, 50);
60 } catch (e
) { _thrown
= e
}; todo(_thrown
&& _thrown
.code
== DOMException
.INDEX_SIZE_ERR
, "should throw INDEX_SIZE_ERR");
61 var _thrown
= undefined; try {
62 ctx
.drawImage(document
.getElementById('red.png'), 0, 0, -5, 5, 0, 0, 100, 50);
63 } catch (e
) { _thrown
= e
}; ok(_thrown
&& _thrown
.code
== DOMException
.INDEX_SIZE_ERR
, "should throw INDEX_SIZE_ERR");
64 var _thrown
= undefined; try {
65 ctx
.drawImage(document
.getElementById('red.png'), 0, 0, 5, -5, 0, 0, 100, 50);
66 } catch (e
) { _thrown
= e
}; ok(_thrown
&& _thrown
.code
== DOMException
.INDEX_SIZE_ERR
, "should throw INDEX_SIZE_ERR");
67 var _thrown
= undefined; try {
68 ctx
.drawImage(document
.getElementById('red.png'), 110, 60, -20, -20, 0, 0, 100, 50);
69 } catch (e
) { _thrown
= e
}; ok(_thrown
&& _thrown
.code
== DOMException
.INDEX_SIZE_ERR
, "should throw INDEX_SIZE_ERR");
70 todo_isPixel(ctx
, 50,25, 0,255,0,255, "50,25", "0,255,0,255", 2);
75 todo(!_thrown_outer
, 'should not throw exception');
81 <img src=
"image_green.png" id=
"green.png" class=
"resource">
82 <img src=
"image_red.png" id=
"red.png" class=
"resource">