Skip to content

Instantly share code, notes, and snippets.

@blaugold
Last active July 2, 2020 13:29
Show Gist options
  • Save blaugold/7e8291eab96908402f41a87d75e48d3b to your computer and use it in GitHub Desktop.
Save blaugold/7e8291eab96908402f41a87d75e48d3b to your computer and use it in GitHub Desktop.
Matrix4 getAnchorToNavigatorTransform(RenderObject anchor, RenderObject navigator) {
// These two transforms project points from the local coordinate spaces of their
// respective RenderObjects to the global coordinates space.
Matrix4 anchorTransform = anchor.getTransformTo(null);
Matrix4 navigatorTransform = navigator.getTransformTo(null);
// This transform projects points from the local coordinates space of [anchor]
// to the global coordinate space and from there to the local coordinate space
// of [navigator] in one step.
return anchorTransform..multiply(Matrix4.tryInvert(navigatorTransform));
}
// This method assumes that [anchor] is a [RenderBox], and returns a [Rect] which
// describes the dimensions of [anchor] in [navigator].
// [RenderBox] is the most common sub class of [RenderObject], where every instance
// is laid out as a box and has a [Size].
Rect getAnchorRectInNavigator(RenderObject anchor, RenderObject navigator) {
Matrix4 transform = getAnchorToNavigatorTransform(anchor, navigator);
Rect anchorRect = Offset.zero & (anchor as RenderBox).size;
return MatrixUtils.transformRect(transform, anchorRect);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment