ursus
01/25/2023, 1:03 AMCanvas
and "finger drawing".
I collect Offset
points via pointerInput.detectDragGestures
, seems easy enough
My issue is in order to draw that, I need a Path
, i.e. List<Offset> -> Path
mapping, i.e. a new Path
instance every draw call, which doesn't seem optimal
Obviously just calling `moveTo`/`lineTo` on drag events won't do as that won't cause recomposition
What would be the best way to make this work?
Is there some neat way to wrap Path
in something recomposition-triggering?ursus
01/25/2023, 1:03 AMval points = remember {
mutableStateListOf<Offset>()
}
Canvas(
modifier = Modifier
.fillMaxWidth()
.height(256.dp)
.pointerInput(Unit) {
detectDragGestures(
onDragStart = { offset ->
points.add(offset)
},
onDrag = { change, _ ->
points.add(change.position)
}
)
}
) {
val path = points.toPath() <-----------------
drawPath(path, color = Color.Red)
}
romainguy
01/25/2023, 1:33 AMromainguy
01/25/2023, 1:34 AMursus
01/25/2023, 1:46 AMursus
01/25/2023, 1:48 AMromainguy
01/25/2023, 2:03 AMromainguy
01/25/2023, 2:04 AMPath
but it can be very trickyromainguy
01/25/2023, 2:04 AMromainguy
01/25/2023, 2:05 AMCanvas
, create an offscreen canvas to render into a bitmap at a lower resolution, and render your path a second time in that bitmapromainguy
01/25/2023, 2:05 AMromainguy
01/25/2023, 2:05 AMursus
01/25/2023, 2:09 AMursus
01/25/2023, 2:11 AM