FabricJS – 检查多边形对象是否完全包含在另一个对象内?
我们可以通过创建 fabric.Polygon 实例来创建多边形对象。多边形对象的特征可以是任何由一组连接的直线段组成的封闭形状。由于它是 FabricJS 的基本元素之一,我们还可以通过应用角度、不透明度等属性轻松地对其进行自定义。
我们使用 isContainedWithinObject 来查找多边形对象是否完全包含在另一个对象的区域内。它可用于测试对象是否完全包含在另一个对象的区域内。
语法
isContainedWithinObject(other: Object, absolute: Boolean, calculate: Boolean ): Boolean
参数
other − 此参数接受一个 Object,该 Object 指定我们要测试的对象。
absolute (可选) − 此参数接受一个字符串值,该字符串值指定是否使用不带 viewportTransform 的坐标。此参数是可选的。
calculate (可选) − 此参数接受一个布尔值,该布尔值指定是否使用当前位置的坐标。此参数是可选的。
示例 1:使用 isContainedWithinObject 方法
让我们看一个代码示例,以查看使用 isContainedWithinObject 方法时记录的输出。isContainedWithinObject 方法在检查多边形对象是否完全包含在另一个对象的区域内时返回 true 或 false。
<!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 isContainedWithinObject method</h2>
<p> You can open console from dev tools and see that the logged output contains a true value </p>
<canvas id="canvas"></canvas>
<script>
// 启动一个canvas实例
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// 初始化多边形对象
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: 50,
fill: "black",
strokeWidth: 2,
strokeLineJoin: "bevil",
}
);
// 初始化一个矩形对象
var rectangle = new fabric.Rect({
width: 390,
height: 130,
top: 40,
left: 80,
fill: "transparent",
stroke: "red",
strokeWidth: 6,
});
// 将它们添加到画布
canvas.add(polygon);
canvas.add(rectangle);
// 使用 isContainedWithinObject 方法
console.log(
"Is the Polygon object contained within the area of the rectangle object?: ",
polygon.isContainedWithinObject(rectangle)
);
</script>
</body>
</html>
示例 2:对多个对象使用 isContainedWithinObject 方法
在此示例中,我们分别对两个矩形对象 rect1 和 rect2 使用了 isContainedWithinObject 方法。由于多边形对象不包含在 rect2 的区域内,因此控制台中返回 false 值。
<!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 isContainedWithinObject method with multiple objects</h2>
<p>You can open console from dev tools and see that the logged output contains a false value </p>
<canvas id="canvas"></canvas>
<script>
// 启动一个canvas实例
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// 初始化多边形对象
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: 50,
fill: "black",
strokeWidth: 2,
strokeLineJoin: "bevil",
}
);
// 初始化一个矩形对象
var rect1 = new fabric.Rect({
width: 390,
height: 130,
top: 40,
left: 80,
fill: "transparent",
stroke: "red",
strokeWidth: 6,
});
// 初始化另一个矩形对象
var rect2 = new fabric.Rect({
width: 120,
height: 40,
top: 170,
left: 320,
fill: "blue",
});
// 将它们添加到画布
canvas.add(rect1);
canvas.add(rect2);
canvas.add(polygon);
// Using isContainedWithinObject method
console.log(
"Is the Polygon object contained within the area of the rectangle (rect2) object?: ",
polygon.isContainedWithinObject(rect2)
);
</script>
</body>
</html>
结论
在本教程中,我们使用了两个简单示例来演示如何使用 FabricJS 检查多边形对象是否完全包含在另一个对象的区域内。
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

