HTML5 Game - Canvas Sub-paths

Using disconnected sub-paths

Sub-paths don't have to touch to form part of a path.

We can have several disconnected sub-paths and they are still treated as being part of the same shape.

Demo

ResultView the demo in separate window

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <style>
            canvas {border: thin solid black}
            body > * {float:left;}
        </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");
       //from w w w .  j  a  v  a2  s .  com
            ctx.fillStyle = "yellow";
            ctx.strokeStyle = "black";
            ctx.lineWidth = 4;
       
            ctx.beginPath();
            ctx.moveTo(110, 10);
            
            ctx.lineTo(110, 100);
            
            ctx.moveTo(210, 10);
            ctx.lineTo(20, 20);
            ctx.closePath();
            
            ctx.rect(110, 10, 100, 90);
            ctx.rect(110, 100, 130, 30);
            
            ctx.fill();
            ctx.stroke();           
        </script>
    </body>
</html>

Related Topic