简单前端——圆与圆碰撞检测

只是没什么做搞个来玩玩,顺便练练手

鼠标按着红球,就可以移动红球,如果以黄球发生碰撞,红球就变为绿球,否则颜色不变。



[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8">  
  5.     <title></title>  
  6.     <style>  
  7.         #a , #b {  
  8.             width: 100px;  
  9.             height: 100px;  
  10.             position: absolute;  
  11.             border-radius: 50%;  
  12.         }  
  13.         #a{  
  14.             left: 0;  
  15.             top: 0;  
  16.             background-color: red;  
  17.             z-index: 1;  
  18.         }  
  19.         #b {  
  20.             left: 0;  
  21.             top: 0;  
  22.             right: 0;  
  23.             bottom: 0;  
  24.             margin: auto;  
  25.             background-color: yellow;  
  26.         }  
  27.     </style>  
  28. </head>  
  29. <body>  
  30.     <div id="a"></div>  
  31.     <div id="b"></div>  
  32. <script>  
  33.     var a = document.getElementById('a');    
  34.     var b = document.getElementById('b');  
  35.     a.onmousedown = function (ev) {   
  36.         aX = ev.clientX - a.offsetLeft;    
  37.         aY = ev.clientY - a.offsetTop;  
  38.         document.onmousemove = function (ev) {  
  39.             var t1 = a.offsetTop;    
  40.             var l1 = a.offsetLeft;  
  41.   
  42.             var t2 = b.offsetTop;    
  43.             var l2 = b.offsetLeft;   
  44.             if (Math.sqrt(Math.pow(t1 - t2, 2) + Math.pow(l1 - l2, 2)) <= 100) {    
  45.                 // 如果两圆的圆心距小于或等于两圆半径和则认为发生碰撞    
  46.                 a.style.background = 'green';  
  47.             }else{  
  48.                 a.style.background = 'red';  
  49.             }  
  50.   
  51.             a.style.left = ev.clientX - aX +'px';    
  52.             a.style.top = ev.clientY - aY +'px';    
  53.         }  
  54.         document.onmouseup = function () {    
  55.             document.onmousemove = null;    
  56.             document.onmouseup = null;    
  57.   
  58.         }    
  59.         return false;    
  60.     }  
  61. </script>  
  62. </body>  
  63. </html>  


转载来自:http://blog.csdn.net/zhuxiongyin/article/details/71499843
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值