How to draw rounded corner polygons in Jetpack Compose Canvas?
Asked Answered
D

2

11

I'm trying to create a rounded triangle using Canvas in Jetpack Compose.

I try this code for drawing triangle:

@Composable
fun RoundedTriangle() {
    Canvas(modifier = Modifier.size(500.dp)) {
        val trianglePath = Path().apply {
            val height = size.height
            val width = size.width
            moveTo(width / 2.0f, 0f)
            lineTo(width, height)
            lineTo(0f, height)
        }
            drawPath(trianglePath, color = Color.Blue)
    }
}

But I don't know how to round the triangle corners. I also tried to use arcTo, but I was unable to get a suitable result.

How can I draw something like the figure below?

enter image description here

Disfigure answered 28/10, 2021 at 5:59 Comment(0)
H
16

For Stroke you can specify rounding like this:

drawPath(
    ...
    style = Stroke(
        width = 2.dp.toPx(),
        pathEffect = PathEffect.cornerPathEffect(4.dp.toPx())
    )
)

Yet Fill seems lack of support rounding. I've created a feature request, please star it.

But Canvas has drawOutline function, which accepts both Outline, which can wrap a Path, and Paint, for which you can specify pathEffect:

Canvas(modifier = Modifier.fillMaxWidth().aspectRatio(1f)) {
    val rect = Rect(Offset.Zero, size)
    val trianglePath = Path().apply {
        moveTo(rect.topCenter)
        lineTo(rect.bottomRight)
        lineTo(rect.bottomLeft)
        close()
    }

    drawIntoCanvas { canvas ->
        canvas.drawOutline(
            outline = Outline.Generic(trianglePath),
            paint = Paint().apply {
                color = Color.Black
                pathEffect = PathEffect.cornerPathEffect(rect.maxDimension / 3)
            }
        )
    }
}

Path helpers:

fun Path.moveTo(offset: Offset) = moveTo(offset.x, offset.y)
fun Path.lineTo(offset: Offset) = lineTo(offset.x, offset.y)

Result:

Hardwick answered 28/10, 2021 at 10:15 Comment(8)
do you know how can this be applied to a square?Fumikofumitory
@Fumikofumitory just add one more point to the path, for example like thisHardwick
Thanks for the answer! However that will render a square with a 45° rotation since you're anchoring the edge centers. I could achieve the correct result using the edges but also closing the path.Fumikofumitory
@Fumikofumitory if you need a plain square, not rotated, you simply can use DrawScope.drawRoundRectHardwick
true but that will not give you the effect of super circle or smoothed rounded edges.Fumikofumitory
@Fumikofumitory agree that it looks cleaner with close()Hardwick
Is there a way to draw a single rounded corner? Or should I open a different question for that?Ferrand
@RaymondArteaga Yes, it's a different question. I guess you can draw it by hand, e.g. create Path with addArc, take this answer as a referenceHardwick
F
1

Based on @philip-dukhov answer, if anyone is interested in appliying this to a square

@Composable
fun SquirclePath(
    modifier: Modifier,
    smoothingFactor: Int = 60,
    color: Color,
    strokeWidth: Float,
) {
    Canvas(
        modifier = modifier
    ) {
        val rect = Rect(Offset.Zero, size)
        val percent = smoothingFactor.percentOf(rect.minDimension)
        val squirclePath = Path().apply {
            with(rect) {
                lineTo(topRight)
                lineTo(bottomRight)
                lineTo(bottomLeft)
                lineTo(topLeft)
                // this is where the path is finally linked together
                close()
            }
        }

        drawIntoCanvas { canvas ->
            canvas.drawOutline(
                outline = Outline.Generic(squirclePath),
                paint = Paint().apply {
                    this.color = color
                    this.style = PaintingStyle.Fill
                    this.strokeWidth = strokeWidth
                    pathEffect = PathEffect.cornerPathEffect(percent)
                }
            )
        }
    }
}

fun Int.percentOf(target:Float) = (this.toFloat() / 100) * target
Fumikofumitory answered 3/2, 2022 at 2:57 Comment(1)
@philip-dukhov I couldn't put the entire code into a comment so i posted it here.Fumikofumitory

© 2022 - 2025 — McMap. All rights reserved.