'How to restrict drawables only shown inside canvas?

I have a canvas positioned on the right side of the screen. To the left are some buttons. If x values of some drawbles are negative, they are drawn on the left side, outside of the canvas. How can I avoid it?

Row {
    Button(onClick = {
    }) {
        Text("File")
    }
    Canvas(modifier = ...) {
        drawPoints(dataPts, PointMode.Points, Color.Blue, strokeWidth = 5f)
    }
}


Solution 1:[1]

By default, all Compose views do not clip their content outside the boundaries.

You can add Modifier.clipToBounds when you need to clip it:

Canvas(Modifier.clipToBounds())

Or you can use Modifier.clip to clip to a specific shape.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pylyp Dukhov