原生js实现移动小球(碰撞检测)


Posted in Javascript onDecember 17, 2020

本文实例为大家分享了js实现移动小球的具体代码,供大家参考,具体内容如下

</head>
<style>
 *{margin: 0;
  padding:0;}
 #main{margin: 0px auto;position: relative;}
 #main div{overflow: hidden;position: absolute;width: 50px;height: 50px;opacity: 0.5;
  -moz-border-radius: 50%;-webkit-border-radius: 50%;border-radius: 50%;}
</style>
<body>
 <div id="main">
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
 <div></div>
</div>

 <script>
 var main = document.getElementById('main');
 var circles = main.getElementsByTagName('div');
 var st = [0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f'];
 var json = [],arr=[],color = [];
 var maxW = 0;
 var maxH = 0;
 var cwidth = circles[0].offsetWidth;
 var cheight = circles[0].offsetHeight;

 //根据浏览器窗口的大小自动调节小球的运动空间
 window.onresize=function(){
  var main = document.getElementById('main');
  maxW=window.innerWidth-circles[0].clientWidth;
  maxH=window.innerHeight-circles[0].clientHeight;
  main.style.width = window.innerWidth+'px';
  main.style.height = window.innerHeight+'px';

 }
 onresize();
 //数组对象的初始化
 for(var i=0;i<circles.length;i++){
  arr=[];
  for(var j=0;j<6;j++){
   color[j] = st[Math.floor(Math.random()*16)];
  }
  arr.x = Math.floor(Math.random()*(maxW+1));//初始x坐标
  arr.y = Math.floor(Math.random()*(maxH+1));//初始y坐标
  arr.cx = arr.x + circles[0].offsetWidth/2; //圆心x坐标
  arr.cy = arr.y + circles[0].offsetHeight/2;//圆心y坐标
  arr.movex = Math.floor(Math.random()*2);//x轴移动方向
  arr.movey = Math.floor(Math.random()*2);//y轴移动方向
  arr.bgolor = '#'+ color.join('');//随机生成一个6位字符串
  arr.speed = 2+Math.floor(Math.random()*5);
  //随机生成一个2~6之间的移动速度(如果设置的改变速度太大,容易造成小球碰撞时两个小球之间有重合,也有可能小球会出界)
  arr.timer = null;//计时器
  arr.index = i;//索引值
  json.push(arr);
  circles[i].style.left = arr.x + 'px';//小球位置初始化
  circles[i].style.top = arr.y + 'px';//小球位置初始化
  circles[i].style.backgroundColor = arr.bgolor;//小球背景颜色初始化
 }
 //碰撞函数
 function crash(a){
  var ball1x = json[a].cx;
  var ball1y = json[a].cy;
  for(var i= 0;i<json.length;i++){
   if(i!=a){
    var ball2x = json[i].cx;
    var ball2y = json[i].cy;
    //圆心距离的平方
    var len = (ball1x-ball2x)*(ball1x-ball2x)+(ball1y-ball2y)*(ball1y-ball2y);
    if(len <= cwidth*cwidth){
    //小球位置的判断,发生碰撞反应
     if(ball1x >ball2x){
      if(ball1y > ball2y){
       json[a].movex=1;
       json[a].movey=1;
      }else if(ball1y < ball2y){
       json[a].movex=1;
       json[a].movey=0;
      }else{
       json[a].movex=1;
      }
     }else if(ball1x < ball2x){
      if(ball1y > ball2y){
       json[a].movex=0;
       json[a].movey=0;
      }else if(ball1y < ball2y){
       json[a].movex=0;
       json[a].movey=1;
      }else{
       json[a].movex=0;
      }
     }else{
      if(ball1y > ball2y){
       json[a].movey=1;
      }else if(ball1y < ball2y){
       json[a].movey=0;
      }
     }
    }
   }

  }
 }
 //移动函数
 function move(circle){
  circle.timer = setInterval(function () {
   if(circle.movex == 1){
    circle.x+=circle.speed;
    if(circle.x+circle.speed >= maxW){//防止小球出界
     circle.x = maxW;
     circle.movex=0;//小球运动方向发生改变
    }
   }else{
    circle.x-=circle.speed;
    if(circle.x-circle.speed <= 0){
     circle.x = 0;
     circle.movex=1;
    }
   }
   if(circle.movey == 1){
    circle.y += circle.speed;
    if(circle.y+circle.speed >= maxH){
     circle.y = maxH;
     circle.movey=0;
    }
   }else{
    circle.y-=circle.speed;
    if(circle.y-circle.speed <= 0){
     circle.y = 0;
     circle.movey=1;
    }
   }
   circle.cx = circle.x + circles[0].offsetWidth/2;//小球每一次运动圆心都会发生改变
   circle.cy = circle.y + circles[0].offsetHeight/2;
   circles[circle.index].style.left = circle.x + 'px';//小球位置重定位
   circles[circle.index].style.top = circle.y + 'px';
   /*console.log('x'+circle.cx+'y'+circle.cy);*/
   crash(circle.index);
  },16);
 }
 //对每一个小球绑定计时器,让小球动起来
 for(var i=0;i<circles.length;i++){
  move(json[i]);
 }
</script>
</body>

效果图:

原生js实现移动小球(碰撞检测)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
jQuery 工具函数学习资料
Apr 29 Javascript
jQuery AjaxQueue改进步骤
Oct 06 Javascript
Javacript实现颜色梯度变化和渐变的效果代码
May 31 Javascript
div模拟滚动条效果示例代码
Oct 16 Javascript
JQuery中$(document)是什么意思有什么作用
Jul 21 Javascript
使用js实现数据格式化
Dec 03 Javascript
JavaScript的instanceof运算符学习教程
Jun 08 Javascript
基于BootStrap的Metronic框架实现页面链接收藏夹功能按钮移动收藏记录(使用Sortable进行拖动排序)
Aug 29 Javascript
30分钟快速入门掌握ES6/ES2015的核心内容(下)
Apr 18 Javascript
解决angularjs前后端分离调用接口传递中文时中文乱码的问题
Aug 13 Javascript
react native基于FlatList下拉刷新上拉加载实现代码示例
Sep 30 Javascript
js实现购物车商品数量加减
Sep 21 Javascript
vue+elementUI动态增加表单项并添加验证的代码详解
Dec 17 #Vue.js
Taro小程序自定义顶部导航栏功能的实现
Dec 17 #Javascript
vue 数据操作相关总结
Dec 17 #Vue.js
Vue 组件注册全解析
Dec 17 #Vue.js
js回到页面指定位置的三种方式
Dec 17 #Javascript
javascript 数组(list)添加/删除的实现
Dec 17 #Javascript
使用webpack5从0到1搭建一个react项目的实现步骤
Dec 16 #Javascript
You might like
php中debug_backtrace、debug_print_backtrace和匿名函数用法实例
2014/12/01 PHP
ThinkPHP5.1框架数据库链接和增删改查操作示例
2019/08/03 PHP
PHP+Mysql分布式事务与解决方案深入理解
2021/02/27 PHP
免费空间广告万能消除代码
2006/09/04 Javascript
js身份证判断方法支持15位和18位
2014/03/18 Javascript
jQuery使用post方法提交数据实例
2015/03/25 Javascript
JavaScript按值删除数组元素的方法
2015/04/24 Javascript
工作中常用的js、jquery自定义扩展函数代码片段汇总
2016/12/22 Javascript
vue2.0数据双向绑定与表单bootstrap+vue组件
2017/02/27 Javascript
JQuery EasyUI的一些常用组件
2017/07/12 jQuery
ReactNative短信验证码倒计时控件的实现代码
2017/07/20 Javascript
jquery实现下拉菜单的手风琴效果
2017/07/23 jQuery
jQuery实现滚动到底部时自动加载更多的方法示例
2018/02/18 jQuery
微信小程序实现跑马灯效果完整代码(附效果图)
2018/05/30 Javascript
vue实现短信验证码输入框
2020/04/17 Javascript
在vue中使用Base64转码的案例
2020/08/07 Javascript
python冒泡排序算法的实现代码
2013/11/21 Python
Python爬虫实例爬取网站搞笑段子
2017/11/08 Python
Numpy中转置transpose、T和swapaxes的实例讲解
2018/04/17 Python
python调用摄像头显示图像的实例
2018/08/03 Python
python 获取sqlite3数据库的表名和表字段名的实例
2019/07/17 Python
wxPython实现带颜色的进度条
2019/11/19 Python
Python3 Click模块的使用方法详解
2020/02/12 Python
django-orm F对象的使用 按照两个字段的和,乘积排序实例
2020/05/18 Python
Django执行源生mysql语句实现过程解析
2020/11/12 Python
HTML5本地存储之Database Storage应用介绍
2013/01/06 HTML / CSS
借助HTML5 Canvas API制作一个简单的猜字游戏
2016/03/25 HTML / CSS
Canvas实现贝赛尔曲线轨迹动画的示例代码
2019/04/25 HTML / CSS
高级Java程序员面试要点
2013/08/02 面试题
会计实习自我鉴定
2013/12/04 职场文书
村官学习十八大感想
2014/01/15 职场文书
支部鉴定材料
2014/06/02 职场文书
营销总经理岗位职责范本
2014/09/02 职场文书
“三支一扶”支教教师思想汇报
2014/09/13 职场文书
2014年大学班长工作总结
2014/11/14 职场文书
护理工作心得体会
2016/01/22 职场文书