1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
description("Ensure correct behavior of canvas with image shadow. A square with a cut-out top-right corner should be displayed with solid shadow (top) and blur shadow (bottom).");
function print(message, color)
{
var paragraph = document.createElement("div");
paragraph.appendChild(document.createTextNode(message));
paragraph.style.fontFamily = "monospace";
if (color)
paragraph.style.color = color;
document.getElementById("console").appendChild(paragraph);
}
function shouldNotBe(a, b)
{
var evalA;
try {
evalA = eval(a);
} catch(e) {
evalA = e;
}
if (evalA != b)
print("PASS " + a + " should not be " + b + " and it's not.", "green")
else
print("FAIL " + a + " should not be " + b + " but it is.", "red");
}
// Create auxiliary canvas to draw to and create an image from.
// This is done instead of simply loading an image from the file system
// because that would throw a SECURITY_ERR DOM Exception.
var aCanvas = document.createElement('canvas');
aCanvas.setAttribute('width', '200');
aCanvas.setAttribute('height', '200');
var aCtx = aCanvas.getContext('2d');
// Draw a circle on the same canvas.
aCtx.beginPath();
aCtx.fillStyle = 'green';
aCtx.arc(100, 100, 150, 0, -Math.PI/2, false);
aCtx.fill();
// Create the image object to be drawn on the master canvas.
var img = new Image();
img.onload = drawImageToCanvasAndCheckPixels;
img.src = aCanvas.toDataURL(); // set a data URI of the base64 enconded image as the source
// Create master canvas.
var canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.setAttribute('width', '600');
canvas.setAttribute('height', '600');
var ctx = canvas.getContext('2d');
function drawImageToCanvasAndCheckPixels() {
ctx.shadowOffsetX = 250;
ctx.shadowColor = 'rgba(240, 50, 50, 1.0)';
ctx.drawImage(img, 50, 50);
ctx.shadowOffsetX = 250;
ctx.shadowBlur = 6;
ctx.shadowColor = 'rgba(50, 50, 200, 0.9)';
ctx.shadowColor = 'rgba(0, 0, 255, 1.0)';
ctx.drawImage(img, 50, 300);
checkPixels();
}
function checkPixels() {
var imageData, data;
// Verify solid shadow.
imageData = ctx.getImageData(260, 300, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '0');
shouldBe('d[3]', '0');
imageData = ctx.getImageData(350, 100, 1, 1);
d = imageData.data;
shouldBe('d[0]', '240');
shouldBe('d[1]', '50');
shouldBe('d[2]', '50');
shouldBe('d[3]', '255');
imageData = ctx.getImageData(400, 200, 1, 1);
d = imageData.data;
shouldBe('d[0]', '240');
shouldBe('d[1]', '50');
shouldBe('d[2]', '50');
shouldBe('d[3]', '255');
imageData = ctx.getImageData(490, 65, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '0');
shouldBe('d[3]', '0');
imageData = ctx.getImageData(485, 65, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '0');
shouldBe('d[3]', '0');
// Verify blurry shadow.
imageData = ctx.getImageData(260, 400, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '0');
shouldBe('d[3]', '0');
imageData = ctx.getImageData(350, 300, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '255');
shouldNotBe('d[3]', '255');
imageData = ctx.getImageData(300, 400, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '255');
shouldNotBe('d[3]', '255');
imageData = ctx.getImageData(300, 500, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '255');
shouldNotBe('d[3]', '255');
imageData = ctx.getImageData(400, 500, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '255');
shouldNotBe('d[3]', '255');
imageData = ctx.getImageData(400, 400, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '255');
imageData = ctx.getImageData(490, 315, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '0');
shouldBe('d[3]', '0');
imageData = ctx.getImageData(485, 320, 1, 1);
d = imageData.data;
shouldBe('d[0]', '0');
shouldBe('d[1]', '0');
shouldBe('d[2]', '0');
shouldBe('d[3]', '0');
finishJSTest();
}
window.jsTestIsAsync = true;
|