相思资源网 Design By www.200059.com
自己做了碰撞检测的封装,先看下实例demo,在看封装
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>碰撞检测</title> <style type="text/css"> *{ margin: 0; padding: 0; } #divA,#divB{ width: 200px; height: 200px; background-color: purple; font-size: 50px; line-height: 200px; text-align: center; position: absolute; color: #fff; } #divA{ left: 30px; top: 30px; z-index: 5; } #divB{ left: 300px; top: 300px; } </style> </head> <body> <div id="divA">A</div> <div id="divB">B</div> </body> <script type="text/javascript"> var divA = document.getElementById("divA"); var divB = document.getElementById("divB"); divA.onmousedown = function (e) { var event1 = window.event || e; var startX = event1.clientX; var startY = event1.clientY; document.onmousemove = function (j) { var event2 = window.event || j; var endX = event2.clientX; var endY = event2.clientY; divA.style.left = divA.offsetLeft + (endX - startX) + "px"; divA.style.top = divA.offsetTop + (endY - startY) + "px"; startX = endX; startY = endY; if (isCrash(divA,divB) == true) { divA.style.backgroundColor = "red"; } else{ divA.style.backgroundColor = "purple"; }; } } divA.onmouseup = function () { document.onmousemove = null; } function isCrash (obj1,obj2) { var boolCrash = true; var left1 = obj1.offsetLeft; var right1 = obj1.offsetLeft + obj1.offsetWidth; var top1 = obj1.offsetTop; var bottom1 = obj1.offsetTop + obj1.offsetHeight; var left2 = obj2.offsetLeft; var right2 = obj2.offsetLeft + obj2.offsetWidth; var top2 = obj2.offsetTop; var bottom2 = obj2.offsetTop + obj2.offsetHeight; if (right1 > left2 && left1 < right2 && bottom1 > top2 && top1 < bottom2) { boolCrash = true; } else{ boolCrash = false; } return boolCrash; } </script> </html>
函数封装
function isCrash (obj1,obj2) { var boolCrash = true;//假设true为碰撞 var left1 = obj1.offsetLeft; var right1 = obj1.offsetLeft + obj1.offsetWidth; var top1 = obj1.offsetTop; var bottom1 = obj1.offsetTop + obj1.offsetHeight; var left2 = obj2.offsetLeft; var right2 = obj2.offsetLeft + obj2.offsetWidth; var top2 = obj2.offsetTop; var bottom2 = obj2.offsetTop + obj2.offsetHeight; if (right1 > left2 && left1 < right2 && bottom1 > top2 && top1 < bottom2) { boolCrash = true; } else{ boolCrash = false; } return boolCrash; }
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无js实现碰撞检测特效代码分享的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。