computeAlignment method

Mat2D computeAlignment([
  1. Offset offset = Offset.zero
])
inherited

Implementation

Mat2D computeAlignment([Offset offset = Offset.zero]) {
  AABB frame = AABB.fromValues(
      offset.dx, offset.dy, offset.dx + size.width, offset.dy + size.height);
  AABB content = aabb;

  double contentWidth = content.width;
  double contentHeight = content.height;

  if (contentWidth == 0 || contentHeight == 0) {
    return Mat2D();
  }

  double x = -1 * content.left -
      contentWidth / 2.0 -
      (_alignment.x * contentWidth / 2.0);
  double y = -1 * content.top -
      contentHeight / 2.0 -
      (_alignment.y * contentHeight / 2.0);

  double scaleX = 1.0, scaleY = 1.0;

  switch (_fit) {
    case BoxFit.fill:
      scaleX = frame.width / contentWidth;
      scaleY = frame.height / contentHeight;
      break;
    case BoxFit.contain:
      double minScale =
          min(frame.width / contentWidth, frame.height / contentHeight);
      scaleX = scaleY = minScale;
      break;
    case BoxFit.cover:
      double maxScale =
          max(frame.width / contentWidth, frame.height / contentHeight);
      scaleX = scaleY = maxScale;
      break;
    case BoxFit.fitHeight:
      double minScale = frame.height / contentHeight;
      scaleX = scaleY = minScale;
      break;
    case BoxFit.fitWidth:
      double minScale = frame.width / contentWidth;
      scaleX = scaleY = minScale;
      break;
    case BoxFit.none:
      scaleX = scaleY = 1.0;
      break;
    case BoxFit.scaleDown:
      double minScale =
          min(frame.width / contentWidth, frame.height / contentHeight);
      scaleX = scaleY = minScale < 1.0 ? minScale : 1.0;
      break;
  }

  Mat2D transform = Mat2D();

  transform[4] = frame.width / 2.0 + (_alignment.x * frame.width / 2.0);
  transform[5] = frame.height / 2.0 + (_alignment.y * frame.height / 2.0);
  if (offsetViewTransform) {
    transform[4] += offset.dx;
    transform[5] += offset.dy;
  }
  Mat2D.scale(transform, transform, Vec2D.fromValues(scaleX, scaleY));
  Mat2D center = Mat2D();
  center[4] = x;
  center[5] = y;
  Mat2D.multiply(transform, transform, center);
  return transform;
}