背景波浪

前言:

<!DOCTYPE html>
<html>
<head>
    <title>水波背景</title>
    <meta charset="utf-8" />
    <style>
    html, body {width:100%; height:100%; padding:0; margin:0;}
    </style>
</head>
<body style="background-color: #092869;">
    <canvas id="canvas" style="position:absolute;top:0px;left:0px;z-index:1;"></canvas>
    <script type="text/javascript">
        var canvas = document.getElementById('canvas');  
        var ctx = canvas.getContext('2d');  
        canvas.width = canvas.parentNode.offsetWidth;  
        canvas.height = canvas.parentNode.offsetHeight;
        //如果浏览器支持requestAnimFrame则使用requestAnimFrame否则使用setTimeout  
        window.requestAnimFrame = (function(){  
        return  window.requestAnimationFrame       ||  
                window.webkitRequestAnimationFrame ||  
                window.mozRequestAnimationFrame    ||  
                function( callback ){  
                  window.setTimeout(callback, 1000 / 60);  
                };  
        })();  
        // 波浪大小
        var boHeight = canvas.height / 5;
        var posHeight = canvas.height / 1.2;
        //初始角度为0  
        var step = 0;  
        //定义三条不同波浪的颜色  
        var lines = ["rgba(0,222,255, 0.2)",  
                       "rgba(157,192,249, 0.2)",  
                       "rgba(0,168,255, 0.2)"];  
        function loop(){  
            ctx.clearRect(0,0,canvas.width,canvas.height);  
            step++;  
            //画3个不同颜色的矩形  
            for(var j = lines.length - 1; j >= 0; j--) {  
                ctx.fillStyle = lines[j];  
                //每个矩形的角度都不同,每个之间相差45度  
                var angle = (step+j*50)*Math.PI/180;  
                var deltaHeight   = Math.sin(angle) * boHeight;
                var deltaHeightRight   = Math.cos(angle) * boHeight;  
                ctx.beginPath();
                ctx.moveTo(0, posHeight+deltaHeight);  
                ctx.bezierCurveTo(canvas.width/2, posHeight+deltaHeight-boHeight, canvas.width / 2, posHeight+deltaHeightRight-boHeight, canvas.width, posHeight+deltaHeightRight);  
                ctx.lineTo(canvas.width, canvas.height);  
                ctx.lineTo(0, canvas.height);  
                ctx.lineTo(0, posHeight+deltaHeight);  
                ctx.closePath();  
                ctx.fill();  
            }
            requestAnimFrame(loop);
        }  
        loop(); 
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值