Nino
08/13/2025, 10:44 AMmoveTo
? Or a Path that shouldn't draw for some discrete positions?
I'm trying to represent lines from a line chart as a Path
which can support missing data between points, and I wish to animate them when displaying them for the first time.
[1] is the result I'm trying to achieve (I'm "cheating" with extra rects to hide the middle line that shouldn't appear)
[2] is what's currently working with only 1 moveTo
(code in 🧵 thread)
[3] is the result with multiple moveTo
How could I approach this problem in a Canvas? I would have to display multiple lines with different animations so I'd like to have a performant approach, but I'm stuck with this "bug" (more a "lack of feature" because we don't always want a "linear jump" between 2 segments of a Path) with the PathMeasure
.
Thank you!Nino
08/13/2025, 10:44 AMval pathMeasure = remember { PathMeasure() }
val strokeProgress = remember { Animatable(0F) }
LaunchedEffect(Unit) {
strokeProgress.animateTo(1F, tween(2_000))
}
Canvas(modifier = Modifier.fillMaxSize()) {
val path = Path().apply {
moveTo(0F, size.height / 2)
lineTo(size.width / 3, 0F)
lineTo(size.width * 2 / 3, size.height / 2) // <-- Using moveTo here breaks the animation!
lineTo(size.width, size.height / 2)
}
pathMeasure.setPath(path, forceClosed = false)
val animatedPath = Path()
pathMeasure.getSegment(
startDistance = 0F,
stopDistance = pathMeasure.length * strokeProgress.value,
destination = animatedPath,
)
drawPath(
path = animatedPath,
color = Color.Magenta,
style = Stroke(2.sp.toPx()),
)
}
romainguy
08/13/2025, 3:09 PMromainguy
08/13/2025, 3:10 PMNino
08/14/2025, 7:46 AMdrawLine
for each segment directly, animating it was a bit of a challenge 😄