Skip to content

Instantly share code, notes, and snippets.

@agarasul
Last active December 13, 2023 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agarasul/81f27b45fafdbdc61b0f259f927806bb to your computer and use it in GitHub Desktop.
Save agarasul/81f27b45fafdbdc61b0f259f927806bb to your computer and use it in GitHub Desktop.
MapDrawerFull
enum class DrawerMotionEvent { idle, down, up, move }
data class MapDrawState(
val currentPosition: Offset = Offset.Unspecified,
val event: MotionEvent = MotionEvent.idle
)
@Composable
fun MapDrawer() {
var state by remember { mutableStateOf(MapDrawState()) }
val brush = remember { SolidColor(Color.Black) }
val path = remember { Path() }
Canvas(
modifier = Modifier
.fillMaxSize()
.pointerInput(Unit) {
awaitEachGesture {
awaitPointerEvent().changes.first().also { changes ->
val position = changes.position
state = state.copy(
currentPosition = position,
event = DrawerMotionEvent.down
)
}
do {
val event: PointerEvent = awaitPointerEvent()
event.changes.forEach { changes ->
val position = changes.position
state = state.copy(
currentPosition = position,
event = DrawerMotionEvent.move
)
}
} while (event.changes.any { it.pressed })
// Touch Up event
state = state.copy(
currentPosition = currentEvent.changes.first().position,
event = DrawerMotionEvent.up
)
}
},
onDraw = {
when (state.event) {
DrawerMotionEvent.idle -> Unit
DrawerMotionEvent.up, DrawerMotionEvent.move -> path.lineTo(
state.currentPosition.x,
state.currentPosition.y
)
DrawerMotionEvent.down -> path.moveTo(state.currentPosition.x, state.currentPosition.y)
}
drawPath(
path = path,
brush = brush,
style = Stroke(width = 5f)
)
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment