CanvasRenderingContext2D.bezierCurveTo()

The CanvasRenderingContext2D.bezierCurveTo() method of the Canvas 2D API adds a cubic Bézier curve to the current sub-path. It requires three points: the first two are control points and the third one is the end point. The starting point is the last point in the current path, which can be changed using moveTo() before creating the Bézier curve.

Syntax

void ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);

Parameters

cp1x
The x-axis coordinate of the first control point.
cp1y
The y-axis coordinate of the first control point.
cp2x
The x-axis coordinate of the second control point.
cp2y
The y-axis coordinate of the second control point.
x
The x-axis coordinate of the end point.
y
The y-axis coordinate of the end point.

Examples

How bezierCurveTo works

This example shows how a Bézier curve is drawn. The control points are red and the start and end points are blue.

HTML

<canvas id="canvas"></canvas>

JavaScript

//  Define Canvas & Context
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');

//  Define the Points as {x,y}
    var start=  { x: 50,    y: 20  };
    var cp1=    { x: 230,   y: 30  };
    var cp2=    { x: 150,   y: 60  };
    var end=    { x: 50,    y: 100 };

//  Draw Curve
    ctx.beginPath();
    ctx.moveTo(start.x,start.y);
    ctx.bezierCurveTo(cp1.x, cp1.y, cp2.x, cp2.y, end.x, end.y);
    ctx.stroke();

//  Start & End Points
    ctx.fillStyle = 'blue';
    ctx.beginPath();
    ctx.arc(start.x, start.y, 5, 0, 2*Math.PI);
    ctx.arc(end.x, end.y, 5, 0, 2*Math.PI);
    ctx.fill();

//  Control Points
    ctx.fillStyle = 'red';
    ctx.beginPath();
    ctx.arc(cp1.x, cp1.y, 5, 0, 2*Math.PI);
    ctx.arc(cp2.x, cp2.y, 5, 0, 2*Math.PI);
    ctx.fill();

Result

A simple Bézier curve

HTML

<canvas id="canvas"></canvas>

JavaScript

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

ctx.beginPath();
ctx.moveTo(30, 30);
ctx.bezierCurveTo(120,160, 180,10, 220,140);
ctx.stroke();

Result

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.beziercurveto' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support

See also

Document Tags and Contributors

Contributors to this page: mfluehr, manngo, fscholz, thatmichael85, nmve, erikadoyle, bleush38p
Last updated by: mfluehr,