# compose


02/05/2023, 12:38 AM
I have a lot of intensive trigonometry operations to draw my Canvas. I'm trying to figure out if it's necessary to move such intensive work outside of the draw call, especially since the draw call runs alot during my fade animations. The problem with moving my shape and position calculations outside of the canvas is that I'm unable to get the size of the canvas before the draw call, and the size is necessary for me to know how to draw the shapes. I've been somewhat able to solve that problem by doing a
Copy code
var outerRadius by remember {
before the code that calculates the shapes, and then setting
Copy code
outerRadius = min(size.width, size.height) / 2
at the start of the draw call. This works in app but the previews in Android will show the canvas as empty, which is annoying. The shape calculation happens before the canvas like this
Copy code
val shapes by remember(indicatorCount, gapAng, outerRadius) {
  // trigonometry etc


02/05/2023, 12:47 AM
You can use drawWithCache to do this.
The lambda will be invoked only once per recomposition while the onDraw lambda inside drawWitchCache will be invoked on every draw
It's designed specifically for cases likesl yours
Also trigonometry isn't that intensive (unless you make thousands of calls per draw). Creating objects though (if you do) is best done with drawWithCache


02/05/2023, 12:56 AM
Thanks a lot, that's very helpful. I will look into using drawWithCache then.