How can I draw a text on a compose canvas? I don't...
# compose
r
How can I draw a text on a compose canvas? I don't see any
drawText
method in the
DrawScope
n
Drawing text is fairly involved and we find that generally speaking the drawText API is a little too simplistic while the text APIs in compose are easier to interact with than the current framework equivalent. You can either leverage the native API as Jim suggests or have a separate text composable on top
a
How would you want such an overload to behave?
d
Just a draw function that takes a composable. As supposed to an overload.
I expect the function to pass down the transformations and draw the composable in a specified bound.
I'm happily assuming that all drawing happens on a
Canvas
.
z
That sounds like the
drawWithContent
modifier (or whatever replaced it since it’s deprecated).
d
Oh? Like
Copy code
Canvas {
    drawWithContent { .... }
}
Ohhh,
Modifier
lol, I misread.
z
Copy code
Text(Modifier.drawWithContent {
  withRotate(…) {
    withScale(…) {
      // Actually draws the modified composable.
      drawContent()
    }
  }
})
d
Woah! That's pretty cool.
Although pretty strange thing to have as a
Modifer
I think. (Since you can't have multiple components in the canvas)
z
What would that api look like? As soon as you’ve got multiple composables, the canvas would probably need to implement some sort of layout system, and then you’d be basically just re-implementing compose again.
I think you could achieve something like that using
SubcomposeLayout
– layout all your non-canvas children first, place them, then subcompose your “canvas” composable underneath them all, passing their locations. Similar to how
SimpleTableLayout
works here: https://github.com/zach-klippenstein/compose-richtext/blob/main/richtext-ui/src/main/java/com/zachklipp/richtext/ui/SimpleTableLayout.kt
a
do you need subcomposition for that? Drawing happens well after layout. If you want to publish layout locations to some drawing code that consumes it you don't need subcomposition for that.
z
I was thinking if you wanted to pass the locations to a composable function as parameters. But that’s not the only way to do it for sure