canvas贝塞尔曲线加渐变动画效果

canvas贝塞尔曲线加渐变动画效果

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Canvas</title>
</head>
    <style>
        #myCanvas{
            border:1px solid #cccccc;
        }
    </style>
<body>
    <canvas id="myCanvas" width="600" height="300">
        您的浏览器不支持 HTML5 canvas 标签。
    </canvas> 
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
    <script>
    $(function () {
        var myAction = {};

        $.extend(myAction, {
            getWidth: function (pointA, pointB) {
                var xWidth = (pointA.x - pointB.x) * (pointA.x - pointB.x);
                var yWidth = (pointA.y - pointB.y) * (pointA.y - pointB.y)
                var edgeWidth = Math.sqrt( xWidth + yWidth );
                return 100 / edgeWidth ;
            },
            initCanvas: function () {
                var canvas = document.getElementById("myCanvas");
                var ctx = canvas.getContext("2d");
                var start = 0;
                var pointA = {
                    x: 0,
                    y: 100
                };
                var pointB = {
                    x: 600,
                    y: 100
                };    
                var width = myAction.getWidth(pointA, pointB);
                function auto() {
                    ctx.beginPath();
                    ctx.moveTo(0, 100);
                    ctx.quadraticCurveTo(300, 5, 600, 100);
                    var grd = ctx.createLinearGradient(pointA.x, pointA.y, pointB.x, pointB.y);
                    grd.addColorStop(0, "#9eacb7");
                    grd.addColorStop(start, "#5BC0DE");
                    grd.addColorStop(start + (width / 2), "#ffff4d");
                    grd.addColorStop(start + width, "#01d1ad");
                    grd.addColorStop(1, "#01d1ad");
                    ctx.lineWidth = 3;
                    ctx.strokeStyle = grd;
                    ctx.stroke();
                    ctx.closePath();  

                    if (start + width + 0.02 >= 1) {
                        start = 0;
                    } else {
                        start = start + 0.02;
                    }
                    setTimeout(function() {
                        auto();
                    }, 100);        
                }
                auto();                
            }, 
        });
        var init = function () {
            myAction.initCanvas();
        }();
    });

    </script>   
</body>
</html>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值