1 description("Ensure correct behavior of canvas with fillPath using a pattern fillStyle 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 aCanvas = document.createElement('canvas');
29 aCanvas.setAttribute('width', '50');
30 aCanvas.setAttribute('height', '50');
32 var aCtx = aCanvas.getContext('2d');
33 aCtx.fillStyle = 'rgba(0, 0, 255, 0.5)';
34 aCtx.fillRect(0, 0, 50, 50);
36 var pattern = aCtx.createPattern(aCanvas, 'repeat');
38 var canvas = document.createElement('canvas');
39 document.body.appendChild(canvas);
40 canvas.setAttribute('width', '600');
41 canvas.setAttribute('height', '1100');
42 var ctx = canvas.getContext('2d');
45 ctx.fillStyle = pattern;
46 ctx.shadowColor = 'rgba(255, 0, 0, 0.5)';
47 ctx.shadowOffsetX = 250;
49 function fillShape(x, y) {
51 ctx.arc(x, y, 100, 0, Math.PI*2, true);
52 ctx.arc(x, y, 50, 0, Math.PI*2, false);
64 ctx.rotate(Math.PI/2);
66 // Rotated alpha shadow.
70 // Rotated blurry shadow.
77 ctx.fillStyle = 'black';
79 function test(alphaTestFunction, x, y, r, g, b, a) {
81 imageData = ctx.getImageData(x, y, 1, 1);
82 data = imageData.data;
83 // Test pixel color components.
84 shouldBe('data[0]', r+'');
85 shouldBe('data[1]', g+'');
86 shouldBe('data[2]', b+'');
87 alphaTestFunction('data[3]', a+'');
89 ctx.fillRect(x, y, 3, 3);
92 print('Verifying alpha shadow...');
93 test(shouldBe, 400, 150, 0, 0, 0, 0);
94 test(shouldBeAround, 400, 75, 255, 0, 0, 64);
95 test(shouldBeAround, 400, 225, 255, 0, 0, 64);
96 test(shouldBeAround, 325, 150, 255, 0, 0, 64);
97 test(shouldBeAround, 475, 150, 255, 0, 0, 64);
100 print('Verifying blurry shadow...');
101 test(shouldBe, 400, 400, 0, 0, 0, 0);
102 test(shouldBeAround, 400, 300, 255, 0, 0, 31);
103 test(shouldBeAround, 400, 500, 255, 0, 0, 31);
104 test(shouldBeAround, 300, 400, 255, 0, 0, 31);
105 test(shouldBeAround, 500, 400, 255, 0, 0, 31);
108 print('Verifying rotated alpha shadow...');
109 test(shouldBe, 400, 650, 0, 0, 0, 0);
110 test(shouldBeAround, 400, 575, 255, 0, 0, 64);
111 test(shouldBeAround, 400, 725, 255, 0, 0, 64);
112 test(shouldBeAround, 325, 650, 255, 0, 0, 64);
113 test(shouldBeAround, 475, 650, 255, 0, 0, 64);
116 print('Verifying rotated blurry shadow...');
117 test(shouldBe, 400, 900, 0, 0, 0, 0);
118 test(shouldBeAround, 400, 800, 255, 0, 0, 31);
119 test(shouldBeAround, 400, 1000, 255, 0, 0, 31);
120 test(shouldBeAround, 300, 900, 255, 0, 0, 31);
121 test(shouldBeAround, 500, 900, 255, 0, 0, 31);