The Geometry APIs allow you to create interactive tools such as erasers and selection mechanisms.
To illustrate practical application of the Geometry APIs, explore the following eraser implementation example.
Whole stroke eraser
fun eraseWholeStrokes(
eraserBox: ImmutableBox,
finishedStrokesState: MutableState<Set<Stroke>>,
) {
val threshold = 0.1f
val strokesToErase = finishedStrokesState.value.filter { stroke ->
stroke.shape.computeCoverageIsGreaterThan(
box = eraserBox,
coverageThreshold = threshold,
)
}
if (strokesToErase.isNotEmpty()) {
Snapshot.withMutableSnapshot {
finishedStrokesState.value -= strokesToErase
}
}
}
For a Compose implementation, make sure to trigger a recomposition, so the
strokes are effectively removed. For example, an approach would be to use
rememberCoroutineScope
in your composable and pass the coroutine scope to your
touch listener, allowing you to modify finishedStrokesState
within the scope
of Compose.