canves 海浪

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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。