{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

DrawScopeKt

public final class DrawScopeKt


Summary

Public methods

static final void
clipPath(
    @NonNull DrawScope receiver,
    @NonNull Path path,
    @NonNull ClipOp clipOp,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Reduces the clip region to the intersection of the current clip and the given path.

static final void
clipRect(
    @NonNull DrawScope receiver,
    float left,
    float top,
    float right,
    float bottom,
    @NonNull ClipOp clipOp,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Reduces the clip region to the intersection of the current clip and the given rectangle indicated by the given left, top, right and bottom bounds.

static final void
drawIntoCanvas(
    @NonNull DrawScope receiver,
    @NonNull Function1<@NonNull CanvasUnit> block
)

Provides access to draw directly with the underlying Canvas.

static final void
inset(
    @NonNull DrawScope receiver,
    float inset,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Convenience method modifies the DrawScope bounds to inset both left, top, right and bottom bounds by inset.

static final void
inset(
    @NonNull DrawScope receiver,
    float horizontal,
    float vertical,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Convenience method modifies the DrawScope bounds to inset both left and right bounds by horizontal as well as the top and bottom by vertical.

static final void
inset(
    @NonNull DrawScope receiver,
    float left,
    float top,
    float right,
    float bottom,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Simultaneously translate the DrawScope coordinate space by left and top as well as modify the dimensions of the current painting area.

static final void
rotate(
    @NonNull DrawScope receiver,
    float degrees,
    @NonNull Offset pivot,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Add a rotation (in degrees clockwise) to the current transform at the given pivot point.

static final void
rotateRad(
    @NonNull DrawScope receiver,
    float radians,
    @NonNull Offset pivot,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Add a rotation (in radians clockwise) to the current transform at the given pivot point.

static final void
scale(
    @NonNull DrawScope receiver,
    float scale,
    @NonNull Offset pivot,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Add an axis-aligned scale to the current transform, scaling both the horizontal direction and the vertical direction at the given pivot coordinate.

static final void
scale(
    @NonNull DrawScope receiver,
    float scaleX,
    float scaleY,
    @NonNull Offset pivot,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Add an axis-aligned scale to the current transform, scaling by the first argument in the horizontal direction and the second in the vertical direction at the given pivot coordinate.

static final void
translate(
    @NonNull DrawScope receiver,
    float left,
    float top,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Translate the coordinate space by the given delta in pixels in both the x and y coordinates respectively

static final void
withTransform(
    @NonNull DrawScope receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawTransformUnit> transformBlock,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> drawBlock
)

Perform 1 or more transformations and execute drawing commands with the specified transformations applied.

Public methods

clipPath

public static final void clipPath(
    @NonNull DrawScope receiver,
    @NonNull Path path,
    @NonNull ClipOp clipOp,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Reduces the clip region to the intersection of the current clip and the given path. This method provides a callback to issue drawing commands within the region defined by the clipped path. After this method is invoked, this clip is no longer applied.

Parameters
@NonNull Path path

Shape to clip drawing content within

@NonNull ClipOp clipOp

Clipping operation to conduct on the given bounds, defaults to ClipOp.Intersect

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block

Lambda callback with this CanvasScope as a receiver scope to issue drawing commands within the provided clip

clipRect

public static final void clipRect(
    @NonNull DrawScope receiver,
    float left,
    float top,
    float right,
    float bottom,
    @NonNull ClipOp clipOp,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Reduces the clip region to the intersection of the current clip and the given rectangle indicated by the given left, top, right and bottom bounds. This provides a callback to issue drawing commands within the clipped region. After this method is invoked, this clip is no longer applied.

Use ClipOp.Difference to subtract the provided rectangle from the current clip.

Parameters
float left

Left bound of the rectangle to clip

float top

Top bound of the rectangle to clip

float right

Right bound of the rectangle to clip

float bottom

Bottom bound of the rectangle to clip

@NonNull ClipOp clipOp

Clipping operation to conduct on the given bounds, defaults to ClipOp.Intersect

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block

Lambda callback with this CanvasScope as a receiver scope to issue drawing commands within the provided clip

drawIntoCanvas

public static final void drawIntoCanvas(
    @NonNull DrawScope receiver,
    @NonNull Function1<@NonNull CanvasUnit> block
)

Provides access to draw directly with the underlying Canvas. This is helpful for situations to re-use alternative drawing logic in combination with DrawScope

Parameters
@NonNull Function1<@NonNull CanvasUnit> block

Lambda callback to issue drawing commands on the provided Canvas

inset

public static final void inset(
    @NonNull DrawScope receiver,
    float inset,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Convenience method modifies the DrawScope bounds to inset both left, top, right and bottom bounds by inset. After this method is invoked, the coordinate space is returned to the state before this inset was applied.

Parameters
float inset

number of pixels to inset left, top, right, and bottom bounds.

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block

lambda that is called to issue additional drawing commands within the modified coordinate space

inset

public static final void inset(
    @NonNull DrawScope receiver,
    float horizontal,
    float vertical,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Convenience method modifies the DrawScope bounds to inset both left and right bounds by horizontal as well as the top and bottom by vertical. After this method is invoked, the coordinate space is returned to the state before this inset was applied.

Parameters
float horizontal

number of pixels to inset both left and right bounds. Zero by default

float vertical

Optional number of pixels to inset both top and bottom bounds. Zero by default

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block

lambda that is called to issue additional drawing commands within the modified coordinate space

inset

public static final void inset(
    @NonNull DrawScope receiver,
    float left,
    float top,
    float right,
    float bottom,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Simultaneously translate the DrawScope coordinate space by left and top as well as modify the dimensions of the current painting area. This provides a callback to issue more drawing instructions within the modified coordinate space. This method modifies the width of the DrawScope to be equivalent to width - (left + right) as well as height to height - (top + bottom). After this method is invoked, the coordinate space is returned to the state before the inset was applied.

Parameters
float left

number of pixels to inset the left drawing bound

float top

number of pixels to inset the top drawing bound

float right

number of pixels to inset the right drawing bound

float bottom

number of pixels to inset the bottom drawing bound

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block

lambda that is called to issue drawing commands within the inset coordinate space

rotate

public static final void rotate(
    @NonNull DrawScope receiver,
    float degrees,
    @NonNull Offset pivot,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Add a rotation (in degrees clockwise) to the current transform at the given pivot point. The pivot coordinate remains unchanged by the rotation transformation. After the provided lambda is invoked, the rotation transformation is undone.

Parameters
float degrees

to rotate clockwise

@NonNull Offset pivot

The coordinate for the pivot point, defaults to the center of the coordinate space

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block

lambda that is called to issue drawing commands within the rotated coordinate space

rotateRad

public static final void rotateRad(
    @NonNull DrawScope receiver,
    float radians,
    @NonNull Offset pivot,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Add a rotation (in radians clockwise) to the current transform at the given pivot point. The pivot coordinate remains unchanged by the rotation transformation

Parameters
float radians

to rotate clockwise

@NonNull Offset pivot

The coordinate for the pivot point, defaults to the center of the coordinate space

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block

lambda that is called to issue drawing commands within the rotated coordinate space

scale

public static final void scale(
    @NonNull DrawScope receiver,
    float scale,
    @NonNull Offset pivot,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Add an axis-aligned scale to the current transform, scaling both the horizontal direction and the vertical direction at the given pivot coordinate. The pivot coordinate remains unchanged by the scale transformation. After this method is invoked, the coordinate space is returned to the state before the scale was applied.

Parameters
float scale

The amount to scale uniformly in both directions

@NonNull Offset pivot

The coordinate for the pivot point, defaults to the center of the coordinate space

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block

lambda used to issue drawing commands within the scaled coordinate space

scale

public static final void scale(
    @NonNull DrawScope receiver,
    float scaleX,
    float scaleY,
    @NonNull Offset pivot,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Add an axis-aligned scale to the current transform, scaling by the first argument in the horizontal direction and the second in the vertical direction at the given pivot coordinate. The pivot coordinate remains unchanged by the scale transformation. After this method is invoked, the coordinate space is returned to the state before the scale was applied.

Parameters
float scaleX

The amount to scale in X

float scaleY

The amount to scale in Y

@NonNull Offset pivot

The coordinate for the pivot point, defaults to the center of the coordinate space

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block

lambda used to issue drawing commands within the scaled coordinate space

translate

public static final void translate(
    @NonNull DrawScope receiver,
    float left,
    float top,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block
)

Translate the coordinate space by the given delta in pixels in both the x and y coordinates respectively

Parameters
float left

Pixels to translate the coordinate space in the x-axis

float top

Pixels to translate the coordinate space in the y-axis

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> block

lambda that is called to issue drawing commands within the translated coordinate space

withTransform

public static final void withTransform(
    @NonNull DrawScope receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawTransformUnit> transformBlock,
    @ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> drawBlock
)

Perform 1 or more transformations and execute drawing commands with the specified transformations applied. After this call is complete, the transformation before this call was made is restored

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.size
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.inset
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.graphics.drawscope.withTransform
import androidx.compose.ui.unit.dp

Canvas(Modifier.size(120.dp)) { // CanvasScope
    inset(20.0f) {
        // Use withTransform to batch multiple transformations for 1 or more drawing calls
        // that are to be drawn.
        // This is more efficient than issuing nested translation, rotation and scaling
        // calls as the internal state is saved once before and after instead of multiple
        // times between each transformation if done individually
        withTransform({
            translate(10.0f, 12.0f)
            rotate(45.0f, center)
            scale(2.0f, 0.5f)
        }) {
            drawRect(Color.Cyan)
            drawCircle(Color.Blue)
        }
        drawRect(Color.Red, alpha = 0.25f)
    }
}
Parameters
@ExtensionFunctionType @NonNull Function1<@NonNull DrawTransformUnit> transformBlock

Callback invoked to issue transformations to be made before the drawing operations are issued

@ExtensionFunctionType @NonNull Function1<@NonNull DrawScopeUnit> drawBlock

Callback invoked to issue drawing operations after the transformations are applied