blob: 26d094684dd2f95dda94cb605149c15613917171 [file] [log] [blame]
commit-queue@webkit.org6f930412011-01-18 18:10:37 +00001<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2<html>
3<head>
mark.lam@apple.com104d9932013-09-07 22:20:15 +00004<script src="../../resources/js-test-pre.js"></script>
commit-queue@webkit.org6f930412011-01-18 18:10:37 +00005</head>
6<body>
ap@apple.com92352d02017-05-24 22:53:00 +00007<script>
8description("Ensure correct behavior of canvas with fillPath using a fillStyle color with alpha and a shadow");
9
10function print(message, color)
11{
12 var paragraph = document.createElement("div");
13 paragraph.appendChild(document.createTextNode(message));
14 paragraph.style.fontFamily = "monospace";
15 if (color)
16 paragraph.style.color = color;
17 document.getElementById("console").appendChild(paragraph);
18}
19
20function shouldBeAround(a, b)
21{
22 var evalA;
23 try {
24 evalA = eval(a);
25 } catch(e) {
26 evalA = e;
27 }
28
29 if (Math.abs(evalA - b) < 15)
30 print("PASS " + a + " is around " + b , "green")
31 else
32 print("FAIL " + a + " is not around " + b + " (actual: " + evalA + ")", "red");
33}
34
35var canvas = document.createElement('canvas');
36document.body.appendChild(canvas);
37canvas.setAttribute('width', '600');
38canvas.setAttribute('height', '1100');
39var ctx = canvas.getContext('2d');
40
41ctx.save();
42ctx.fillStyle = 'rgba(0, 0, 255, 0.5)';
43ctx.shadowColor = 'rgba(255, 0, 0, 0.5)';
44ctx.shadowOffsetX = 250;
45
46function fillShape(x, y) {
47 ctx.beginPath();
48 ctx.arc(x, y, 100, 0, Math.PI*2, true);
49 ctx.arc(x, y, 50, 0, Math.PI*2, false);
50 ctx.fill();
51}
52
53// Alpha shadow.
54ctx.shadowBlur = 0;
55fillShape(150, 150);
56
57// Blurry shadow.
58ctx.shadowBlur = 10;
59fillShape(150, 400);
60
61ctx.rotate(Math.PI/2);
62
63// Rotated alpha shadow.
64ctx.shadowBlur = 0;
65fillShape(650, -150);
66
67// Rotated blurry shadow.
68ctx.shadowBlur = 10;
69fillShape(900, -150);
70
71ctx.restore();
72
73var imageData, data;
74ctx.fillStyle = 'black';
75
76function test(alphaTestFunction, x, y, r, g, b, a) {
77 // Get pixel.
78 imageData = ctx.getImageData(x, y, 1, 1);
79 data = imageData.data;
80 // Test pixel color components.
81 shouldBe('data[0]', r+'');
82 shouldBe('data[1]', g+'');
83 shouldBe('data[2]', b+'');
84 alphaTestFunction('data[3]', a+'');
85 // Plot test point.
86 ctx.fillRect(x, y, 3, 3);
87}
88
89print('Verifying alpha shadow...');
90test(shouldBe, 400, 150, 0, 0, 0, 0);
91test(shouldBeAround, 400, 75, 255, 0, 0, 64);
92test(shouldBeAround, 400, 225, 255, 0, 0, 64);
93test(shouldBeAround, 325, 150, 255, 0, 0, 64);
94test(shouldBeAround, 475, 150, 255, 0, 0, 64);
95
96print(' ');
97print('Verifying blurry shadow...');
98test(shouldBe, 400, 400, 0, 0, 0, 0);
99test(shouldBeAround, 400, 300, 255, 0, 0, 31);
100test(shouldBeAround, 400, 500, 255, 0, 0, 31);
101test(shouldBeAround, 300, 400, 255, 0, 0, 31);
102test(shouldBeAround, 500, 400, 255, 0, 0, 31);
103
104print(' ');
105print('Verifying rotated alpha shadow...');
106test(shouldBe, 400, 650, 0, 0, 0, 0);
107test(shouldBeAround, 400, 575, 255, 0, 0, 64);
108test(shouldBeAround, 400, 725, 255, 0, 0, 64);
109test(shouldBeAround, 325, 650, 255, 0, 0, 64);
110test(shouldBeAround, 475, 650, 255, 0, 0, 64);
111
112print(' ');
113print('Verifying rotated blurry shadow...');
114test(shouldBe, 400, 900, 0, 0, 0, 0);
115test(shouldBeAround, 400, 800, 255, 0, 0, 31);
116test(shouldBeAround, 400, 1000, 255, 0, 0, 31);
117test(shouldBeAround, 300, 900, 255, 0, 0, 31);
118test(shouldBeAround, 500, 900, 255, 0, 0, 31);
119
120print(' ');
121</script>
mark.lam@apple.com104d9932013-09-07 22:20:15 +0000122<script src="../../resources/js-test-post.js"></script>
commit-queue@webkit.org6f930412011-01-18 18:10:37 +0000123</body>
124</html>