Google is committed to advancing racial equity for Black communities. See how.

DrawScope

interface DrawScope : Density
androidx.compose.ui.graphics.drawscope.DrawScope

Creates a scoped drawing environment with the provided Canvas. This provides a declarative, stateless API to draw shapes and paths without requiring consumers to maintain underlying Canvas state information. DrawScope implementations are also provided sizing information and transformations are done relative to the local translation. That is left and top coordinates are always the origin and the right and bottom coordinates are always the specified width and height respectively. Drawing content is not clipped, so it is possible to draw outside of the specified bounds.

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.preferredSize
import androidx.compose.ui.graphics.drawscope.inset
import androidx.compose.ui.graphics.drawscope.rotate

// Sample showing how to use the DrawScope receiver scope to issue
// drawing commands
Canvas(Modifier.preferredSize(120.dp)) {
    drawRect(color = Color.Gray) // Draw grey background
    // Inset content by 10 pixels on the left/right sides and 12 by the
    // top/bottom
    inset(10.0f, 12.0f) {
        val quadrantSize = size / 2.0f

        // Draw a rectangle within the inset bounds
        drawRect(
            size = quadrantSize,
            color = Color.Red
        )

        rotate(45.0f) {
            drawRect(size = quadrantSize, color = Color.Blue)
        }
    }
}

Summary

Public methods
abstract Unit
drawArc(brush: Brush, startAngle: Float, sweepAngle: Float, useCenter: Boolean, topLeft: Offset = Offset.Zero, size: Size = this.size.offsetSize(topLeft), @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draw an arc scaled to fit inside the given rectangle.

abstract Unit
drawArc(color: Color, startAngle: Float, sweepAngle: Float, useCenter: Boolean, topLeft: Offset = Offset.Zero, size: Size = this.size.offsetSize(topLeft), @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draw an arc scaled to fit inside the given rectangle.

abstract Unit
drawCircle(brush: Brush, radius: Float = size.minDimension / 2.0f, center: Offset = this.center, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws a circle at the provided center coordinate and radius.

abstract Unit
drawCircle(color: Color, radius: Float = size.minDimension / 2.0f, center: Offset = this.center, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws a circle at the provided center coordinate and radius.

abstract Unit
drawImage(image: ImageAsset, topLeft: Offset = Offset.Zero, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws the given ImageAsset into the canvas with its top-left corner at the given Offset.

abstract Unit
drawImage(image: ImageAsset, srcOffset: IntOffset = IntOffset.Zero, srcSize: IntSize = IntSize(image.width, image.height), dstOffset: IntOffset = IntOffset.Zero, dstSize: IntSize = srcSize, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws the subset of the given image described by the src argument into the canvas in the axis-aligned rectangle given by the dst argument.

abstract Unit
drawLine(brush: Brush, start: Offset, end: Offset, strokeWidth: Float = Stroke.HairlineWidth, cap: StrokeCap = Stroke.DefaultCap, pathEffect: NativePathEffect? = null, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws a line between the given points using the given paint.

abstract Unit
drawLine(color: Color, start: Offset, end: Offset, strokeWidth: Float = Stroke.HairlineWidth, cap: StrokeCap = Stroke.DefaultCap, pathEffect: NativePathEffect? = null, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws a line between the given points using the given paint.

abstract Unit
drawOval(brush: Brush, topLeft: Offset = Offset.Zero, size: Size = this.size.offsetSize(topLeft), @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws an oval with the given offset and size.

abstract Unit
drawOval(color: Color, topLeft: Offset = Offset.Zero, size: Size = this.size.offsetSize(topLeft), @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws an oval with the given offset and size.

abstract Unit
drawPath(path: Path, color: Color, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws the given Path with the given Color.

abstract Unit
drawPath(path: Path, brush: Brush, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws the given Path with the given Color.

abstract Unit
drawPoints(points: List<Offset>, pointMode: PointMode, color: Color, strokeWidth: Float = Stroke.HairlineWidth, cap: StrokeCap = StrokeCap.Butt, pathEffect: NativePathEffect? = null, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws a sequence of points according to the given PointMode.

abstract Unit
drawPoints(points: List<Offset>, pointMode: PointMode, brush: Brush, strokeWidth: Float = Stroke.HairlineWidth, cap: StrokeCap = StrokeCap.Butt, pathEffect: NativePathEffect? = null, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws a sequence of points according to the given PointMode.

abstract Unit
drawRect(brush: Brush, topLeft: Offset = Offset.Zero, size: Size = this.size.offsetSize(topLeft), @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws a rectangle with the given offset and size.

abstract Unit
drawRect(color: Color, topLeft: Offset = Offset.Zero, size: Size = this.size.offsetSize(topLeft), @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws a rectangle with the given offset and size.

abstract Unit
drawRoundRect(brush: Brush, topLeft: Offset = Offset.Zero, size: Size = this.size.offsetSize(topLeft), cornerRadius: CornerRadius = CornerRadius.Zero, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DefaultBlendMode)

Draws a rounded rectangle with the provided size, offset and radii for the x and y axis respectively.

abstract Unit
drawRoundRect(color: Color, topLeft: Offset = Offset.Zero, size: Size = this.size.offsetSize(topLeft), cornerRadius: CornerRadius = CornerRadius.Zero,