canvas_32 动画-水纹效果

<template>
    <view class="zcvs">

        <view>
            <canvas canvas-id="cvs" id="cvs" style=" 300px; height: 300px;border: 1px solid #007AFF;" />
        </view>

    </view>
</template>

<script>
    export default {
        data() {
            return {};
        },
        onReady() {
            this.drawCvs();
        },
        methods: {
            drawCvs() {
                const ctx = uni.createCanvasContext('cvs');
                let canvas = {
                     300,
                    height: 300
                };
                window.requestAnimFrame = (function() {
                    return window.requestAnimationFrame ||
                        window.webkitRequestAnimationFrame ||
                        window.mozRequestAnimationFrame ||
                        function(callback) {
                            window.setTimeout(callback, 1000 / 60);
                        };
                })();

                let boHeight = canvas.height / 20;
                let posHeight = canvas.height / 1.05;
                let step = 0;
                //定义几条不同波浪的颜色 
                let lines = ["rgba(0,222,255, 0.2)", "rgba(157,192,249, 0.2)"];
                let loop = () => {
                    console.log(111);
                    ctx.clearRect(0, 0, canvas.width, canvas.height);
                    step++;
                    //画3个不同颜色的矩形 
                    for (let j = lines.length - 1; j >= 0; j--) {
                        ctx.setFillStyle(lines[j]);
                        //每个矩形的角度都不同,每个之间相差100度 
                        let angle = (step + j * 100) * Math.PI / 180;
                        let deltaHeight = Math.sin(angle) * boHeight;
                        let 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();
                        ctx.draw(true)
                    }
                    requestAnimFrame(loop);
                };
                loop();
            },
        }
    }
</script>

<style lang="scss" scoped></style>
原文地址:https://www.cnblogs.com/luwei0915/p/15388549.html