如何用原生JS将复杂物体间的碰撞检测改写为长尾词?

2026-04-05 09:348阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计553个文字,预计阅读时间需要3分钟。

如何用原生JS将复杂物体间的碰撞检测改写为长尾词?

本文分享了一个简单的碰撞检测JavaScript代码示例,适合作为参考。代码用于检测两个矩形(box1和box2)是否发生碰撞,若发生碰撞,则box2的颜色会随机改变。

javascript// 获取box1和box2的属性const box1={ x: 100, y: 100, width: 50, height: 50, color: 'red'};

const box2={ x: 150, y: 150, width: 50, height: 50};

// 碰撞检测函数function detectCollision(box1, box2) { return ( box1.x box2.x && box1.y box2.y );}

// 碰撞处理函数function handleCollision(box1, box2) { if (detectCollision(box1, box2)) { box2.color=Math.random() * 16777215.toString(16).padStart(6, '0'); }}

// 模拟碰撞检测function simulate() { const interval=setInterval(()=> { handleCollision(box1, box2); // 输出box2的颜色 console.log(`box2 color: ${box2.color}`); }, 1000);}

如何用原生JS将复杂物体间的碰撞检测改写为长尾词?

simulate();

本文实例为大家分享了js实现碰撞检测的具体代码,供大家参考,具体内容如下

随手写了个简单的碰撞检测的代码。检测box1和box2是否发生碰撞,若发生碰撞,box2颜色发生随机改变,并反弹到随机位置。

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box1,.box2{ width: 100px; height: 100px; background-color: #f00; position:absolute; } .box2{ background-color: #00f; left: 200px; top: 200px; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> </body> <script> var box1=document.querySelector(".box1"); var box2=document.querySelector(".box2"); box1.addEventListener("mousedown",mouseHandler); function mouseHandler(e){ if(e.type==="mousedown"){ e.preventDefault(); document.elem=this; document.pointX= e.offsetX; document.pointY= e.offsetY; document.addEventListener("mousemove",mouseHandler); this.addEventListener("mouseup",mouseHandler); }else if(e.type==="mousemove"){ this.elem.style.left= e.x-this.pointX+"px"; this.elem.style.top= e.y-this.pointY+"px"; hitText(this.elem,box2); }else if(e.type==="mouseup"){ document.removeEventListener("mousemove",mouseHandler); this.removeEventListener("mouseup",mouseHandler); } } function hitText(elem1,elem2){ var rect1=elem1.getBoundingClientRect(); var rect2=elem2.getBoundingClientRect(); var ponit1={x:rect1.x,y:rect1.y}; var ponit4={x:rect1.x+rect1.width,y:rect1.y+rect1.height}; if( ponit4.x>rect2.x &&ponit1.x<(rect2.x+rect2.width) &&ponit4.y>rect2.y &&ponit1.y<(rect2.y+rect2.height)){ elem2.style.backgroundColor=randomColor(); elem2.style.left=Math.round(Math.random()*document.documentElement.clientWidth)+"px"; elem2.style.top=Math.round(Math.random()*document.documentElement.clientHeight)+"px"; } } function randomColor(){ var a=Math.round(Math.random()*255); var b=Math.round(Math.random()*255); var c=Math.round(Math.random()*255); var color="rgb("+ a+","+b+","+c+")"; return color; } </script> </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计553个文字,预计阅读时间需要3分钟。

如何用原生JS将复杂物体间的碰撞检测改写为长尾词?

本文分享了一个简单的碰撞检测JavaScript代码示例,适合作为参考。代码用于检测两个矩形(box1和box2)是否发生碰撞,若发生碰撞,则box2的颜色会随机改变。

javascript// 获取box1和box2的属性const box1={ x: 100, y: 100, width: 50, height: 50, color: 'red'};

const box2={ x: 150, y: 150, width: 50, height: 50};

// 碰撞检测函数function detectCollision(box1, box2) { return ( box1.x box2.x && box1.y box2.y );}

// 碰撞处理函数function handleCollision(box1, box2) { if (detectCollision(box1, box2)) { box2.color=Math.random() * 16777215.toString(16).padStart(6, '0'); }}

// 模拟碰撞检测function simulate() { const interval=setInterval(()=> { handleCollision(box1, box2); // 输出box2的颜色 console.log(`box2 color: ${box2.color}`); }, 1000);}

如何用原生JS将复杂物体间的碰撞检测改写为长尾词?

simulate();

本文实例为大家分享了js实现碰撞检测的具体代码,供大家参考,具体内容如下

随手写了个简单的碰撞检测的代码。检测box1和box2是否发生碰撞,若发生碰撞,box2颜色发生随机改变,并反弹到随机位置。

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box1,.box2{ width: 100px; height: 100px; background-color: #f00; position:absolute; } .box2{ background-color: #00f; left: 200px; top: 200px; } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> </body> <script> var box1=document.querySelector(".box1"); var box2=document.querySelector(".box2"); box1.addEventListener("mousedown",mouseHandler); function mouseHandler(e){ if(e.type==="mousedown"){ e.preventDefault(); document.elem=this; document.pointX= e.offsetX; document.pointY= e.offsetY; document.addEventListener("mousemove",mouseHandler); this.addEventListener("mouseup",mouseHandler); }else if(e.type==="mousemove"){ this.elem.style.left= e.x-this.pointX+"px"; this.elem.style.top= e.y-this.pointY+"px"; hitText(this.elem,box2); }else if(e.type==="mouseup"){ document.removeEventListener("mousemove",mouseHandler); this.removeEventListener("mouseup",mouseHandler); } } function hitText(elem1,elem2){ var rect1=elem1.getBoundingClientRect(); var rect2=elem2.getBoundingClientRect(); var ponit1={x:rect1.x,y:rect1.y}; var ponit4={x:rect1.x+rect1.width,y:rect1.y+rect1.height}; if( ponit4.x>rect2.x &&ponit1.x<(rect2.x+rect2.width) &&ponit4.y>rect2.y &&ponit1.y<(rect2.y+rect2.height)){ elem2.style.backgroundColor=randomColor(); elem2.style.left=Math.round(Math.random()*document.documentElement.clientWidth)+"px"; elem2.style.top=Math.round(Math.random()*document.documentElement.clientHeight)+"px"; } } function randomColor(){ var a=Math.round(Math.random()*255); var b=Math.round(Math.random()*255); var c=Math.round(Math.random()*255); var color="rgb("+ a+","+b+","+c+")"; return color; } </script> </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。