FabricJS – 如何檢查多邊形物件是否與另一個物件相交?
我們可以透過建立fabric.Polygon的例項來建立一個多邊形物件。多邊形物件可以用任何由一組連線的直線段組成的封閉形狀來表示。由於它是 FabricJS 的基本元素之一,我們也可以透過應用角度、不透明度等屬性輕鬆對其進行自定義。
為了檢查多邊形物件是否與另一個物件相交,我們使用intersectsWithObject方法。此方法檢查傳遞給它的物件是否與多邊形物件相交。
語法
intersectsWithObject(other: Object, absolute: Boolean, calculate: Boolean ): Boolean
引數
other − 此引數接受一個物件,該物件指定我們要測試的物件。
absolute(可選) − 此引數接受一個字串值,該值指定是否使用不帶viewportTransform的座標。此引數是可選的。
Calculate(可選) − 此引數接受一個布林值,該值指定是否使用當前位置的座標。此引數是可選的。
示例 1:使用 intersectsWithObject 方法
讓我們來看一個程式碼示例,看看使用 intersectsWithObject 方法時的日誌輸出。intersectsWithObject 方法在檢查多邊形物件是否與另一個物件相交時返回 true 或 false。在這裡,我們初始化了兩個矩形物件,即 rectangleRed 和 rectangleBlue。由於我們的多邊形物件與 rectangleRed 相交,因此返回 true 值。
<!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 檢查多邊形物件是否與另一個物件相交。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP