summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/canvas/script-tests/canvas-fillPath-gradient-shadow.js
blob: 82d4751f4193785146bb2400ee2e9a2c029006bb (plain)
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
description("Ensure correct behavior of canvas with fillPath using a gradient fillStyle and a shadow");

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 shouldBeAround(a, b)
{
    var evalA;
    try {
        evalA = eval(a);
    } catch(e) {
        evalA = e;
    }

    if (Math.abs(evalA - b) < 15)
        print("PASS " + a + " is around " + b , "green")
    else
        print("FAIL " + a + " is not around " + b + " (actual: " + evalA + ")", "red");
}

var canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.setAttribute('width', '600');
canvas.setAttribute('height', '1100');
var ctx = canvas.getContext('2d');

var gradient = ctx.createLinearGradient(0, 0, 300, 0);
gradient.addColorStop(0, 'rgba(0, 0, 255, 0.5)');
gradient.addColorStop(1, 'rgba(0, 0, 255, 0.5)');

ctx.save();
ctx.fillStyle = gradient;
ctx.shadowColor = 'rgba(255, 0, 0, 0.5)';
ctx.shadowOffsetX = 250;

function fillShape(x, y) {
    ctx.beginPath();
    ctx.arc(x, y, 100, 0, Math.PI*2, true);
    ctx.arc(x, y, 50,  0, Math.PI*2, false);
    ctx.fill();
}

// Alpha shadow.
ctx.shadowBlur = 0;
fillShape(150, 150);

// Blurry shadow.
ctx.shadowBlur = 10;
fillShape(150, 400);

ctx.rotate(Math.PI/2);

// Rotated alpha shadow.
ctx.shadowBlur = 0;
fillShape(650, -150);

// Rotated blurry shadow.
ctx.shadowBlur = 10;
fillShape(900, -150);

ctx.restore();

var imageData, data;
ctx.fillStyle = 'black';

function test(alphaTestFunction, x, y, r, g, b, a) {
    // Get pixel.
    imageData = ctx.getImageData(x, y, 1, 1);
    data = imageData.data;
    // Test pixel color components.
    shouldBe('data[0]', r+'');
    shouldBe('data[1]', g+'');
    shouldBe('data[2]', b+'');
    alphaTestFunction('data[3]', a+'');
    // Plot test point.
    ctx.fillRect(x, y, 3, 3);
}

print('Verifying alpha shadow...');
test(shouldBe, 400, 150, 0, 0, 0, 0);
test(shouldBeAround, 400, 75, 255,  0, 0, 64);
test(shouldBeAround, 400, 225, 255, 0, 0, 64);
test(shouldBeAround, 325, 150, 255, 0, 0, 64);
test(shouldBeAround, 475, 150, 255, 0, 0, 64);

print(' ');
print('Verifying blurry shadow...');
test(shouldBe, 400, 400, 0, 0, 0, 0);
test(shouldBeAround, 400, 300, 255, 0, 0, 31);
test(shouldBeAround, 400, 500, 255, 0, 0, 31);
test(shouldBeAround, 300, 400, 255, 0, 0, 31);
test(shouldBeAround, 500, 400, 255, 0, 0, 31);

print(' ');
print('Verifying rotated alpha shadow...');
test(shouldBe, 400, 650, 0, 0, 0, 0);
test(shouldBeAround, 400, 575, 255, 0, 0, 64);
test(shouldBeAround, 400, 725, 255, 0, 0, 64);
test(shouldBeAround, 325, 650, 255, 0, 0, 64);
test(shouldBeAround, 475, 650, 255, 0, 0, 64);

print(' ');
print('Verifying rotated blurry shadow...');
test(shouldBe, 400, 900, 0, 0, 0, 0);
test(shouldBeAround, 400, 800, 255, 0, 0, 31);
test(shouldBeAround, 400, 1000, 255, 0, 0, 31);
test(shouldBeAround, 300, 900, 255, 0, 0, 31);
test(shouldBeAround, 500, 900, 255, 0, 0, 31);

print(' ');