完全检测另一个物体内部的物体

Detect object completely inside another object

大家好,

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

我正在查看文档中的 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(other) → {Boolean}

§ Checks if object is fully contained within area of another object

Parameters:

Name : other
Type: Object

Description: Object to test

Source: fabric.js, line 12300

Returns: true if object is fully contained within area of another object

Type: Boolean

这是来源:

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);
    }

它使用您要测试的对象的边界框工作。