HTML Canvas Bezier curve

Introduction

HTML5 canvas Bezier curves are defined by the

  • context point,
  • two control points, and an
  • ending point.

The additional control point gives us much more control over its curvature compared to Quadratic curves:

context.bezierCurveTo(controlPointX1, 
                      controlPointY1,  
                      controlPointX2, 
                      controlPointY2,  
                      endingPointX, 
                      endingPointY); 
                      
                      

View in separate window

<html>
    <head>
        <script>
            window.onload = function(){
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
        /*www . j  ava 2  s .c  o m*/
                context.lineWidth = 10;
                context.strokeStyle = "black"; // line color
                context.moveTo(150, 130);
                context.bezierCurveTo(150, 10, 420, 10, 420, 180);
                context.stroke();
            };
        </script>
    </head>
    <body>
        <canvas id="myCanvas" width="600" height="250" style="border:1px solid black;">
        </canvas>
    </body>
</html>



PreviousNext

Related