How to check if an Image object intersects with another object using FabricJS?


In this tutorial, we are going to learn how to check if an Image object intersects with another object using FabricJS. We can create an Image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to check if an Image object intersects with another object, we use the intersectsWithObject method.

Syntax

intersectsWithObject(other: Object, absolute: Boolean, calculate:Boolean ): Boolean

Parameters

  • other − This parameter accepts an Object which specifies the object we want to test.

  • absolute(optional) − This parameter accepts a String value which specifies whether to use coordinates without viewportTransform or not. This parameter is optional.

  • calculate(optional) − This parameter accepts a Boolean value which specifies whether to use coordinates of current position. This parameter is optional.

Using intersectsWithObject method

Example

Let’s see a code example to see the logged output when the intersectsWithObject method is used. The intersectsWithObject method returns true or false on checking if the image object intersects with another object. Here, we have initialized two rectangle objects namely rectangle1 and rectangle2. Since our image object intersects with rectangle1, a true value is returned.

<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>Using intersectsWithObject method</h2> <p>You can open console from dev tools and see the logged output</p> <canvas id="canvas"></canvas> <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" /> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiating the image element var imageElement = document.getElementById("img1"); // Initiate an Image object var image = new fabric.Image(imageElement, { top: 50, left: 110, }); // Initiate a rectangle object var rectangleRed = new fabric.Rect({ width: 60, height: 20, top: 40, left: 80, fill: "red", strokeWidth: 6, }); // Initiate another rectangle object var rectangleBlue = new fabric.Rect({ width: 20, height: 40, top: 70, left: 520, fill: "blue", }); // Add them to the canvas canvas.add(image); canvas.add(rectangleRed); canvas.add(rectangleBlue); // Using intersectsWithObject method console.log( "Does the image object intersect with rectangleRed?: ", image.intersectsWithObject(rectangleRed) ); console.log( "Does the image object intersect with rectangleBlue?: ", image.intersectsWithObject(rectangleBlue) ); </script> </body> </html>

Using intersectsWithObject method with different objects

Example

In this example, we have used the intersectsWithObject method along with different objects to prove that this method can work with any object.

<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>Using intersectsWithObject method with different objects</h2> <p>You can open console from dev tools and see the logged output</p> <canvas id="canvas"></canvas> <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" /> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiating the image element var imageElement = document.getElementById("img1"); // Initiate an Image object var image = new fabric.Image(imageElement, { top: 50, left: 110, }); // Initiate a triangle object var triangle = new fabric.Triangle({ width: 90, height: 70, top: 40, left: 80, fill: "red", strokeWidth: 6, }); // Initiate a circle object var circle = new fabric.Circle({ radius: 40, top: 70, left: 520, fill: "blue", }); // Add them to the canvas canvas.add(image); canvas.add(triangle); canvas.add(circle); // Using intersectsWithObject method console.log( "Does the image object intersect with triangle?: ", image.intersectsWithObject(triangle) ); console.log( "Does the image object intersect with circle?: ", image.intersectsWithObject(circle) ); </script> </body> </html>

Conclusion

In this tutorial, we used two examples to demonstrate how you can check if an Image object intersects with another object using FabricJS.

Updated on: 26-Oct-2022

597 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements