HTML5 Game - Creating cubic Bezier curves

Description

Creating cubic Bezier curves

Demo

ResultView the demo in separate window

<!DOCTYPE html>

<html>
  <head>
    <title>Pushing canvas further</title>
    <meta charset="utf-8">
    /*  www.j  a  v  a 2s . c o m*/
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    
    <script type="text/javascript">
      $(document).ready(function() {  
        let canvas = $("#myCanvas");
        let context = canvas.get(0).getContext("2d");
   context.lineWidth = 5;  
   context.beginPath();  
   context.moveTo(50, 50);  
   context.bezierCurveTo(150, 50, 350, 450, 450, 250);  
   context.stroke();  
      
      });
    </script>
  </head>
  
  <body>
    <canvas id="myCanvas" width="500" height="500">
      <!-- Insert fallback content here -->
    </canvas>
  </body>
</html>

The bezierCurveTo function takes six arguments:

  • the (x , y) coordinate value of the first control point (cp1x and cp1y)
  • the (x , y) coordinate value of the second control point (cp2x and cp2y),
  • the (x , y) coordinate origin of the destination of the path.

Related Topic