I wanted to move some code out of my Composable, a...
# compose
n
I wanted to move some code out of my Composable, as a function, to make my code generally clearer, but I have trouble referencing the drawLine method of my CanvasDrawScope as a parameter. I thought it would be a Unit, but no success. I am putting the snippet in the thread. Any ideas or is it best to keep all that code within the Composable?
Copy code
fun drawDynamicGrid(
    listOfWords: List<String>,
    bitmapWidth: Float,
    bitMapHeight: Float,
drawLine: // what to put here as Type?
) {
    // Dynamic Grid
    val spacingX = 150f
    val spacingY = 70f

    var anchorX = spacingX
    var anchorY = spacingY

    while (anchorX < getWordCount(listOfWords)) {
        drawLine(
            strokeWidth = 2F, color = Color.White, start = Offset(anchorX, 0f),
            end = Offset(anchorX, bitMapHeight)
        )
        anchorX += spacingX
    }

    while (anchorY < 2) {
        drawLine(
            strokeWidth = 2F, color = Color.White, start = Offset(0f, anchorY),
            end = Offset(bitmapWidth, anchorY)
        )
        anchorY += spacingY
    }
}
l
Maybe could you try an extension function of DrawScope ?
Copy code
fun DrawScope.drawline(...) {
}
☝️ 1
Then inside the
drawDynamicGrid
you can call
drawline
.
n
Sounds good, many thanks!
👌 1
@loloof64 I am struggling with this. The drawLine remains red. Would you please be able to have a look?
l
Hi ! Are you calling the code from inside a
Canvas
or a call to
Modifier.drawBehind
? If so, we may find a workaround.
Then all you have to do is to simply replace
DrawScope.drawline(
by just
drawline
, in both calls to
drawline
.
n
Hi, well I am using this:
Copy code
CanvasDrawScope().draw(
            Density(1.0f), LayoutDirection.Ltr, canvas,
            Size(bitmapWidth, bitMapHeight),
        ) {
l
Are you calling the code above from inside
CanvasDrawScope
? If so, you can simply call
drawLine
each time (without the `DrawScope.`prefix)
n
I was calling it from a button
not inside
CanvasDrawScope
l
Ah. Then what I said cannot work.
n
ah ok, in that case I'll try things differently.
Thanks for your help!
l
No problem. Sorry for having pointed you in the wrong direction.
n
No, you probably didn't! 🙂
🙂 1