https://kotlinlang.org logo
#compose
Title
# compose
b

Billy Newman

08/01/2022, 6:35 PM
Hello all, looking for some help drawing text along an arc drawn in Canvas. Code in replies.
Drawing and arc on a canvas
Copy code
Canvas(modifier = Modifier.fillMaxSize()) {
   drawArc(
      Color.Green,
      30f,
      90f,
      topLeft = Offset(0f, 0f),
      useCenter = true,
      size = Size(sizeInPx, sizeInPx)
   )
}
I would like to draw text along the inside of that arc. Looks like I can use drawIntoCanvas and get ahold of the native canvas:
Copy code
Canvas(modifier = Modifier.fillMaxSize()) {
   drawIntoCanvas {
      it.nativeCanvas.drawText(
         sector.text,
         0f,
         0f,
         Paint().apply {
            setColor(android.graphics.Color.BLACK)
            textSize = textSizeInPx
         }
      )
   }
}
However that is not drawing along the arc path. From some searching looks like maybe there used to be a compose drawText method, but I don’t see that anymore. Looks like I might be able to use nativeCanvas.drawTextOnPath(), which requires another calculation of the arc. Am I going down the correct path?
s

Scott Peterson

08/01/2022, 6:59 PM
pretty sure there is a
drawText
method. We just used one inside a
Canvas()
we used it inside of a
drawContext.canvas.nativeCanvas.apply { }
b

Billy Newman

08/01/2022, 7:01 PM
Right so there is a drawText on nativeCanvas, but not on the compose Canvas?
s

Scott Peterson

08/01/2022, 7:05 PM
I think you’re right.
t

Tin Tran

08/02/2022, 2:00 AM
drawText
in compose canvas has been added to compose in 1.3.0-alpha02 you can give it a try
4 Views