imageRectOnDragCropRect method

Rect imageRectOnDragCropRect({
  1. required Rect container,
  2. required Rect imageRect,
  3. required Rect cropRect,
  4. double overdragMax = 3.0,
})

When the crop rect is pinned against the image edge, drags the image along the finger by an amount weighted by the focal's distance to each crop edge. overdragMax caps per-frame catch-up.

Implementation

Rect imageRectOnDragCropRect({
  required Rect container,
  required Rect imageRect,
  required Rect cropRect,
  double overdragMax = 3.0,
}) {
  double dx = -focalPointDelta.dx;
  if ((cropRect.width - container.width).abs() > 1) {
    final leftToFocalDistance = max(0.1, focalPoint.dx - cropRect.left);
    final rightToFocalDistance = max(0.1, cropRect.right - focalPoint.dx);
    if (focalPointDelta.dx < 0 && (cropRect.left - container.left).abs() < 1) {
      dx += overdragMax - min(overdragMax, leftToFocalDistance / rightToFocalDistance);
    } else if (focalPointDelta.dx > 0 && (cropRect.right - container.right).abs() < 1) {
      dx -= overdragMax - min(overdragMax, rightToFocalDistance / leftToFocalDistance);
    } else {
      dx = 0;
    }
  }

  double dy = -focalPointDelta.dy;
  if ((cropRect.height - container.height).abs() > 1) {
    final topToFocalDistance = max(0.1, focalPoint.dy - cropRect.top);
    final bottomToFocalDistance = max(0.1, cropRect.bottom - focalPoint.dy);
    if (focalPointDelta.dy < 0 && (cropRect.top - container.top).abs() < 1) {
      dy += overdragMax - min(overdragMax, topToFocalDistance / bottomToFocalDistance);
    } else if (focalPointDelta.dy > 0 && (cropRect.bottom - container.bottom).abs() < 1) {
      dy -= overdragMax - min(overdragMax, bottomToFocalDistance / topToFocalDistance);
    } else {
      dy = 0;
    }
  }

  return imageRect
      .translate(dx, dy)
      .shiftXToFitInside(container)
      .shiftYToFitInside(container);
}