探测完全在另一个物体内部的物体

Detect object completely inside another object

本文关键字:内部 另一个 探测      更新时间:2023-09-26

大家好,

我想知道是否有一种方法来检测织物对象是否完全在另一个对象内?

我在文档中查看object.intersectsWithObject(object2),但不幸的是,一旦对象完全在object2内,该函数不再返回true,而是false。

其他人有这个问题吗?我一点也不知道该怎么做。我在fabric.js中搜索了函数。有人能帮忙吗?

    intersectsWithObject: function(other) {
  // extracts coords
  function getCoords(oCoords) {
    return {
      tl: new fabric.Point(oCoords.tl.x, oCoords.tl.y),
      tr: new fabric.Point(oCoords.tr.x, oCoords.tr.y),
      bl: new fabric.Point(oCoords.bl.x, oCoords.bl.y),
      br: new fabric.Point(oCoords.br.x, oCoords.br.y)
    };
  }
  var thisCoords = getCoords(this.oCoords),
      otherCoords = getCoords(other.oCoords),
      intersection = fabric.Intersection.intersectPolygonPolygon(
        [thisCoords.tl, thisCoords.tr, thisCoords.br, thisCoords.bl],
        [otherCoords.tl, otherCoords.tr, otherCoords.br, otherCoords.bl]
      );
  return intersection.status === 'Intersection';
}

谢谢你的帮助塞巴斯蒂安。

如果你想知道一个对象是否完全在另一个对象内部,你应该使用isContainedWithinObject:

isContainedWithinObject(其他)→{布尔}

§检查对象是否已满包含在另一个对象的区域内

参数:

Name: other
:

对象

Description: Object to test

来源:fabric.js,第12300行

返回:true如果对象完全包含在另一个对象

:布尔

来源:

isContainedWithinObject: function(other) {
      var boundingRect = other.getBoundingRect(),
          point1 = new fabric.Point(boundingRect.left, boundingRect.top),
          point2 = new fabric.Point(boundingRect.left + boundingRect.width, boundingRect.top + boundingRect.height);
      return this.isContainedWithinRect(point1, point2);
    }

它使用你要测试的对象的边界框。