An interactive example of the canvas bezierCurveTo functionRecommend RGraph:
Posted on 29th January 2013
An interactive example of the canvas bezierCurveTo function. You can drag and drop the points and see the effect that it has on both the curve and also the code.
This is a demonstration of how to use the bezierCurveTo() function of the canvas 2D API. A bezier curve is a type of curve which is available to you via the function built into the canvas API. The example here should help you to understand them because the control points (the coordinates that you give to the .bezierCurveTo() function ) are illustrated and you can also drag them around with your mouse - showing the effect on the curve. The resulting code is shown below.
This is the code that can be used to achieve the curve (the points are color coded to match the output above):
The arguments to the function
The bezierCurveTo() function take six arguments - ie three sets of X/Y coordinates. The curve is actually made up of four points - but the first point is the current point in your path. If you're starting a new path with the bezierCurveTo() function then you need to use the moveTo function to move to the start point.
This is the moveTo() call in the example above. The bezier curve starts from this point.
Control point 1
This is the first set of coordinates that you give to the bezierCurveTo() function. This point dictates the shape of the curve.
Control point 2
This is the second set of coordinates that you give to the bezierCurveTo() function. This point dictates the shape of the curve.
This is the third set of coordinates given to the bezierCurveTo() function. This is where the curve ends.