美文网首页
canves 海浪

canves 海浪

作者: Ray_afab | 来源:发表于2020-07-19 13:24 被阅读0次
    WX20200719-132412@2x.png
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        
    <canvas id="myCanvas"></canvas>
    
    <script>
        var WAVE_HEIGHT = 200 //波浪变化高度
        var SCALE = 1 // 绘制速率
        var CYCLE = 360 // SCALE
        var TIME = 0
    
        function initCanvas() {
            var c = document.getElementById("myCanvas")
            var width = window.screen.width
            var height = window.screen.height
    
            var ctx = c.getContext("2d")
            c.width = width
            c.height = height
    
            // start
            window.requestAnimationFrame(function() {
                console.log(new Date().getTime())
                console.log(123)
                draw(ctx, width, height)
            })
        }
    
        function draw(ctx, width, height) {
            ctx.clearRect(0, 0, width, height)
            TIME = (TIME + 1)
            var angle = SCALE * TIME // 当前正弦角度
            var dAngle = 60 // 两个波峰相差的角度
            ctx.beginPath()
            ctx.moveTo(0, height * 0.4 + distance(WAVE_HEIGHT, angle, 0))
            ctx.bezierCurveTo(
                width * 0.4,
                height * 0.5 + distance(WAVE_HEIGHT, angle, dAngle),
                width * 0.6,
                height * 0.5 + distance(WAVE_HEIGHT, angle, 2 * dAngle),
                width,
                height * 0.5 + distance(WAVE_HEIGHT, angle, 3 * dAngle)
            )
            ctx.lineTo(2000,1600);
            ctx.lineTo(0,1600);
            ctx.closePath();
            ctx.fillStyle = "rgba(24, 207, 196, 1.0)";
            ctx.fill();
            ctx.strokeStyle = "yellow"
            ctx.stroke()
            //第二条
            ctx.beginPath()
            ctx.moveTo(0, height * 0.5 + distance(WAVE_HEIGHT, angle, -30))
            ctx.bezierCurveTo(
                width * 0.3,
                height * 0.5 + distance(WAVE_HEIGHT, angle, dAngle),
                width * 0.7,
                height * 0.5 + distance(WAVE_HEIGHT, angle, 2 * dAngle),
                width,
                height * 0.5 + distance(WAVE_HEIGHT, angle, 3 * dAngle)
            )
            ctx.lineTo(2000,1600);
            ctx.lineTo(0,1600);
            ctx.closePath();
            ctx.fillStyle = 'rgba(39, 239, 159, 1.0)';
            ctx.fill();
            ctx.strokeStyle = "yellow"
            ctx.stroke()
    
    
            //disi
            ctx.beginPath()
            ctx.moveTo(0, height * 0.5 + distance(WAVE_HEIGHT, angle, 30))
            ctx.bezierCurveTo(
                width * 0.1,
                height * 0.5 + distance(WAVE_HEIGHT, angle, dAngle),
                width * 0.5,
                height * 0.5 + distance(WAVE_HEIGHT, angle, 2 * dAngle),
                width,
                height * 0.5 + distance(WAVE_HEIGHT, angle, 3 * dAngle)
            )
            ctx.lineTo(2000,1600);
            ctx.lineTo(0,1600);
            ctx.closePath();
            ctx.fillStyle = 'rgba(0, 158, 252, 1.0)';
            ctx.fill();
            ctx.strokeStyle = "yellow"
            ctx.stroke()
            function distance(height, currAngle, diffAngle) {
                return height * Math.cos((((currAngle - diffAngle) % 360) * Math.PI * 2) / 360)
            }
    
            // animate
            window.requestAnimationFrame(function() {
                draw(ctx, width, height)
            })
        }
    
        initCanvas()
    </script>
    </body>
    <style>
        body{
            background: skyblue;
        }
        *{
            margin: 0;
            padding: 0;
        }
    </style>
    </html>
    

    相关文章

      网友评论

          本文标题:canves 海浪

          本文链接:https://www.haomeiwen.com/subject/zgilkktx.html