summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/2dcontext/shadows/canvas_shadows_001.htm
blob: 1763950d6127f2dffe9fb3ed76285ebef88f395a (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
<!doctype HTML>
<html>
    <head>
        <title>HTML5 Canvas Test: Shadows for linear gradients</title>
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
        <link rel="author" title="Microsoft" href="http://www.microsoft.com" />
        <link rel="help" href="http://www.w3.org/TR/2dcontext/#shadows" />
        <meta name="assert" content="Shadows must be drawn for linear gradients." />
        <script type="text/javascript">
            async_test(function(t) {
              window.addEventListener("load", t.step_func_done(function runTest() {
                var canvas = document.getElementById("canvas1");
                var ctx = canvas.getContext("2d");

                // Draw a red rectangle.
                ctx.fillStyle = "rgba(255, 0, 0, 1.0)";
                ctx.fillRect(150, 0, 100, 50);

                // Set shadow styles to draw a black shadow to overlap the red rectangle.
                ctx.shadowOffsetX = 150;
                ctx.shadowColor = "rgba(0, 0, 0, 1.0)";

                // Draw a left to right, green-to-blue linear gradient.
                var lingrad = ctx.createLinearGradient(0, 50, 100, 50);
                lingrad.addColorStop(0, "rgba(0, 255, 0, 1.0)");
                lingrad.addColorStop(1, "rgba(0, 0, 255, 1.0)");
                ctx.fillStyle = lingrad;
                ctx.fillRect(0, 0, 100, 50);

                // Check the red is gone
                var data = ctx.getImageData(150, 0, 100, 50);
                for (var i = 0; i < data.data.length; i += 4) {
                  var r = data.data[i];
                  var g = data.data[i+1];
                  var b = data.data[i+2];
                  var a = data.data[i+3];
                  assert_equals(r, 0, "r channel");
                  assert_equals(g, 0, "g channel");
                  assert_equals(b, 0, "b channel");
                  assert_equals(a, 0xFF, "a channel");
                }

                for (var j = 0; j < data.data.length; j += 4) {
                  var r2 = data.data[j];
                  var g2 = data.data[j+1];
                  var b2 = data.data[j+2];
                  var a2 = data.data[j+3];
                  assert_false(r2 == 0xFF && g2 == 0 && b2 == 0 && a2 == 0xFF, "no red");
                }
              }));
            }, "linear gradient fillRect draws shadow (black rectange)");
        </script>
    </head>
    <body>
        <p>Description: Shadows must be drawn for linear gradients.</p>
        <p>Test passes if there is one gradient filled rectangle and one black rectangle, and no red seen on the page.</p>
        <canvas id="canvas1" width="300" height="150">Browser does not support HTML5 Canvas.</canvas>
    </body>
</html>