I have a Canvas composable I created that allows me to drag and scale a child composable within the boundaries of the parent (the Canvas).
I need to know what is the position of the child composable relative to its parent (the Canvas) at any given time while being dragged or scaled.
GridCanvas(
ratio = canvasRatio,
) { gridWidth, gridHeight ->
var scale by remember { mutableStateOf(1f) }
var offsetX by remember { mutableStateOf(0f) }
var offsetY by remember { mutableStateOf(0f) }
RenderedImage(
modifier = Modifier
.pointerInput(Unit) {
detectTransformGestures { _, pan, zoom, _ ->
val futureScale = scale * zoom
if (futureScale in MIN_SCALE_FACTOR..MAX_SCALE_FACTOR) {
scale = futureScale
}
offsetX += pan.x
offsetY += pan.y
}
}
.graphicsLayer {
this.scaleX = scale
this.scaleY = scale
this.translationX = offsetX
this.translationY = offsetY
},
imageUri = someUri,
)
}
How to get the X and Y position of the child RenderedImage relative to its parent?