Bézier curves are interesting. They are mostly used for drawing "S" shapes or asymmetric curves.
Bézier curves are defined by a context point, like quadratic curves, two control points that define two tangents, and an ending point.
The first part of the curve is tangential to the imaginary line defined by the context point and the first control point. The second part of the curve is tangential to the imaginary line defined by the second control point and the ending point.
The best way to understand how they work is to check out one of these interactive applications:
Nice video tutorial: Bézier curves under the hood
Source code:
ctx.moveTo(contextX, contextY);
context.bezierCurveTo(controlX1, controlY1, controlX2, controlY2, endX, endY);
// Optional : set lineWidth and stroke color
context.lineWidth = 5;
context.strokeStyle = "#0000ff";
// Draw!
ctx.stroke();
Try this:
Code source:
context.beginPath();
context.moveTo(100, 20);
context.bezierCurveTo(290, -40, 200, 200, 400, 100);
context.lineWidth = 5;
context.strokeStyle = "#0000ff";
context.stroke();
Try this:
Extract from source code:
context.beginPath();
context.moveTo(100, 20);
context.lineTo(200, 160);
context.quadraticCurveTo(230, 200, 250, 120);
context.bezierCurveTo(290, -40, 300, 200, 400, 150);
context.lineTo(500, 90);
// TRY COMMENTING THIS LINE OUT
context.closePath();
context.lineWidth = 5;
context.strokeStyle = "#0000ff";
context.stroke();
In this example we use the closePath() method to draw a line between the last path point and the first path point (line 11), so that the drawing looks like a pair of goggles.
Note how the different parts are linked together and make a "path":
This Bézier tool ("HTML5 <canvas> bezierCurveTo command generator") is available online: try it!