Drawing Bezier Curves

The canvas supports drawing two kinds of Bezier curves: cubic and quadratic.

We pick a start and end point and then add one or more control points that shape the curve.

The curve methods:

NameDescriptionReturns
bezierCurveTo(cx1, cy1, cx2, cy2, x, y)Draws a Bezier curve to the point (x, y) with the control points (cx1, cy1) and (cx2, cy2).void
quadraticCurveTo(cx, xy, x, y)Draws a quadratic Bezier curve to (x, y) with the control point (cx, cy).void

Drawing Cubic Bezier Curves

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
canvas {
      border: thin solid black
}

body>* {
      float: left;
}
</style>
</head>
<body>
      <canvas id="canvas" width="500" height="140"> 
      Your browser doesn't support the <code>canvas</code> element 
      </canvas>
      <script>
            var canvasElem = document.getElementById("canvas");
            var ctx = canvasElem.getContext("2d");
            var startPoint = [ 20, 100 ];
            var endPoint = [ 200, 100 ];
            var cp1 = [ 20, 50 ];
            var cp2 = [ 30, 50 ];
            function draw() {
                  ctx.lineWidth = 3;
                  ctx.strokeStyle = "black";
                  ctx.beginPath();
                  ctx.moveTo(startPoint[0], startPoint[1]);
                  ctx.bezierCurveTo(cp1[0], cp1[1], cp2[0], cp2[1], endPoint[0],endPoint[1]);
                  ctx.stroke();
                  ctx.lineWidth = 1;
                  ctx.strokeStyle = "red";
                  var points = [ startPoint, endPoint, cp1, cp2 ];
                  for ( var i = 0; i < points.length; i++) {
                        drawPoint(points[i]);
                  }
                  drawLine(startPoint, cp1);
                  drawLine(endPoint, cp2);
            }
            function drawPoint(point) {
                  ctx.beginPath();
                  ctx.strokeRect(point[0] - 2, point[1] - 2, 4, 4);
            }
            function drawLine(from, to) {
                  ctx.beginPath();
                  ctx.moveTo(from[0], from[1]);
                  ctx.lineTo(to[0], to[1]);
                  ctx.stroke();
            }
            draw();
      </script>
</body>
</html>
  
Click to view this demo.

Drawing Quadratic Bezier Curves

A quadratic Bezier curve has only one control point.

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
canvas {
      border: thin solid black
}

body>* {
      float: left;
}
</style>
</head>
<body>
      <canvas id="canvas" width="500" height="140"> 
      Your browser doesn't support the <code>canvas</code> element 
      </canvas>
      <script>
            var canvasElem = document.getElementById("canvas");
            var ctx = canvasElem.getContext("2d");
            var startPoint = [ 150, 100 ];
            var endPoint = [ 400, 100 ];
            var cp1 = [ 250, 50 ];
            draw();
            function draw() {
                  ctx.lineWidth = 3;
                  ctx.strokeStyle = "red";
                  ctx.beginPath();
                  ctx.moveTo(startPoint[0], startPoint[1]);
                  ctx.quadraticCurveTo(cp1[0], cp1[1], endPoint[0], endPoint[1]);
                  ctx.stroke();
                  ctx.lineWidth = 1;
                  ctx.strokeStyle = "red";
                  var points = [ startPoint, endPoint, cp1 ];
                  for ( var i = 0; i < points.length; i++) {
                        drawPoint(points[i]);
                  }
                  drawLine(startPoint, cp1);
                  drawLine(endPoint, cp1);
            }
            function drawPoint(point) {
                  ctx.beginPath();
                  ctx.strokeRect(point[0] - 2, point[1] - 2, 4, 4);
            }
            function drawLine(from, to) {
                  ctx.beginPath();
                  ctx.moveTo(from[0], from[1]);
                  ctx.lineTo(to[0], to[1]);
                  ctx.stroke();
            }
      </script>
</body>
</html>
  
Click to view this demo.

Creating a Clipping Region

The clip method:

NameDescriptionReturns
clip()Creates a new clipping regionvoid
 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
canvas {
      border: thin solid black
}

</style>
</head>
<body>
      <canvas id="canvas" width="500" height="140"> 
      Your browser doesn't support the <code>canvas</code> element 
      </canvas>
      <script>
            var ctx = document.getElementById("canvas").getContext("2d");
            ctx.fillStyle = "yellow";
            ctx.beginPath();
            ctx.rect(20, 20, 500, 140);
            ctx.fill();
            ctx.beginPath();
            ctx.rect(100, 20, 300, 100);
            ctx.clip();
            ctx.fillStyle = "red";
            ctx.beginPath();
            ctx.rect(0, 0, 500, 140);
            ctx.fill();
      </script>
</body>
</html>
  
Click to view this demo.
Home 
  HTML CSS Book 
    HTML  

canvas:
  1. Getting Started with the Canvas Element
  2. Getting a Canvas Context
  3. Drawing Rectangles
  4. Canvas Drawing State
  5. Setting the Line Join Style
  6. Using Gradients
  7. Using Patterns
  8. Using smaller shapes with an image pattern
  9. Drawing Images
  10. Using Video Images
  11. Using Canvas Images
  12. Setting the Fill & Stroke Styles
  13. Saving and Restoring Drawing State
  14. Drawing Using Paths
  15. Drawing Arcs
  16. Drawing Bezier Curves
  17. Drawing Text
  18. Using Shadows
  19. Using Transparency
Related: