Chris Fillmore
05/19/2022, 6:33 PMcentroid
https://developer.android.com/reference/kotlin/androidx/compose/foundation/gestures/package-summary#(androidx.compose.ui.i[…]olean,kotlin.Function4)
Are the centroid coordinates relative to the display? The window? Something else?will also provide centroid of all the pointers that are down.onGesture
Kirill Grouchnikov
05/19/2022, 6:41 PMcentroid
is usedChris Fillmore
05/19/2022, 6:42 PMcentroid
into transformOrigin
centroid
coordinatesgraphicsLayer
is placed after pointerInput
, meaning the entire screen accepts input, not just the transformed area.transformOrigin
at (0,0), but rotation occurs about the centroid
var offset by remember { mutableStateOf(Offset.Zero) }
var zoom by remember { mutableStateOf(1f) }
var angle by remember { mutableStateOf(0f) }
Box(
Modifier
.graphicsLayer {
val rotatedOffset = offset.rotateBy(angle)
translationX = -rotatedOffset.x
translationY = -rotatedOffset.y
scaleX = zoom
scaleY = zoom
rotationZ = angle
transformOrigin = TransformOrigin(0f, 0f)
}
.pointerInput(Unit) {
detectTransformGestures(
onGesture = { centroid, pan, gestureZoom, gestureRotate ->
val oldScale = zoom
val newScale = zoom * gestureZoom
angle += gestureRotate
zoom = newScale
offset = (offset - centroid * oldScale).rotateBy(-gestureRotate) +
(centroid * newScale - pan * oldScale)
}
)
}
.background(Color.Yellow)
.fillMaxSize()
)
Alex Vanyo
05/19/2022, 8:34 PMChris Fillmore
05/19/2022, 8:37 PMpointerInput
after graphicsLayer
, the centroid
doesn’t update, its position remains fixed to the position it was in when touch slop first occurred. Any idea if that is expected behaviour? Or should I file a bugAlex Vanyo
05/19/2022, 8:41 PMexampleCentroid
(I only see that set, not read anywhere). Is that related?Chris Fillmore
05/19/2022, 8:41 PMKirill Grouchnikov
05/19/2022, 8:44 PMChris Fillmore
05/19/2022, 8:44 PMonGesture = { centroid, pan, gestureZoom, gestureRotate ->
Log.d("onGesture", "centroid=$centroid")
...
You can see the output:
onGesture D centroid=Offset(612.1, 1684.4)
onGesture D centroid=Offset(612.0, 1684.4)
onGesture D centroid=Offset(612.0, 1684.4)
onGesture D centroid=Offset(612.0, 1684.4)
onGesture D centroid=Offset(612.0, 1684.4)
... (goes on like this until pointers are released)
Kirill Grouchnikov
05/19/2022, 8:45 PMChris Fillmore
05/19/2022, 8:45 PMcentroid
is updating throughout gesturingAlex Vanyo
05/19/2022, 8:48 PMcentroid
is fixed in relation toChris Fillmore
05/19/2022, 8:52 PMcentroid
does update, but only very tiny incrementsAlex Vanyo
05/19/2022, 9:25 PMgraphicsLayer
transformation before consuming the the pointer events, the pointer events are being transformed as well when you’re consuming them in detectTransformGestures
.
So in your case, the centroid coordinates are in the yellow box’s coordinates space (as opposed to the screen wide coordinate space).
Now, when you’re rotating and zooming in on the yellow box, the same part of the box remains underneath the centroid of the gesture, which makes sense: that makes it natural to interact with, because you can rotate the box anchored around a given point.
So, because the same part of the box is remaining underneath the centroid, and the centroid is expressed in the box’s coordinate space, the centroid won’t be moving relative to the box (except for maybe some minor rounding errors)Chris Fillmore
05/19/2022, 9:28 PMAlex Vanyo
05/19/2022, 9:31 PMthat seems so obviousI don’t know about you, but the interactions here still hurt my brain 😆
Chris Fillmore
05/19/2022, 9:32 PMAlex Vanyo
05/19/2022, 10:16 PM