FabricJS – 检查多边形对象是否完全包含在另一个对象内?
我们可以通过创建fabric.Polygon的实例来创建一个多边形对象。多边形对象可以用任何由一组连接的直线段组成的封闭形状来表示。由于它是FabricJS的基本元素之一,我们也可以通过应用角度、不透明度等属性轻松自定义它。
我们使用isContainedWithinObject来查找多边形对象是否完全包含在另一个对象的区域内。它可以用来测试一个对象是否完全包含在另一个对象的区域内。
语法
isContainedWithinObject(other: Object, absolute: Boolean, calculate: Boolean ): Boolean
参数
other − 此参数接受一个对象,该对象指定我们要测试的对象。
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>
// 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: 50,
fill: "black",
strokeWidth: 2,
strokeLineJoin: "bevil",
}
);
// Initiate a rectangle object
var rectangle = new fabric.Rect({
width: 390,
height: 130,
top: 40,
left: 80,
fill: "transparent",
stroke: "red",
strokeWidth: 6,
});
// Add them to the canvas
canvas.add(polygon);
canvas.add(rectangle);
// Using isContainedWithinObject method
console.log(
"Is the Polygon object contained within the area of the rectangle object?: ",
polygon.isContainedWithinObject(rectangle)
);
</script>
</body>
</html>
示例2:使用isContainedWithinObject方法处理多个对象
在这个例子中,我们使用了isContainedWithinObject方法以及两个矩形对象rect1和rect2。由于多边形对象不包含在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>
// 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: 50,
fill: "black",
strokeWidth: 2,
strokeLineJoin: "bevil",
}
);
// Initiate a rectangle object
var rect1 = new fabric.Rect({
width: 390,
height: 130,
top: 40,
left: 80,
fill: "transparent",
stroke: "red",
strokeWidth: 6,
});
// Initiate another rectangle object
var rect2 = new fabric.Rect({
width: 120,
height: 40,
top: 170,
left: 320,
fill: "blue",
});
// Add them to the canvas
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检查多边形对象是否完全包含在另一个对象的区域内。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP