Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
FabricJS – How to check if a Polygon Object Intersects with Another Object?
We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. 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 a Polygon object intersects with another object, we use the intersectsWithObject method. This method checks whether the object that is passed to it, intersects with the polygon object.
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 Boolean 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.
Example 1: Using intersectsWithObject Method
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 polygon object intersects with another object. Here, we have initialized two rectangle objects namely rectangleRed and rectangleBlue. Since our polygon object intersects with rectangleRed, 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>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating a polygon object
var polygon = new fabric.Polygon(
[
{ x: -20, y: -35 },
{ x: 20, y: -35 },
{ x: 40, y: 0 },
{ x: 20, y: 35 },
{ x: -20, y: 35 },
{ x: -40, y: 0 },
],
{
stroke: "red",
left: 100,
top: 10,
fill: "black",
strokeWidth: 2,
strokeLineJoin: "bevil",
}
);
// 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: 200,
fill: "blue",
});
// Add them to the canvas
canvas.add(polygon);
canvas.add(rectangleRed);
canvas.add(rectangleBlue);
// Using intersectsWithObject method
console.log(
"Does the polygon object intersect with rectangleRed?: ",
polygon.intersectsWithObject(rectangleRed)
);
console.log(
"Does the polygon object intersect with rectangleBlue?: ",
polygon.intersectsWithObject(rectangleBlue)
);
</script>
</body>
</html>
Does the polygon object intersect with rectangleRed?: true Does the polygon object intersect with rectangleBlue?: false
Example 2: Using intersectsWithObject Method with Different Objects
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>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating a polygon object
var polygon = new fabric.Polygon(
[
{ x: -20, y: -35 },
{ x: 20, y: -35 },
{ x: 40, y: 0 },
{ x: 20, y: 35 },
{ x: -20, y: 35 },
{ x: -40, y: 0 },
],
{
stroke: "red",
left: 100,
top: 10,
fill: "black",
strokeWidth: 2,
strokeLineJoin: "bevil",
}
);
// 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: 200,
fill: "blue",
});
// Add them to the canvas
canvas.add(polygon);
canvas.add(triangle);
canvas.add(circle);
// Using intersectsWithObject method
console.log(
"Does the polygon object intersect with triangle?: ",
polygon.intersectsWithObject(triangle)
);
console.log(
"Does the polygon object intersect with circle?: ",
polygon.intersectsWithObject(circle)
);
</script>
</body>
</html>
Does the polygon object intersect with triangle?: true Does the polygon object intersect with circle?: false
Key Points
- The intersectsWithObject method returns a Boolean value (true/false)
- It works with any FabricJS object type (Rectangle, Circle, Triangle, etc.)
- The method considers the actual position and dimensions of objects on the canvas
- Use the optional absolute parameter to ignore viewport transformations
Conclusion
The intersectsWithObject method provides an efficient way to detect collision between a polygon and any other FabricJS object. This is particularly useful for interactive canvas applications and games.
