How to constraint an image panning to the edges of the Box in Compose?
I'm using pointerInput(Unit) { detectTransformGestures { centroid, pan, zoom, rotation -> }} to control zooming and panning.
I'm solving this panning problem when the image is minimized to 1f with if (scale.value == 1f) 0f else panOffsetX. I want to do the same for the image zoomed-in (1f < scale <= 3f)
Box(
    modifier = Modifier
        .clip(RectangleShape)
        .fillMaxWidth()
        .background(Color.Gray)
        .pointerInput(Unit) {
            detectTransformGestures { centroid, pan, zoom, rotation ->
                val constraintZoom = when {
                    scale.value > 3f -> 3f
                    scale.value < 1f -> 1f
                    else -> (scale.value * zoom)
                }
                scale.value = constraintZoom
                panOffset += pan
                panOffsetX += pan.x
                panOffsetY += pan.y
                centroidOffset = centroid
                rotationState.value += rotation
            }
        }
) {
    Image(
        modifier = Modifier
            .align(Alignment.Center)
            .graphicsLayer(
                scaleX = maxOf(1f, minOf(3f, scale.value)),
                scaleY = maxOf(1f, minOf(3f, scale.value)),
                translationX = if (scale.value == 1f) 0f else panOffsetX,
                translationY = if (scale.value == 1f) 0f else panOffsetY,
            ),
        contentDescription = null,
        painter = painterResource(R.drawable.my_sample_image)
    )
}

                        
Trick is to limit it using your Composable size and zoom level
Full Implementation
States
Modifier