DrawScope: Lines, Rects & Circles
Draw primitives onto the canvas.
DrawScope: Lines, Rects & Circles is a free Jetpack Compose Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet the Canvas
The Canvas Composable hands you a blank drawing surface. Inside it you paint pixels directly instead of stacking other Composables.
Canvas(modifier = Modifier.size(200.dp)) {
// drawing goes here
}Inside DrawScope
The lambda you pass to Canvas runs inside a DrawScope. That receiver gives you draw functions and the current size to work with.
Know Your Size
DrawScope exposes size, the area you can paint. Read size.width and size.height so your art scales to whatever space it gets.
Canvas(Modifier.fillMaxSize()) {
val w = size.width
val h = size.height
}Draw a Line
Use drawLine to connect two points. You pass a color, a start Offset, an end Offset, and a stroke width.
drawLine(
color = Color.Blue,
start = Offset(0f, 0f),
end = Offset(size.width, size.height),
strokeWidth = 4f
)Pixels Are Floats
Every coordinate here is a Float in pixels, not dp. The origin (0,0) sits at the top-left and x grows right, y grows down.
Offset Points the Way
An Offset is just an x and y pair. You will build almost every shape position from Offsets, so they show up everywhere on the canvas.
val center = Offset(size.width / 2, size.height / 2)Draw a Rectangle
Call drawRect with a color, a topLeft Offset, and a Size. Leave topLeft out and it fills the whole canvas for you.
drawRect(
color = Color.Green,
topLeft = Offset(20f, 20f),
size = Size(100f, 60f)
)Draw a Circle
Use drawCircle with a color, a radius, and a center Offset. Center it on the canvas and you get a perfectly placed dot.
drawCircle(
color = Color.Red,
radius = 50f,
center = center
)Fill or Stroke
Each shape takes a style. The default is solid Fill, but pass Stroke to draw just the outline with a width you choose.
drawCircle(
color = Color.Black,
radius = 50f,
style = Stroke(width = 6f)
)Control Transparency
Pass an alpha between 0f and 1f to fade any shape. Stack a few translucent circles and you get a soft layered glow.
drawCircle(color = Color.Magenta, radius = 60f, alpha = 0.4f)Order Is Layering
Draw calls paint in order, so later shapes sit on top of earlier ones. Think of it like dropping cutouts onto a page one by one.
Quick Check
Where does the canvas coordinate system begin?
Recap
You learned the Canvas basics: draw inside DrawScope, read size, and paint lines, rects, and circles with Offsets and float pixels. Order is everything! 🎨
Frequently asked questions
Is the “DrawScope: Lines, Rects & Circles” lesson free?
Yes — the full text of “DrawScope: Lines, Rects & Circles” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.
What will I learn in “DrawScope: Lines, Rects & Circles”?
Draw primitives onto the canvas. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Jetpack Compose Academy?
No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “DrawScope: Lines, Rects & Circles” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Jetpack Compose Academy lesson?
Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- DrawScope: Lines, Rects & Circles
- Paths, Gradients & Blend Modes
- drawWithCache & Performance
- Build a Live Progress Ring