{"id":5162,"date":"2023-05-22T23:32:00","date_gmt":"2023-05-23T04:32:00","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5162"},"modified":"2023-05-22T22:59:30","modified_gmt":"2023-05-23T03:59:30","slug":"javascript-delete-object-if-it-collides-or-overlaps-with-another-object-that-has-css-class-x","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/javascript-delete-object-if-it-collides-or-overlaps-with-another-object-that-has-css-class-x\/","title":{"rendered":"JavaScript Delete Object if it Collides or Overlaps with another object that has CSS Class X"},"content":{"rendered":"\n<p>Imagine we have a page that has anywhere from 1 to 100 floating blocks.  Now what if I want the block(s) to disappear if it runs into another type of block or a boarder?  How would we do that?<\/p>\n\n\n\n<p>First lets get a list of all elements with X and Y class.  We could swap one of these classes out for an ID if we wanted to.<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">const elements1 = document.querySelectorAll('.boxes')\nconst elements2 = document.querySelectorAll('.borders')<\/code><\/pre>\n\n\n\n<p>The variable elements1 and 2 are both arrays of all the elements that have class .boxes and boarders.<\/p>\n\n\n\n<p>Now lets create a function to detect a collision.<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">function detectCollision(class1, class2) {\n  for (let i = 0; i &lt; class1.length; i++) {\n    const e1Rectangle = class1[i].getBoundingClientRect()\n    for (let i2 = 0; i2 &lt; e2.length; i2++) {\n      const e2Rectangle = class2[i2].getBoundingClientRect()\n      if (\n        e1Rectangle.left &lt; e2Rectangle.right &amp;&amp;\n        e1Rectangle.right > e2Rectangle.left &amp;&amp;\n        e1Rectangle.top &lt; e2Rectangle.bottom &amp;&amp;\n        e1Rectangle.bottom > e2Rectangle.top\n      ) {\n        destroyElement(class1[i])\n        destroyElement(class1[i2])\n      }\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p>This function takes two arrays as inputs, loops over each array object and compares to see if any are overlapping.  If they are overlapping, run the destroyElement() function for those two specific elements.<\/p>\n\n\n\n<p>Create the destroyElement function with<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\"> function destroyElement(element) {\n  elementId.parentNode.removeChild(elementId)\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Here is our full code<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">const elements1 = document.querySelectorAll('.boxes')\nconst elements2 = document.querySelectorAll('.borders')\n\nfunction detectCollision (class1, class2) {\n  for (let i = 0; i &lt; class1.length; i++) {\n    const e1Rectangle = class1[i].getBoundingClientRect()\n    for (let i2 = 0; i2 &lt; e2.length; i2++) {\n      const e2Rectangle = class2[i2].getBoundingClientRect()\n      if (\n        e1Rectangle.left &lt; e2Rectangle.right &amp;&amp;\n        e1Rectangle.right > e2Rectangle.left &amp;&amp;\n        e1Rectangle.top &lt; e2Rectangle.bottom &amp;&amp;\n        e1Rectangle.bottom > e2Rectangle.top\n      ) {\n        destroyElement(class1[i])\n        destroyElement(class1[i2])\n      }\n    }\n  }\n}\n\n\/\/ Element is element[arraynumber].etc\nfunction destroyElement(element) {\n  elementId.parentNode.removeChild(elementId)\n}\n\n\/\/ Every second, let's run our collision function to check for collisions\nsetInterval(function () {\n   detectCollision(elements1, elements2)\n  }, 1000)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Imagine we have a page that has anywhere from 1 to 100 floating blocks. Now what if I want the block(s) to disappear if it runs into another type of block or a boarder? How would we do that? First &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/javascript-delete-object-if-it-collides-or-overlaps-with-another-object-that-has-css-class-x\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[792],"tags":[1502,1501,1500,1437,1463],"class_list":["post-5162","post","type-post","status-publish","format-standard","hentry","category-programming","tag-class","tag-classes","tag-function","tag-javascript","tag-js"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5162"}],"version-history":[{"count":3,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5162\/revisions"}],"predecessor-version":[{"id":5166,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5162\/revisions\/5166"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}