FabricJS – 如何检查一个多边形对象是否与另一个对象相交?
我们可以通过创建一个fabric.Polygon的实例来创建一个Polygon对象。一个多边形对象可以被描述为由一组连接的直线段组成的任何封闭形状。由于它是FabricJS的基本元素之一,我们也可以通过应用角度、不透明度等属性轻松地定制它。
为了检查一个多边形对象是否与另一个对象相交,我们使用intersectsWithObject方法。该方法检查传递给它的对象是否与多边形对象相交。
语法
intersectsWithObject(other: Object, absolute: Boolean, calculate: Boolean ): Boolean
参数
- other – 这个参数接受一个对象,指定我们要测试的对象。
-
absolute (可选) – 这个参数接受一个字符串值,指定是否使用没有viewportTransform的坐标。这个参数是可选的。
-
Calculate (可选) – 该参数接受一个布尔值,指定是否使用当前位置的坐标。这个参数是可选的。
例1:使用intersectsWithObject方法
让我们看一个代码例子,看看使用intersectsWithObject方法时的记录输出。intersectsWithObject方法在检查多边形对象是否与另一个对象相交时返回真或假。这里,我们已经初始化了两个矩形对象,即矩形红和矩形蓝。由于我们的多边形对象与矩形红相交,所以返回真值。
<!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>
例2:对不同的对象使用intersectsWithObject方法
在这个例子中,我们将intersectsWithObject方法与不同的对象一起使用,以证明这个方法可以对任何对象起作用。
<!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>
结论
在本教程中,我们用两个例子来演示如何使用FabricJS检查一个Polygon对象是否与另一个对象相交。