1

I have a Black box out of which I want to take away a specific path.

Code

@Composable
fun PathDiff() {
    Canvas(
        modifier = Modifier
            .fillMaxSize(),
    ) {
        val rect1 = Path().apply {
            addRect(Rect(Offset.Zero, Offset(400.0F, 400.0F)))
        }
        val rect2 = Path().apply {
            moveTo(100.0F, 100.0F)
            lineTo(200.0F, 100.0F)
            lineTo(200.0F, 200.0F)
        }
        val newPath = Path.combine(
            operation = PathOperation.Difference,
            path1 = rect1,
            path2 = rect2,
        )
        drawPath(
            path = newPath,
            color = Black,
            alpha = 1F,
        )
    }
}

Current result

enter image description here

I want it to be a vertical and a horizontal line with given stroke width and rounded cap, not as a triangle.

How do I achieve that?

Note: I have not used close() but it gets added automatically.

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
  • I also tried available path operations but it clips to a triangle instead of 2 lines. If Path operations is not a requirement you can use BlendMode and draw 2 paths. – Thracian Aug 19 '22 at 04:36
  • Unfortunately, path operations are the main requirement I a trying to do. The above is a simplified example. The actual use case is much more complex. – Abhimanyu Aug 19 '22 at 04:48
  • 1
    Also, I found this in docs in the source code. (`drawPath` comments) "If the path is * filled, then subpaths within it are implicitly closed (see [Path.close])." – Abhimanyu Aug 19 '22 at 04:49
  • I tried with `Stroke(width=2.dp.toPx(), cap =StrokeCap.Round)`, it's still clipped as triangle. If you draw lines they are drawn as 2 lines but when you clip with a Rectangle lines are clipped to a triangle – Thracian Aug 19 '22 at 04:50

0 Answers0