1 description("Ensure correct behavior of canvas with fillRect shadow");
3 function print(message
, color
)
5 var paragraph
= document
.createElement("div");
6 paragraph
.appendChild(document
.createTextNode(message
));
7 paragraph
.style
.fontFamily
= "monospace";
9 paragraph
.style
.color
= color
;
10 document
.getElementById("console").appendChild(paragraph
);
13 function shouldBeAround(a
, b
)
22 if (Math
.abs(evalA
- b
) < 5)
23 print("PASS " + a
+ " is around " + b
, "green")
25 print("FAIL " + a
+ " is not around " + b
+ " (actual: " + evalA
+ ")", "red");
28 function shouldBeSmaller(a
, b
)
38 print("PASS " + a
+ " is smaller than " + b
, "green")
40 print("FAIL " + a
+ " is not smaller than " + b
+ " (actual: " + evalA
+ ")", "red");
43 var canvas
= document
.createElement('canvas');
44 document
.body
.appendChild(canvas
);
45 canvas
.setAttribute('width', '400');
46 canvas
.setAttribute('height', '650');
47 var ctx
= canvas
.getContext('2d');
49 ctx
.shadowOffsetX
= 200;
50 ctx
.fillStyle
= 'rgba(0, 0, 255, 1)';
52 ctx
.shadowColor
= 'rgba(255, 0, 0, 1.0)';
53 ctx
.fillRect(50, 50, 100, 100);
55 ctx
.shadowColor
= 'rgba(255, 0, 0, 0.3)';
56 ctx
.fillRect(50, 200, 100, 100);
58 ctx
.shadowColor
= 'rgba(255, 0, 0, 1.0)';
60 ctx
.fillRect(50, 350, 100, 100);
62 ctx
.shadowColor
= 'rgba(255, 0, 0, 0.3)';
63 ctx
.fillRect(50, 500, 100, 100);
65 var d
; // imageData.data
67 // Verify solid shadow.
68 d
= ctx
.getImageData(250, 50, 1, 1).data
;
69 shouldBe('d[0]', '255');
70 shouldBe('d[1]', '0');
71 shouldBe('d[2]', '0');
72 shouldBe('d[3]', '255');
74 d
= ctx
.getImageData(250, 149, 1, 1).data
;
75 shouldBe('d[0]', '255');
76 shouldBe('d[1]', '0');
77 shouldBe('d[2]', '0');
78 shouldBe('d[3]', '255');
80 d
= ctx
.getImageData(349, 50, 1, 1).data
;
81 shouldBe('d[0]', '255');
82 shouldBe('d[1]', '0');
83 shouldBe('d[2]', '0');
84 shouldBe('d[3]', '255');
86 d
= ctx
.getImageData(349, 149, 1, 1).data
;
87 shouldBe('d[0]', '255');
88 shouldBe('d[1]', '0');
89 shouldBe('d[2]', '0');
90 shouldBe('d[3]', '255');
92 // Verify solid alpha shadow.
93 d
= ctx
.getImageData(250, 200, 1, 1).data
;
94 shouldBe('d[0]', '255');
95 shouldBe('d[1]', '0');
96 shouldBe('d[2]', '0');
97 shouldBeAround('d[3]', '76');
99 d
= ctx
.getImageData(250, 299, 1, 1).data
;
100 shouldBe('d[0]', '255');
101 shouldBe('d[1]', '0');
102 shouldBe('d[2]', '0');
103 shouldBeAround('d[3]', '76');
105 d
= ctx
.getImageData(349, 200, 1, 1).data
;
106 shouldBe('d[0]', '255');
107 shouldBe('d[1]', '0');
108 shouldBe('d[2]', '0');
109 shouldBeAround('d[3]', '76');
111 d
= ctx
.getImageData(349, 299, 1, 1).data
;
112 shouldBe('d[0]', '255');
113 shouldBe('d[1]', '0');
114 shouldBe('d[2]', '0');
115 shouldBeAround('d[3]', '76');
117 // Verify blurry shadow.
118 d
= ctx
.getImageData(248, 348, 1, 1).data
;
119 shouldBe('d[0]', '255');
120 shouldBe('d[1]', '0');
121 shouldBe('d[2]', '0');
122 shouldBeSmaller('d[3]', '25');
124 d
= ctx
.getImageData(248, 451, 1, 1).data
;
125 shouldBe('d[0]', '255');
126 shouldBe('d[1]', '0');
127 shouldBe('d[2]', '0');
128 shouldBeSmaller('d[3]', '25');
130 d
= ctx
.getImageData(351, 348, 1, 1).data
;
131 shouldBe('d[0]', '255');
132 shouldBe('d[1]', '0');
133 shouldBe('d[2]', '0');
134 shouldBeSmaller('d[3]', '25');
136 d
= ctx
.getImageData(351, 451, 1, 1).data
;
137 shouldBe('d[0]', '255');
138 shouldBe('d[1]', '0');
139 shouldBe('d[2]', '0');
140 shouldBeSmaller('d[3]', '25');
142 // Verify blurry alpha shadow.
143 d
= ctx
.getImageData(248, 498, 1, 1).data
;
144 shouldBe('d[0]', '255');
145 shouldBe('d[1]', '0');
146 shouldBe('d[2]', '0');
147 shouldBeSmaller('d[3]', '10');
149 d
= ctx
.getImageData(248, 601, 1, 1).data
;
150 shouldBe('d[0]', '255');
151 shouldBe('d[1]', '0');
152 shouldBe('d[2]', '0');
153 shouldBeSmaller('d[3]', '10');
155 d
= ctx
.getImageData(351, 498, 1, 1).data
;
156 shouldBe('d[0]', '255');
157 shouldBe('d[1]', '0');
158 shouldBe('d[2]', '0');
159 shouldBeSmaller('d[3]', '10');
161 d
= ctx
.getImageData(351, 601, 1, 1).data
;
162 shouldBe('d[0]', '255');
163 shouldBe('d[1]', '0');
164 shouldBe('d[2]', '0');
165 shouldBeSmaller('d[3]', '10');