Bezier curves

INTRODUCTION

Bezier curves are interesting – used mostly for drawing “S” shapes or asymmetric curves.

bezier curve control points

(Picture from : http://www.html5canvastutorials.com/tutorials/html5-canvas-bezier-curves/)

Bezier 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:

TYPICAL USAGE OF BEZIER CURVES

Source code:

  1. ctx.moveTo(contextX, contextY);
  2. context.bezierCurveTo(controlX1, controlY1, controlX2, controlY2, endX, endY);
  3. // Optional : set lineWidth and stroke color
  4. context.lineWidth = 5;
  5. context.strokeStyle = “#0000ff”;
  6. // Draw!
  7. ctx.stroke();

EXAMPLE 1

Try this interactive example: http://jsbin.com/hodawa/2/edit

bezier curve example

  1. context.beginPath();
  2. context.moveTo(100, 20);
  3. context.bezierCurveTo(290, 40, 200, 200, 400, 100);
  4. context.lineWidth = 5;
  5. context.strokeStyle = “#0000ff”;
  6. context.stroke();

EXAMPLE 2 WITH LINES, QUADRATIC, AND BEZIER CURVES IN A PATH

Try this example online: http://jsbin.com/wifowa/1/edit

path with bezier curve, quadratic curve and line in the same, closed path

Extract from source code:

  1. context.beginPath();
  2. context.moveTo(100, 20);
  3. context.lineTo(200, 160);
  4. context.quadraticCurveTo(230, 200, 250, 120);
  5. context.bezierCurveTo(290, 40, 300, 200, 400, 150);
  6. context.lineTo(500, 90);
  7. // TRY COMMENTING THIS LINE OUT
  8. context.closePath();
  9. context.lineWidth = 5;
  10. context.strokeStyle = “#0000ff”;
  11. 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”:

path composition explained

INTERESTING, INTERACTIVE TOOL FOR GENERATING CODE THAT DRAWS BEZIER CURVES

This tool is available online: http://www.victoriakirst.com/beziertool/, try it!

Screenshot:

bezier curves code generator

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s