• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

canvas_18滚动的小球-小程序版

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<template>
    <view>

        <!-- #ifdef APP-PLUS || H5 -->
        <canvas canvas-id="canvas" class="canvas" :change:start="animate.start" :start="startStatus"
            :data-width="canvasWidth" :data-height="canvasHeight" :style="canvasStyle"></canvas>
        <!-- #endif -->

        <!-- #ifndef APP-PLUS || H5 -->
        <canvas canvas-id="canvas" class="canvas" :style="canvasStyle"></canvas>
        <!-- #endif -->
    </view>
</template>

<script>
    // #ifndef APP-PLUS || H5
    let ctx = null,
        interval = null;

    function randNum(num) {
        return Math.random() * num;
    }

    function Ball(ctx, w, h) {
        this.ctx = ctx;
        this.w = w;
        this.h = h;
        this.x = randNum(5) + 60; // 60 防止卡住
        this.y = randNum(3) + 60;
        this.r = randNum(50) + 10; // [10, 60)
        this.xSpeed = randNum(3) + 2; // [2, 5)
        this.ySpeed = randNum(3) + 1; // [1, 4)
        // this.color = "blue";
        this.color = "rgb(" + parseInt(Math.random() * 255) +
            "," + parseInt(Math.random() * 255) +
            "," + parseInt(Math.random() * 255) + ")";
    }

    Ball.prototype.show = function() {
        // 更新球坐标
        this.run();
        this.ctx.beginPath();
        this.ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);
        this.ctx.setFillStyle(this.color);
        this.ctx.fill();
    }

    Ball.prototype.run = function() {
        if (this.x + this.r >= this.w || this.x - this.r <= 0) {
            this.xSpeed = -this.xSpeed
        }

        if (this.y + this.r >= this.h || this.y - this.r <= 0) {
            this.ySpeed = -this.ySpeed
        }

        this.x += this.xSpeed;
        this.y += this.ySpeed;
    }

    // #endif

    export default {
        name: "zk-star",
        data() {
            return {
                title: 'canvas',
                canvasWidth: 0,
                canvasHeight: 0,
                startStatus: false,
                ballList: [],
            };
        },
        mounted: function() {
            this.$nextTick(() => {
                uni.createSelectorQuery().in(this).select(".canvas").boundingClientRect(data => {
                    // #ifdef APP-PLUS || H5
                    this.startStatus = true;
                    // #endif

                    // #ifndef APP-PLUS || H5
                    ctx = uni.createCanvasContext('canvas', this);
                    this.drawBall();
                    // #endif

                }).exec()
            })
        },
        computed: {
            canvasStyle() {
                const info = uni.getSystemInfoSync();
                this.canvasHeight = info.windowHeight;
                this.canvasWidth = info.windowWidth;
                let retStyle = "height:" + this.canvasHeight + "px;";
                retStyle += "width:" + this.canvasWidth + "px;";
                // retStyle += "background: #060e1b;";
                return retStyle;
            },
        },

        // #ifndef APP-PLUS || H5
        onUnload: function() {

        },

        methods: {
            drawBall: function() {
                let w = this.canvasWidth;
                let h = this.canvasHeight;
                var ballArr = [];
                for (var i = 0; i < 50; i++) {
                    var ball = new Ball(ctx, w, h);
                    ballArr.push(ball);
                    ball.show();
                }

                function animation() {
                    ctx.clearRect(0, 0, w, h);
                    for (var i = 0; i < ballArr.length; i++) {
                        var ball = ballArr[i];
                        ball.show();
                    }
                    ctx.draw();
                    interval = setInterval(function() {
                        animation()
                    }, 50)
                }
                animation()
            }
        }
        // #endif
    }
</script>

<script module="animate" lang="renderjs">
    function randNum(num) {
        return Math.random() * num;
    }

    function Ball(ctx, w, h) {
        this.ctx = ctx;
        this.w = w;
        this.h = h;
        this.x = randNum(5) + 60; // 60 防止卡住
        this.y = randNum(3) + 60;
        this.r = randNum(50) + 10; // [10, 60)
        this.xSpeed = randNum(3) + 2; // [2, 5)
        this.ySpeed = randNum(3) + 1; // [1, 4)
        this.color = "#" + parseInt(Math.random() * 0xffffff).toString(16);
    }

    Ball.prototype.show = function() {
        // 更新球坐标
        this.run();
        this.ctx.beginPath();
        this.ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);
        this.ctx.fillStyle = this.color;
        this.ctx.fill();
    }

    Ball.prototype.run = function() {
        if (this.x + this.r >= this.w || this.x - this.r <= 0) {
            this.xSpeed = -this.xSpeed
        }

        if (this.y + this.r >= this.h || this.y - this.r <= 0) {
            this.ySpeed = -this.ySpeed
        }

        this.x += this.xSpeed;
        this.y += this.ySpeed;

    }

    export default {
        methods: {
            start(newVal, oldVal, owner, ins) {
                var canvas = document.querySelectorAll('.canvas>canvas')[0];
                var ctx = canvas.getContext("2d");
                let w = this.canvasWidth;
                let h = this.canvasHeight;
                var ballArr = [];
                for (var i = 0; i < 50; i++) {
                    var ball = new Ball(ctx, w, h);
                    ballArr.push(ball);
                    ball.show();
                }

                function animation() {
                    ctx.clearRect(0, 0, w, h);
                    for (var i = 0; i < ballArr.length; i++) {
                        var ball = ballArr[i];
                        ball.show();
                    }
                    requestAnimationFrame(function() {
                        animation()
                    })
                }
                animation();
            }
        }
    }
</script>

<style lang="scss">

</style>

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何推广微信小程序到企业微信 - shenzen_小白发布时间:2022-07-18
下一篇:
踩一踩微信小程序开发的坑---tabBar发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap