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:
Name | Description | Returns |
---|---|---|
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>
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>
Creating a Clipping Region
The clip method:
Name | Description | Returns |
---|---|---|
clip() | Creates a new clipping region | void |
<!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>
Home
HTML CSS Book
HTML
HTML CSS Book
HTML
canvas:
- Getting Started with the Canvas Element
- Getting a Canvas Context
- Drawing Rectangles
- Canvas Drawing State
- Setting the Line Join Style
- Using Gradients
- Using Patterns
- Using smaller shapes with an image pattern
- Drawing Images
- Using Video Images
- Using Canvas Images
- Setting the Fill & Stroke Styles
- Saving and Restoring Drawing State
- Drawing Using Paths
- Drawing Arcs
- Drawing Bezier Curves
- Drawing Text
- Using Shadows
- Using Transparency
Related: