HTML5 Game - Canvas Shape Spade

Creating custom draw spade shape

The following code uses bezierCurveTo() 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 ava  2s . co  m*/
                let bottomWidth = width * 0.7;
                let topHeight = height * 0.7;
                let bottomHeight = height * 0.3;
                
                context.beginPath();
                context.moveTo(x, y);
                
                // top left of spade          
                context.bezierCurveTo(x, y + topHeight / 2, // control point 1
                                      x - width / 2, y + topHeight / 2, // control point 2
                                      x - width / 2, y + topHeight // end point
                );
                
                // bottom left of spade
                context.bezierCurveTo(x - width / 2, y + topHeight * 1.3, // control point 1
                                      x, y + topHeight * 1.3, // control point 2
                                      x, y + topHeight // end point
                );
                
                // bottom right of spade
                context.bezierCurveTo(x, y + topHeight * 1.3, // control point 1
                                      x + width / 2, y + topHeight * 1.3, // control point 2
                                      x + width / 2, y + topHeight // end point
                );
                
                // top right of spade
                context.bezierCurveTo(x + width / 2, y + topHeight / 2, // control point 1
                                      x, y + topHeight / 2, // control point 2
                                      x, y // end point
                );
                
                context.closePath();
                context.fill();
                
                // bottom of spade
                context.beginPath();
                context.moveTo(x, y + topHeight);
                context.quadraticCurveTo(x, y + topHeight + bottomHeight, // control point
                                         x - bottomWidth / 2, y + topHeight + bottomHeight // end point
                );
                context.lineTo(x + bottomWidth / 2, y + topHeight + bottomHeight);
                context.quadraticCurveTo(x, y + topHeight + bottomHeight, // control point
                                         x, y + topHeight // end point
                );
                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>