HTML5 Game - Canvas Shape Diamond

Creating custom draw diamond shape

The following code uses lineTo() method to draw custom shape.

Demo

ResultView the demo in separate window

<html>
    <head>
        <script>
            function draw(context, x, y, width, height){
                context.save();//from  w w w. j  a v  a2 s .c o m
                context.beginPath();
                context.moveTo(x, y);
                
                // top left edge
                context.lineTo(x - width / 2, y + height / 2);
                
                // bottom left edge
                context.lineTo(x, y + height);
                
                // bottom right edge
                context.lineTo(x + width / 2, y + height / 2);
                
                // closing the path automatically creates
                // the top right edge
                context.closePath();
                
                context.fillStyle = "red";
                context.fill();
                context.restore();
            }
            
            window.onload = function(){
                let canvas = document.getElementById("myCanvas");
                let context = canvas.getContext("2d");
        
                draw(context, canvas.width * 0.2, 70, 75, 100);
            };
        </script>
    </head>
    <body>
        <canvas id="myCanvas" width="600" height="250" style="border:1px solid black;">
        </canvas>
    </body>
</html>