二次曲线和贝塞尔曲线

1 篇文章 0 订阅
1 篇文章 0 订阅

截图如下,拖动黄色控制点可以改变曲线:



 二次曲线代码:

 

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        var start = { x: 10, y: 150 }, ctl = { x: 100, y: 50 }, end = { x: 200, y: 150 };
        var mouseIndex = -1;
        function draw() {
            var ctx = document.getElementById("canvas").getContext("2d");
            ctx.clearRect(0, 0, 600, 500);
            ctx.lineWidth = 1;
            ctx.strokeStyle = "#000000";
            ctx.beginPath();
            //辅助连线
            ctx.moveTo(start.x, start.y);
            ctx.lineTo(ctl.x, ctl.y);
            ctx.lineTo(end.x, end.y);
            ctx.stroke();

            //曲线
            ctx.beginPath();
            ctx.strokeStyle = "#ff0000";
            ctx.lineWidth = 4;
            ctx.moveTo(start.x, start.y);
            ctx.quadraticCurveTo(ctl.x, ctl.y, end.x, end.y);
            ctx.stroke();

            //控制点
            ctx.beginPath();
            ctx.strokeStyle = "#000000";
            ctx.fillStyle = mouseIndex == 0 ? "rgba(250,250,0,1)" : "rgba(210,210,0,1)";
            ctx.lineWidth = 2;
            ctx.arc(start.x, start.y, 7, 0, 2 * Math.PI, false);
            ctx.fill();
            ctx.stroke();
            ctx.beginPath();
            ctx.fillStyle = mouseIndex == 1 ? "rgba(250,250,0,1)" : "rgba(210,210,0,1)";
            ctx.arc(ctl.x, ctl.y, 7, 0, 2 * Math.PI, false);
            ctx.fill();
            ctx.stroke();
            ctx.beginPath();
            ctx.fillStyle = mouseIndex == 2 ? "rgba(250,250,0,1)" : "rgba(210,210,0,1)";
            ctx.arc(end.x, end.y, 7, 0, 2 * Math.PI, false);
            ctx.fill();
            ctx.stroke();
        }
        function getRect(point) {
            return { x: point.x - 8, y: point.y - 8, width: 16, height: 16 };
        }
        function contains(rect, point) {
            return point.x > rect.x && point.x < rect.x + rect.width && point.y > rect.y && point.y < rect.y + rect.height;
        }
        function handleMove(event) {
            if (window.mousedown) {
                if (mouseIndex == 0) {
                    start = { x: event.offsetX, y: event.offsetY };
                } else if (mouseIndex == 1) {
                    ctl = { x: event.offsetX, y: event.offsetY };
                } else if (mouseIndex == 2) {
                    end = { x: event.offsetX, y: event.offsetY };
                }
                draw();
            } else {
                var newMouseIndex = mouseIndex;
                if (contains(getRect(start), { x: event.offsetX, y: event.offsetY })) {
                    newMouseIndex = 0;
                } else if (contains(getRect(ctl), { x: event.offsetX, y: event.offsetY })) {
                    newMouseIndex = 1;
                } else if (contains(getRect(end), { x: event.offsetX, y: event.offsetY })) {
                    newMouseIndex = 2;
                } else {
                    newMouseIndex = -1;
                }
                if (newMouseIndex !== mouseIndex) {
                    mouseIndex = newMouseIndex;
                    draw();
                }
            }
        }
    
    </script>
</head>
<body οnlοad="draw()" οnmοusedοwn="window.mousedown=1" οnmοuseup="window.mousedown=0">
    <canvas id="canvas" width="600" height="500" οnmοusemοve="handleMove(event)"></canvas>
</body>
</html>

 

 贝塞尔曲线:

 

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        var start = { x: 10, y: 150 }, ctl = { x: 100, y: 50 }, ctl2={x:200,y:200},end = { x: 300, y: 150 };
        var mouseIndex = -1;
        function draw() {
            var ctx = document.getElementById("canvas").getContext("2d");
            ctx.clearRect(0, 0, 600, 500);
            ctx.lineWidth = 1;
            ctx.strokeStyle = "#000000";
            ctx.beginPath();
            //辅助连线
            ctx.moveTo(start.x, start.y);
            ctx.lineTo(ctl.x, ctl.y);
            ctx.lineTo(ctl2.x, ctl2.y);
            ctx.lineTo(end.x, end.y);
            ctx.stroke();

            //曲线
            ctx.beginPath();
            ctx.strokeStyle = "#ff0000";
            ctx.lineWidth = 4;
            ctx.moveTo(start.x, start.y);
            ctx.bezierCurveTo(ctl.x, ctl.y, ctl2.x,ctl2.y,end.x, end.y);
            ctx.stroke();

            //控制点
            ctx.beginPath();
            ctx.strokeStyle = "#000000";
            ctx.fillStyle = mouseIndex == 0 ? "rgba(250,250,0,1)" : "rgba(210,210,0,1)";
            ctx.lineWidth = 2;
            ctx.arc(start.x, start.y, 7, 0, 2 * Math.PI, false);
            ctx.fill();
            ctx.stroke();
            ctx.beginPath();
            ctx.fillStyle = mouseIndex == 1 ? "rgba(250,250,0,1)" : "rgba(210,210,0,1)";
            ctx.arc(ctl.x, ctl.y, 7, 0, 2 * Math.PI, false);
            ctx.fill();
            ctx.stroke();
            ctx.beginPath();
            ctx.fillStyle = mouseIndex == 2 ? "rgba(250,250,0,1)" : "rgba(210,210,0,1)";
            ctx.arc(ctl2.x, ctl2.y, 7, 0, 2 * Math.PI, false);
            ctx.fill();
            ctx.stroke();
            ctx.beginPath();
            ctx.fillStyle = mouseIndex == 3 ? "rgba(250,250,0,1)" : "rgba(210,210,0,1)";
            ctx.arc(end.x, end.y, 7, 0, 2 * Math.PI, false);
            ctx.fill();
            ctx.stroke();
        }
        function getRect(point) {
            return { x: point.x - 8, y: point.y - 8, width: 16, height: 16 };
        }
        function contains(rect, point) {
            return point.x > rect.x && point.x < rect.x + rect.width && point.y > rect.y && point.y < rect.y + rect.height;
        }
        function handleMove(event) {
            if (window.mousedown) {
                if (mouseIndex == 0) {
                    start = { x: event.offsetX, y: event.offsetY };
                } else if (mouseIndex == 1) {
                    ctl = { x: event.offsetX, y: event.offsetY };
                } else if (mouseIndex == 2) {
                    ctl2 = { x: event.offsetX, y: event.offsetY };
                }else if (mouseIndex == 3) {
                    end = { x: event.offsetX, y: event.offsetY };
                }
                draw();
            } else {
                var newMouseIndex = mouseIndex;
                if (contains(getRect(start), { x: event.offsetX, y: event.offsetY })) {
                    newMouseIndex = 0;
                } else if (contains(getRect(ctl), { x: event.offsetX, y: event.offsetY })) {
                    newMouseIndex = 1;
                } else if (contains(getRect(ctl2), { x: event.offsetX, y: event.offsetY })) {
                    newMouseIndex = 2;
                } else if (contains(getRect(end), { x: event.offsetX, y: event.offsetY })) {
                    newMouseIndex = 3;
                }else {
                    newMouseIndex = -1;
                }
                if (newMouseIndex !== mouseIndex) {
                    mouseIndex = newMouseIndex;
                    draw();
                }
            }
        }
    
    </script>
</head>
<body οnlοad="draw()" οnmοusedοwn="window.mousedown=1" οnmοuseup="window.mousedown=0">
    <canvas id="canvas" width="600" height="500" οnmοusemοve="handleMove(event)"></canvas>
</body>
</html>
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值