HTML5 Game - Using smaller shapes with a radial gradient

Description

Using smaller shapes with a radial gradient

Demo

ResultView the demo in separate window

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <style>
            canvas {border: thin solid black; margin: 4px}
        </style>
    </head>
    <body>
        <canvas id="canvas" width="500" height="140">
            Your browser doesn't support the <code>canvas</code> element
        </canvas>
        <script>
            let ctx = document.getElementById("canvas").getContext("2d");

            let grad = ctx.createRadialGradient(250, 70, 20, 200, 60, 100);
            grad.addColorStop(0, "red");
            grad.addColorStop(0.5, "white");
            grad.addColorStop(1, "black");
            /*from   ww w  .  java  2 s  . c  o m*/
            ctx.fillStyle = grad;
            ctx.fillRect(150, 20, 75, 50);
            
            ctx.lineWidth = 8;
            ctx.strokeStyle = grad;
            ctx.strokeRect(250, 20, 75, 50);
        </script>
    </body>
</html>

We can use the gradient for both the fillStyle and strokeStyle properties.

Related Topic