构造函数&&类--canvas小球碰撞

1. 构造函数 canvas小球碰撞

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>构造函数运动球</title>
        <style type="text/css">
            canvas {
                border: 1px solid red;
            }
        </style>
    </head>
    <body>
        <canvas id="myCanvas" width="500" height="400">你的浏览器不支持canvas</canvas>
    </body>
    <script type="text/javascript">
        var canvas = document.getElementById("myCanvas");
        var context = canvas.getContext("2d");
        
        //定义球的构造函数
        function Ball(x,y,r,color,speedx,speedy){
            this.x = x;
            this.y = y;
            this.r = r;
            this.color = color;
            this.speedx = speedx;
            this.speedy = speedy;
        }
        //花的方法
        Ball.prototype.draw = function(){
            context.beginPath();
            context.arc(this.x,this.y,this.r,0,Math.PI*2);
            context.fillStyle = this.color;
            context.fill();
        }
        //球的移动方法
        Ball.prototype.move = function(){
            this.x += this.speedx;
            this.y += this.speedy;
            //碰壁检测
            if(this.x < this.r || this.x > canvas.width - this.r){
                this.speedx *= -1;
            }
            if(this.y < this.r || this.y > canvas.height - this.r){
                this.speedy *= -1;
            }
        }
        
        //随机数函数
        function randomNum(min,max){
            return parseInt(Math.random()*(max-min)+min);
        }
        function randomColor(){
            return "rgb("+randomNum(0,256)+","+randomNum(0,256)+","+randomNum(0,256)+")";
        }
        var arr = [];
        for(var i = 0;i < 100;i++){
            var r = randomNum(1,50);
            var x = randomNum(r,canvas.width - r);
            var y = randomNum(r,canvas.height - r);
            var speedx = randomNum(1,10);
            var speedy = randomNum(1,10);
            var color = randomColor();
            var newBall = new Ball(x,y,r,color,speedx,speedy);
            arr.push(newBall);
        }
        
        //实例化球
        function act(){
            context.clearRect(0,0,canvas.width,canvas.height);
            for(var i = 0;i < arr.length;i++){
                arr[i].draw();
                arr[i].move();
            }
            window.requestAnimationFrame(act);
        }
        act();
    </script>
</html>

2. 类 canvas小球碰撞

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>构造函数运动球</title>
        <style type="text/css">
            canvas {
                border: 1px solid red;
            }
        </style>
    </head>
    <body>
        <canvas id="myCanvas" width="500" height="400">你的浏览器不支持canvas</canvas>
    </body>
    <script type="text/javascript">
        var canvas = document.getElementById("myCanvas");
        var context = canvas.getContext("2d");
        class Ball{
            constructor(x,y,r,color,speedx,speedy){
                this.x = x;
                this.y = y;
                this.r = r;
                this.color = color;
                this.speedx = speedx;
                this.speedy = speedy;
            }
            //花的方法
            draw(){
                context.beginPath();
                context.arc(this.x,this.y,this.r,0,Math.PI*2);
                context.fillStyle = this.color;
                context.fill();
            }
            //球的移动方法
            move(){
                this.x += this.speedx;
                this.y += this.speedy;
                //碰壁检测
                if(this.x < this.r || this.x > canvas.width - this.r){
                    this.speedx *= -1;
                }
                if(this.y < this.r || this.y > canvas.height - this.r){
                    this.speedy *= -1;
                }
            }
        }
        
        //随机数函数
        function randomNum(min,max){
            return parseInt(Math.random()*(max-min)+min);
        }
        function randomColor(){
            return "rgb("+randomNum(0,256)+","+randomNum(0,256)+","+randomNum(0,256)+")";
        }
        var arr = [];
        for(var i = 0;i < 100;i++){
            var r = randomNum(1,50);
            var x = randomNum(r,canvas.width - r);
            var y = randomNum(r,canvas.height - r);
            var speedx = randomNum(1,10);
            var speedy = randomNum(1,10);
            var color = randomColor();
            var newBall = new Ball(x,y,r,color,speedx,speedy);
            arr.push(newBall);
        }
        
        //实例化球
        function act(){
            context.clearRect(0,0,canvas.width,canvas.height);
            for(var i = 0;i < arr.length;i++){
                arr[i].draw();
                arr[i].move();
            }
            window.requestAnimationFrame(act);
        }
        act();
    </script>
</html>

有上可以看出,用类写面向对象的方法,思路更清晰。当然,在考虑兼容的情况下,还是使用构造函数

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,886评论 25 709
  • 一、概述 1、面向对象的概述: java是一种面向对象的编程语言,也就是说对象是这种语言的基础,没有对象了,就没有...
    玉圣阅读 630评论 0 1
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,263评论 4 61
  • 我是一个典型的标题党,之所以到现在还能清晰记得初中的一篇语文课文“爸爸的花儿落了”完全是因为它选自林海音的《...
    官官小姐阅读 291评论 0 0
  • 纽甜是一个特例独行的女人,纹身,在皮肤上面刺大朵大朵鲜艳的花朵,远远看着,有股诡异的气息。 脚踝处有一条脚链,红色...
    gnani阅读 455评论 0 3