HTML Canvas Bezier curve draw Spade

Description

HTML Canvas Bezier curve draw Spade

View in separate window

<html>
    <head>
        <script>
            function drawSpade(context, x, y, width, height){
                context.save();//from  ww w .  j  a v a2 s . c  om
                var bottomWidth = width * 0.7;
                var topHeight = height * 0.7;
                var 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 = "black";
                context.fill();
                context.restore();
            }
            window.onload = function(){
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
        
                drawSpade(context, 100, 70, 75, 100);
            };
        </script>
    </head>
    <body>
        <canvas id="myCanvas" width="600" height="250" style="border:1px solid black;">
        </canvas>
    </body>
</html>



PreviousNext

Related