Canvas Path2D:CanvasRenderingContext2D.bezierCurveTo()

返回到:Canvas API:Path2D

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);

看个DEMO:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CanvasRenderingContext2D.bezierCurveTo() - 使用_beziercurveto_方法 | Web176教程www.web176.com</title>
        <meta name="robots" content="noindex, nofollow">
        <style>
            body {
              padding: 0;
              margin: 0;
            }

            svg:not(:root) {
              display: block;
            }

            .playable-code {
              background-color: #f4f7f8;
              border: none;
              border-left: 6px solid #558abb;
              border-width: medium medium medium 6px;
              color: #4d4e53;
              height: 100px;
              width: 90%%;
              padding: 10px 10px 0;
            }

            .playable-canvas {
              border: 1px solid #4d4e53;
              border-radius: 2px;
            }

            .playable-buttons {
              text-align: right;
              width: 90%%;
              padding: 5px 10px 5px 26px;
            }
        </style>
        
        
    </head>
    <body>
        
            <canvas id="canvas"></canvas>

        
        
            <script>
                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);

            </script>
        
    </body>
</html>

尝试 bezierCurveTo 参数

修改下面的代码并在线查看 canvas 的变化:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CanvasRenderingContext2D.bezierCurveTo() - 尝试_beziercurveto_参数 | Web176教程www.web176.com</title>
        <meta name="robots" content="noindex, nofollow">
        <style>
            body {
              padding: 0;
              margin: 0;
            }

            svg:not(:root) {
              display: block;
            }

            .playable-code {
              background-color: #f4f7f8;
              border: none;
              border-left: 6px solid #558abb;
              border-width: medium medium medium 6px;
              color: #4d4e53;
              height: 100px;
              width: 90%%;
              padding: 10px 10px 0;
            }

            .playable-canvas {
              border: 1px solid #4d4e53;
              border-radius: 2px;
            }

            .playable-buttons {
              text-align: right;
              width: 90%%;
              padding: 5px 10px 5px 26px;
            }
        </style>
        
        
    </head>
    <body>
        
            <canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas>
<div class="playable-buttons">
  <input id="edit" type="button" value="Edit" />
  <input id="reset" type="button" value="Reset" />
</div>
<textarea id="code" class="playable-code">
ctx.beginPath();
ctx.bezierCurveTo(50, 100, 180, 10, 20, 10);
ctx.stroke();</textarea>

        
        
            <script>
                var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var textarea = document.getElementById("code");
var reset = document.getElementById("reset");
var edit = document.getElementById("edit");
var code = textarea.value;

function drawCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  eval(textarea.value);
}

reset.addEventListener("click", function() {
  textarea.value = code;
  drawCanvas();
});

edit.addEventListener("click", function() {
  textarea.focus();
})

textarea.addEventListener("input", drawCanvas);
window.addEventListener("load", drawCanvas);

            </script>
        
    </body>
</html>

返回到:Canvas API:Path2D

作者:terry,如若转载,请注明出处:https://www.web176.com/canvas_api/7752.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年1月3日
下一篇 2023年1月3日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注