Created
April 3, 2020 18:34
-
-
Save Sephi-Chan/cae79fdebb1be35d357a93314ea89275 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const image = $('img#my-image'); | |
const images = $$('img'); | |
const imagesColliding = imagesCollidingWithImage(image, images); | |
function imagesCollidingWithImage(image, images) { | |
const rectangle = image.getBoundingClientRect(); | |
images.filter(function(otherImage){ | |
if (image == otherImage) return false; // Ignore self. | |
const otherRectangle = image.getBoundingClientRect(); | |
return areRectanglesColliding(rectangle, otherRectangle); | |
}); | |
} | |
function areRectanglesColliding(rectangle, otherRectangle) { | |
return rectangle.x < otherRectangle.x + otherRectangle.width | |
&& rectangle.x + rectangle.width > otherRectangle.x | |
&& rectangle.y < otherRectangle.y + otherRectangle.height | |
&& rectangle.height + rectangle.y > otherRectangle.y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment