Skip to content

Instantly share code, notes, and snippets.

@Sephi-Chan
Created April 3, 2020 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sephi-Chan/cae79fdebb1be35d357a93314ea89275 to your computer and use it in GitHub Desktop.
Save Sephi-Chan/cae79fdebb1be35d357a93314ea89275 to your computer and use it in GitHub Desktop.
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