viewportToSource method

Offset viewportToSource(
  1. Offset viewport, {
  2. required Matrix4 camera,
})

Inverse of the full viewport-rendering transform — viewport = camera · sandwich(imageTransform) · source. Used by composed-scene gesture handlers (e.g. DrawGestures) that need to convert pointer-in-viewport coordinates back to source-space so strokes recorded in a crop-overlayed editor still land in the right place on the original source.

Implementation

Offset viewportToSource(Offset viewport, {required math.Matrix4 camera}) {
  final scale = camera.storage[0];
  final tx = camera.storage[12];
  final ty = camera.storage[13];
  if (scale.abs() < 1e-9) return viewport;
  final imagePoint = Offset(
    (viewport.dx - tx) / scale,
    (viewport.dy - ty) / scale,
  );

  final cx = imageSize.width / 2;
  final cy = imageSize.height / 2;
  final inv = math.Matrix4.copy(imageTransform.matrix)..invert();
  final v = math.Vector3(imagePoint.dx - cx, imagePoint.dy - cy, 0)
    ..applyMatrix4(inv);

  return Offset(v.x + cx, v.y + cy);
}