1 description("Ensure correct behavior of canvas with strokePath using a gradient strokeStyle and a 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) < 15)
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', '1100');
32 var ctx = canvas.getContext('2d');
34 var gradient = ctx.createLinearGradient(0, 0, 300, 0);
35 gradient.addColorStop(0, 'rgba(0, 0, 255, 0.5)');
36 gradient.addColorStop(1, 'rgba(0, 0, 255, 0.5)');
39 ctx.strokeStyle = gradient;
40 ctx.shadowColor = 'rgba(255, 0, 0, 0.5)';
41 ctx.shadowOffsetX = 250;
49 ctx.rect(50, 50, side, side);
55 ctx.rect(50, 300, side, side);
58 ctx.rotate(Math.PI / 2);
60 // Rotated alpha shadow.
63 ctx.rect(550, -250, side, side);
66 // Rotated blurry shadow.
69 ctx.rect(800, -250, side, side);
75 ctx.fillStyle = 'black';
77 function test(alphaTestFunction, x, y, r, g, b, a) {
79 imageData = ctx.getImageData(x, y, 1, 1);
80 data = imageData.data;
81 // Test pixel color components.
82 shouldBe('data[0]', r+'');
83 shouldBe('data[1]', g+'');
84 shouldBe('data[2]', b+'');
85 alphaTestFunction('data[3]', a+'');
87 ctx.fillRect(x, y, 3, 3);
90 print('Verifying alpha shadow...');
91 test(shouldBe, 400, 150, 0, 0, 0, 0);
93 test(shouldBe, 400, 75, 0, 0, 0, 0);
94 test(shouldBe, 400, 225, 0, 0, 0, 0);
95 test(shouldBe, 325, 150, 0, 0, 0, 0);
96 test(shouldBe, 475, 150, 0, 0, 0, 0);
98 test(shouldBeAround, 400, 50, 255, 0, 0, 64);
99 test(shouldBeAround, 500, 150, 255, 0, 0, 64);
100 test(shouldBeAround, 400, 250, 255, 0, 0, 64);
101 test(shouldBeAround, 300, 150, 255, 0, 0, 64);
103 test(shouldBe, 400, 25, 0, 0, 0, 0);
104 test(shouldBe, 525, 150, 0, 0, 0, 0);
105 test(shouldBe, 400, 275, 0, 0, 0, 0);
106 test(shouldBe, 275, 150, 0, 0, 0, 0);
109 print('Verifying blurry shadow...');
110 test(shouldBe, 400, 400, 0, 0, 0, 0);
112 test(shouldBe, 400, 325, 0, 0, 0, 0);
113 test(shouldBe, 475, 400, 0, 0, 0, 0);
114 test(shouldBe, 400, 475, 0, 0, 0, 0);
115 test(shouldBe, 325, 400, 0, 0, 0, 0);
117 test(shouldBeAround, 400, 300, 255, 0, 0, 64);
118 test(shouldBeAround, 400, 500, 255, 0, 0, 64);
119 test(shouldBeAround, 300, 400, 255, 0, 0, 64);
120 test(shouldBeAround, 500, 400, 255, 0, 0, 64);
122 test(shouldBe, 525, 400, 0, 0, 0, 0);
123 test(shouldBe, 275, 400, 0, 0, 0, 0);
126 print('Verifying rotated alpha shadow...');
127 test(shouldBe, 400, 650, 0, 0, 0, 0);
129 test(shouldBe, 400, 575, 0, 0, 0, 0);
130 test(shouldBe, 400, 725, 0, 0, 0, 0);
131 test(shouldBe, 325, 650, 0, 0, 0, 0);
132 test(shouldBe, 475, 650, 0, 0, 0, 0);
134 test(shouldBeAround, 400, 550, 255, 0, 0, 64);
135 test(shouldBeAround, 500, 650, 255, 0, 0, 64);
136 test(shouldBeAround, 400, 750, 255, 0, 0, 64);
137 test(shouldBeAround, 300, 650, 255, 0, 0, 64);
139 test(shouldBe, 400, 525, 0, 0, 0, 0);
140 test(shouldBe, 525, 650, 0, 0, 0, 0);
141 test(shouldBe, 400, 775, 0, 0, 0, 0);
142 test(shouldBe, 275, 650, 0, 0, 0, 0);
145 print('Verifying rotated blurry shadow...');
146 test(shouldBe, 400, 900, 0, 0, 0, 0);
148 test(shouldBe, 400, 825, 0, 0, 0, 0);
149 test(shouldBe, 475, 900, 0, 0, 0, 0);
150 test(shouldBe, 400, 975, 0, 0, 0, 0);
151 test(shouldBe, 325, 900, 0, 0, 0, 0);
153 test(shouldBeAround, 400, 800, 255, 0, 0, 64);
154 test(shouldBeAround, 400, 1000, 255, 0, 0, 64);
155 test(shouldBeAround, 300, 900, 255, 0, 0, 64);
156 test(shouldBeAround, 500, 900, 255, 0, 0, 64);
158 test(shouldBe, 525, 900, 0, 0, 0, 0);
159 test(shouldBe, 275, 900, 0, 0, 0, 0);
160 test(shouldBe, 400, 1025, 0, 0, 0, 0);