当前位置: 首页 > news >正文

JS 面向对象例题

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>小球碰到返回</title>
</head>

<style>
#canvas{
    border: 1px dashed #999;
    box-shadow:0px 4px 40px #233;
    /*background: black;*/
}
</style>

<body>

<canvas id="canvas" width="800" height="600"></canvas>

<script type="text/javascript">
    var canvas=document.getElementById("canvas");
    var context=canvas.getContext("2d");
    var maxWidth=canvas.width;
    var maxHeight=canvas.height;
    var colors = ["#33B5E5", "#0099CC", "#AA66CC", "#9933CC", "#99CC00", "#669900", "#FFBB33", "#FF8800", "#FF4444", "#CC0000"]

    //随机数
    function random(min,max){
        return Math.floor(Math.random()*(max-min)+min)
    }

    //构造函数
    function Ball(){
        this.a=true;
        this.b=true;
        this.r=random(10,30);
        this.ballColor={color: colors[Math.floor(Math.random() * colors.length)]}
        this.vx=random(30,maxWidth-30);
        this.vy=random(30,maxHeight-30);
        this.ispeed=random(1,10);
        this.ispeed2=random(1,10);
    }

    // 面向对象
    Ball.prototype.moveBall=function(){
        context.beginPath();
        if (this.a) {
            this.vx += this.ispeed;
            if (this.vx>=maxWidth-this.r) {    
                this.a = false;
            }
        } else {
            this.vx -= this.ispeed;
            if (this.vx <= this.r) {
                this.a = true;
            }
        }

        if (this.b) {
            this.vy+= this.ispeed2;
            if (this.vy >= maxHeight-this.r) {
                this.b = false;
            }

        } else {
            this.vy -= this.ispeed2;
            if (this.vy <= this.r) {
                this.b = true;
            }
        }

        context.fillStyle=this.ballColor.color;
        context.arc(this.vx,this.vy,this.r,0,Math.PI*2,false);
        context.fill();
    }

    var Aball=[];
    for(var i=0;i<100;i++){
        Aball[i]=new Ball();
    }

    setInterval(function(){
        context.clearRect(0,0,canvas.width,canvas.height)
        for(var i=0;i<100;i++){
            Aball[i].moveBall();
        }
        
    },30)

</script>

</body>
</html>
cava小球

2.冒泡

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{padding: 0;margin: 0;}
            html,body{width: 100%;
                    height: 100%;
                    overflow: hidden;
                    background: #000;
            }
        </style>
    </head>
    <body>

        <script>
            var canvas=document.createElement("canvas")
            var ctx=canvas.getContext("2d")
            var w=window.innerWidth;
            var h=window.innerHeight;
            init()
            function init(){
                canvas.width=w;
                canvas.height=h;
                document.body.appendChild(canvas)
                setInterval(loop,30)
            }
            function Yuan(x,y,color){
                this.x=x;
                this.y=y;
                this.xval=0;
                this.yval=-9;
                this.gro=0.1;
                this.color=color;
                this.yuan=function(){
                    ctx.beginPath()
                    ctx.fillStyle=this.color
                    ctx.arc(this.x,this.y,10,0,Math.PI*2)
                    ctx.fill()
                    ctx.closePath()
                }
                this.weizi=function(){
                    this.yval+=this.gro
                    this.x+=this.xval
                    this.y+=this.yval
                    
                }
                
            }
            var arr=[];
            function loop(){
                var x=w*0.5
                var y=h*0.8
                ctx.clearRect(0,0,w,h)
                var col=sColor()
                var yuan=new Yuan(x,y,col)
                    yuan.xval=Math.random()*4
                arr.push(yuan)
                //console.log(arr.length)
                for(var i in arr){
                    arr[i].yuan()
                    arr[i].weizi()
                }
                if(arr.length>500){
                    arr.shift()
                }        
            }
            function sColor(){
                return "rgba("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+",1)"
            }
        </script>
    </body>
</html>
冒泡

 

转载于:https://www.cnblogs.com/ordinaryk/p/6579364.html

相关文章:

  • Idea中的插件-列出Java Bean的所有set方法
  • JavaScript的数据类型与变量
  • Android 权限的实现
  • 看《神探夏洛克》经典台词
  • 挂载硬盘,并分区格式化
  • JavaScript中的对象
  • 用vs2015 编译 web app ionic
  • HTTP访问控制(CORS)
  • 02_SimpleTrigger
  • gbdt调参的小结
  • BZOJ 2055: 80人环游世界 [上下界费用流]
  • 给object数组进行排序(排序条件是每个元素对象的属性个数)
  • spring(一) IOC讲解
  • java中字符串数组、字符串、整形之间的转换。
  • Bzoj 1857: [Scoi2010]传送带(三分套三分)
  • bootstrap创建登录注册页面
  • happypack两次报错的问题
  • HTML中设置input等文本框为不可操作
  • Java,console输出实时的转向GUI textbox
  • JAVA_NIO系列——Channel和Buffer详解
  • KMP算法及优化
  • PyCharm搭建GO开发环境(GO语言学习第1课)
  • Spring框架之我见(三)——IOC、AOP
  • 每天10道Java面试题,跟我走,offer有!
  • 浅谈Golang中select的用法
  • 网络应用优化——时延与带宽
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • 中文输入法与React文本输入框的问题与解决方案
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​520就是要宠粉,你的心头书我买单
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • #Linux(帮助手册)
  • ${factoryList }后面有空格不影响
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (13)Hive调优——动态分区导致的小文件问题
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (备忘)Java Map 遍历
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • (转)Unity3DUnity3D在android下调试
  • .net core 6 集成 elasticsearch 并 使用分词器
  • .NET 回调、接口回调、 委托
  • .NET开发人员必知的八个网站
  • /ThinkPHP/Library/Think/Storage/Driver/File.class.php  LINE: 48
  • @RequestMapping用法详解
  • [ linux ] linux 命令英文全称及解释
  • [ 常用工具篇 ] AntSword 蚁剑安装及使用详解
  • [android学习笔记]学习jni编程
  • [codeforces]Recover the String
  • [dfs] 图案计数