top of page
  • Writer's pictureAmit Bidlan

How to Draw Shapes on a Canvas with Jetpack Compose: A Beginner's Guide

UI toolkit for developing Android applications using declarative syntax is called Jetpack Compose. Drawing shapes on a canvas is one of the capabilities of Jetpack Compose. In this guide, we'll go through the fundamentals of utilizing Jetpack Compose to draw shapes on a canvas.




Let's begin by taking a look at the code you supplied. The program uses Jetpack Compose to create a red blob shape on a white canvas in Kotlin. Here is an explanation of what is occurring in the code:



@Composable
fun Blob() {     
Canvas(modifier = Modifier
                  .size(400.dp)         
                  .background(Color.White)
){

    // Define the path of the blob shape
    val blobPath = Path().apply {
    moveTo(size.width * 0.5f, size.height * 0.2f)  
               
    cubicTo(size.width * 0.8f, size.height * 0.3f, size.width * 0.8f, size.height * 0.7f, size.width * 0.5f, size.height * 0.8f) 
    
    cubicTo(size.width * 0.2f, size.height * 0.9f, size.width * 0.2f, size.height * 0.5f, size.width * 0.5f, size.height * 0.2f)             
    
    close()
    
    }
    
    // Draw the blob shape on the canvas
    drawPath(
    path = blobPath,
    color = Color.Red
    )
    
    }
}

Use the Canvas composable function in Jetpack Compose to draw shapes on a canvas. The above code instance's canvas has a white background and a 400.dp size.


The path of the blob shape is then defined using the Path class. The Path class depicts a geometrical line that can be drawn on a canvas. In this instance, a succession of moveTo and cubicTo instructions defines the path. The cubicTo command specifies a cubic Bezier curve that connects the path's starting and ending points, whereas the moveTo command specifies the path's starting point.


Finally, we draw the blob shape on the canvas using the draw path method. The drawPath process takes the path of the shape and the color to fill the shape with. In this case, we're filling the blob shape with a red color.


You can try different ways of sketching shapes by altering their trajectory. Furthermore, the drawPath method allows you to specify a Stroke or Brush object so as to customize the color and line thickness of your shape.


Here's an example of drawing a simple rectangle on a canvas:


@Composable
fun Rectangle() {     
             Canvas(modifier = Modifier         
                               .size(400.dp)         
                               .background(Color.White)     
  ) {         
        // Define the path of the rectangle shape
        val rectPath = Path().apply {             
        moveTo(0f, 0f)             
        lineTo(size.width, 0f)             
        lineTo(size.width, size.height)             
        lineTo(0f, size.height)             
        close()
         }  
        // Draw the rectangle shape on the canvas         
         drawPath(             
        path = rectPath,             
        color = Color.Blue,             
        style = Stroke(width = 2.dp.toPx())        
        )
  }

You can also draw other shapes like circles, ovals, and triangles using the Path class. For example, to draw a circle on a canvas, you can use the addCircle method of the Path class like this:



@Composable
funCircle() {
           Canvas(modifier = Modifier         
                             .size(400.dp)         
                             .background(Color.White)     
  ) {         
            // Define the path of the circle shape
      val circlePath = Path().apply {             
                       addCircle(size.width/2f, size.height/2f, 100.dp.toPx(), Path.Direction.CW)         
            }
            // Draw the circle shape on the canvas
            drawPath(             
            path = circlePath,             
            color = Color.Green,             
            style = Stroke(width = 2.dp.toPx())         
            )
  }
 }

In this example, we're drawing a green circle with a radius of 100 pixels and a 2-pixel-wide stroke.

In conclusion, Jetpack Compose provides a powerful way to draw shapes on a canvas using the Canvas composable and the Path class. You can create a variety of shapes and customize their appearance using strokes and brushes. With some experimentation and practice, you can create beautiful and engaging user interfaces for your Android applications.

bottom of page