CanvasRenderingContext2D.bezierCurveTo()
CanvasRenderingContext2D
.bezierCurveTo()
是 Canvas 2D API 绘制三次贝赛尔曲线路径的方法。 该方法需要三个点。 第一、第二个点是控制点,第三个点是结束点。起始点是当前路径的最后一个点,绘制贝赛尔曲线前,可以通过调用 moveTo()
进行修改。
语法
void ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
参数
cp1x
- 第一个控制点的 x 轴坐标。
cp1y
- 第一个控制点的 y 轴坐标。
cp2x
- 第二个控制点的 x 轴坐标。
cp2y
- 第二个控制点的 y 轴坐标。
x
- 结束点的 x 轴坐标。
y
- 结束点的 y 轴坐标。
示例
使用 bezierCurveTo
方法
这是一段绘制贝赛尔曲线的简单的代码片段。 控制点是红色的, 开始和结束点是蓝色的。
HTML
<canvas id="canvas"></canvas>
JavaScript
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(50,20);
ctx.bezierCurveTo(230, 30, 150, 60, 50, 100);
ctx.stroke();
ctx.fillStyle = 'blue';
// start point
ctx.fillRect(50, 20, 10, 10);
// end point
ctx.fillRect(50, 100, 10, 10);
ctx.fillStyle = 'red';
// control point one
ctx.fillRect(230, 30, 10, 10);
// control point two
ctx.fillRect(150, 70, 10, 10);
尝试 bezierCurveTo
参数
修改下面的代码并在线查看 canvas 的变化:
规范描述
Specification | Status | Comment |
---|---|---|
HTML Living Standard CanvasRenderingContext2D.beziercurveto |
Living Standard |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help! (en-US)
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |