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

Rafs

11/20/2020, 12:17 PM
How can I draw a text on a compose canvas? I don't see any
drawText
method in the
DrawScope
n

Nader Jawad

11/20/2020, 4:07 PM
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

Adam Powell

01/05/2021, 3:42 PM
How would you want such an overload to behave?
d

Dominaezzz

01/05/2021, 3:44 PM
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

Zach Klippenstein (he/him) [MOD]

01/05/2021, 5:19 PM
That sounds like the
drawWithContent
modifier (or whatever replaced it since it’s deprecated).
d

Dominaezzz

01/05/2021, 6:57 PM
Oh? Like
Copy code
Canvas {
    drawWithContent { .... }
}
Ohhh,
Modifier
lol, I misread.
z

Zach Klippenstein (he/him) [MOD]

01/05/2021, 8:07 PM
Copy code
Text(Modifier.drawWithContent {
  withRotate(…) {
    withScale(…) {
      // Actually draws the modified composable.
      drawContent()
    }
  }
})
d

Dominaezzz

01/05/2021, 8:52 PM
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

Zach Klippenstein (he/him) [MOD]

01/05/2021, 9:57 PM
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

Adam Powell

01/06/2021, 12:48 AM
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

Zach Klippenstein (he/him) [MOD]

01/06/2021, 2:02 AM
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
2 Views