intersectionRatio property

double get intersectionRatio

A fraction in the range [0, 1] that represents what proportion of the element is visible (assuming rectangular bounding boxes).

0 means not visible; 1 means fully visible.

Implementation

double get intersectionRatio {
  final visibleArea = _area(intersectionRect.size);
  final maxVisibleArea = _area(size);

  if (_floatNear(maxVisibleArea, 0)) {
    // Avoid division-by-zero.
    return 0;
  }

  var intersectionRatio = visibleArea / maxVisibleArea;

  if (_floatNear(intersectionRatio, 0)) {
    intersectionRatio = 0;
  } else if (_floatNear(intersectionRatio, 1)) {
    // The inexact nature of floating-point arithmetic means that sometimes
    // the visible area might never equal the maximum area (or could even
    // be slightly larger than the maximum).  Snap to the maximum.
    intersectionRatio = 1;
  }

  assert(intersectionRatio >= 0);
  assert(intersectionRatio <= 1);
  return intersectionRatio;
}