1 description("Ensure correct behavior of canvas with fillRect+shadow after translation+rotation+scaling. A blue and red checkered pattern should be displayed.");
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) < 20)
23 print("PASS " + a + " is around " + b , "green")
25 print("FAIL " + a + " is not around " + b + " (actual: " + evalA + ")", "red");
28 var canvas = document.createElement('canvas');
29 document.body.appendChild(canvas);
30 canvas.setAttribute('width', '600');
31 canvas.setAttribute('height', '600');
32 var ctx = canvas.getContext('2d');
34 ctx.fillStyle = 'rgba(0, 0, 255, 1.0)';
35 ctx.shadowOffsetX = 100;
36 ctx.shadowOffsetY = 100;
38 ctx.translate(-100, -100);
39 ctx.rotate(Math.PI/2);
42 ctx.shadowColor = 'rgba(255, 0, 0, 1.0)';
43 ctx.fillRect(100, -150, 50, 50);
45 ctx.shadowColor = 'rgba(255, 0, 0, 0.5)';
46 ctx.fillRect(200, -150, 50, 50);
49 ctx.shadowColor = 'rgba(255, 0, 0, 1.0)';
50 ctx.fillRect(100, -250, 50, 50);
52 ctx.shadowColor = 'rgba(255, 0, 0, 0.5)';
53 ctx.fillRect(200, -250, 50, 50);
55 var d; // imageData.data
57 // Verify solid shadow.
58 d = ctx.getImageData(201, 205, 1, 1).data;
59 shouldBe('d[0]', '255');
60 shouldBe('d[1]', '0');
61 shouldBe('d[2]', '0');
62 shouldBe('d[3]', '255');
64 d = ctx.getImageData(298, 298, 1, 1).data;
65 shouldBe('d[0]', '255');
66 shouldBe('d[1]', '0');
67 shouldBe('d[2]', '0');
68 shouldBe('d[3]', '255');
70 d = ctx.getImageData(201, 298, 1, 1).data;
71 shouldBe('d[0]', '255');
72 shouldBe('d[1]', '0');
73 shouldBe('d[2]', '0');
74 shouldBe('d[3]', '255');
76 // Verify solid alpha shadow.
77 d = ctx.getImageData(201, 401, 1, 1).data;
78 shouldBe('d[0]', '255');
79 shouldBe('d[1]', '0');
80 shouldBe('d[2]', '0');
81 shouldBeAround('d[3]', '127');
83 d = ctx.getImageData(298, 450, 1, 1).data;
84 shouldBe('d[0]', '255');
85 shouldBe('d[1]', '0');
86 shouldBe('d[2]', '0');
87 shouldBeAround('d[3]', '127');
89 d = ctx.getImageData(205, 498, 1, 1).data;
90 shouldBe('d[0]', '255');
91 shouldBe('d[1]', '0');
92 shouldBe('d[2]', '0');
93 shouldBeAround('d[3]', '127');
95 // Verify blurry shadow.
96 d = ctx.getImageData(399, 205, 1, 1).data;
97 shouldBe('d[0]', '255');
98 shouldBe('d[1]', '0');
99 shouldBe('d[2]', '0');
100 shouldBeAround('d[3]', '106');
102 d = ctx.getImageData(500, 205, 1, 1).data;
103 shouldBe('d[0]', '255');
104 shouldBe('d[1]', '0');
105 shouldBe('d[2]', '0');
106 shouldBeAround('d[3]', '106');
108 d = ctx.getImageData(499, 299, 1, 1).data;
109 shouldBe('d[0]', '255');
110 shouldBe('d[1]', '0');
111 shouldBe('d[2]', '0');
112 shouldBeAround('d[3]', '83');
114 // Verify blurry alpha shadow.
115 d = ctx.getImageData(398, 405, 1, 1).data;
116 shouldBe('d[0]', '255');
117 shouldBe('d[1]', '0');
118 shouldBe('d[2]', '0');
119 shouldBeAround('d[3]', '36');
121 d = ctx.getImageData(405, 501, 1, 1).data;
122 shouldBe('d[0]', '255');
123 shouldBe('d[1]', '0');
124 shouldBe('d[2]', '0');
125 shouldBeAround('d[3]', '36');
127 d = ctx.getImageData(405, 501, 1, 1).data;
128 shouldBe('d[0]', '255');
129 shouldBe('d[1]', '0');
130 shouldBe('d[2]', '0');
131 shouldBeAround('d[3]', '36');