Class

RemoteDrawScope

A remote-compatible drawing scope for RemoteCompose.

Source set: JvmAndAndroid
public open class RemoteDrawScope internal constructor(
    @get:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) public val remoteCanvas: RemoteCanvas
) : RemoteStateScope by remoteCanvas.creationState {
    public val remoteComposeCreationState: RemoteComposeCreationState
        get() = remoteCanvas.creationState

    /** The width of the drawing area as a [RemoteFloat]. */
    public open val width: RemoteFloat
        get() = remoteCanvas.remote.component.width

    /** The height of the drawing area as a [RemoteFloat]. */
    public open val height: RemoteFloat
        get() = remoteCanvas.remote.component.height

    /** The center of the drawing area as a [RemoteOffset]. */
    public val center: RemoteOffset
        get() = RemoteOffset(width / 2f, height / 2f)

    /** The size of the drawing area as a [RemoteSize]. */
    public val size: RemoteSize
        get() = RemoteSize(width, height)

    /**
     * Applies [paint] for drawing commands executed within [block].
     *
     * @param paint The [RemotePaint] to apply before executing [block].
     * @param block Drawing commands to execute with [paint] active.
     */
    public fun usePaint(paint: RemotePaint, block: () -> Unit) {
        remoteCanvas.usePaint(paint)
        block()
    }

    /**
     * Draws a rectangle with the given [paint], [topLeft] offset, and [size].
     *
     * @param paint The [RemotePaint] to use for drawing the rectangle.
     * @param topLeft The top left offset of the rectangle.
     * @param size The size of the rectangle.
     */
    public fun drawRect(
        paint: RemotePaint?,
        topLeft: RemoteOffset = RemoteOffset.Zero,
        size: RemoteSize = [email protected],
    ) {
        remoteCanvas.drawRect(
            topLeft.x,
            topLeft.y,
            topLeft.x + size.width,
            topLeft.y + size.height,
            paint,
        )
    }

    /**
     * Draws a rounded rectangle with the given [paint], [topLeft] offset, [size], and
     * [cornerRadius].
     *
     * @param paint The [RemotePaint] to use for drawing the rounded rectangle.
     * @param topLeft The top-left offset of the rounded rectangle.
     * @param size The size of the rounded rectangle.
     * @param cornerRadius The x and y radii of the rounded corners.
     */
    public fun drawRoundRect(
        paint: RemotePaint?,
        topLeft: RemoteOffset = RemoteOffset.Zero,
        size: RemoteSize = [email protected],
        cornerRadius: RemoteOffset = RemoteOffset.Zero,
    ) {
        remoteCanvas.drawRoundRect(
            topLeft.x,
            topLeft.y,
            topLeft.x + size.width,
            topLeft.y + size.height,
            cornerRadius.x,
            cornerRadius.y,
            paint,
        )
    }

    /**
     * Draws a circle at [center] with the given [radius] and [paint].
     *
     * @param paint The [RemotePaint] to use for drawing the circle.
     * @param radius The radius of the circle in pixels.
     * @param center The center coordinate of the circle.
     */
    public fun drawCircle(
        paint: RemotePaint?,
        radius: RemoteFloat,
        center: RemoteOffset = [email protected],
    ) {
        RemoteSize(radius * 2f, radius * 2f)
        remoteCanvas.drawCircle(center.x, center.y, radius, paint)
    }

    /**
     * Draws an oval bounded by the rectangle defined by [topLeft] and [size].
     *
     * @param paint The [RemotePaint] to use for drawing the oval.
     * @param topLeft The top-left offset of the bounding rectangle.
     * @param size The size of the bounding rectangle.
     */
    public fun drawOval(
        paint: RemotePaint?,
        topLeft: RemoteOffset = RemoteOffset.Zero,
        size: RemoteSize = [email protected],
    ) {
        remoteCanvas.drawOval(
            topLeft.x,
            topLeft.y,
            topLeft.x + size.width,
            topLeft.y + size.height,
            paint,
        )
    }

    /**
     * Draws an arc scaled to fit inside the rectangle defined by [topLeft] and [size].
     *
     * @param paint The [RemotePaint] to use for drawing the arc.
     * @param startAngle Starting angle in degrees where 0 degrees is at 3 o'clock.
     * @param sweepAngle Clockwise sweep angle in degrees relative to [startAngle].
     * @param useCenter Whether the arc is connected to the center of the oval (forming a wedge).
     * @param topLeft The top-left offset of the bounding rectangle.
     * @param size The size of the bounding rectangle.
     */
    public fun drawArc(
        paint: RemotePaint?,
        startAngle: RemoteFloat,
        sweepAngle: RemoteFloat,
        useCenter: Boolean,
        topLeft: RemoteOffset = RemoteOffset.Zero,
        size: RemoteSize = [email protected],
    ) {
        remoteCanvas.drawArc(
            topLeft.x,
            topLeft.y,
            topLeft.x + size.width,
            topLeft.y + size.height,
            startAngle,
            sweepAngle,
            useCenter,
            paint,
        )
    }

    /**
     * Draws a line segment between [start] and [end] using the given [paint].
     *
     * @param paint The [RemotePaint] to use for drawing the line.
     * @param start The starting point of the line.
     * @param end The ending point of the line.
     */
    public fun drawLine(paint: RemotePaint?, start: RemoteOffset, end: RemoteOffset) {
        remoteCanvas.drawLine(start.x, start.y, end.x, end.y, paint)
    }

    /**
     * Draws [image] with its top-left corner at [topLeft] using [paint].
     *
     * @param image [RemoteImageBitmap] to draw
     * @param topLeft top-left coordinate where [image] is drawn in pixels
     * @param paint [RemotePaint] applied when drawing [image], or `null` for default paint
     */
    public fun drawImage(
        image: RemoteImageBitmap,
        topLeft: RemoteOffset = RemoteOffset.Zero,
        paint: RemotePaint? = RemotePaint(),
    ) {
        RemoteSize(image.width, image.height)
        remoteCanvas.drawBitmap(image, topLeft.x, topLeft.y, paint)
    }

    /** Draws a bitmap scaled to the destination rectangle. */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public fun drawScaledBitmap(
        image: RemoteImageBitmap,
        srcOffset: RemoteOffset = RemoteOffset.Zero,
        srcSize: RemoteSize = RemoteSize(image.width, image.height),
        dstOffset: RemoteOffset = RemoteOffset.Zero,
        dstSize: RemoteSize = size,
        scaleType: RemoteContentScale = RemoteContentScale.Fit,
        scaleFactor: RemoteFloat = 1f.rf,
        contentDescription: String? = null,
    ) {
        remoteCanvas.drawScaledBitmap(
            image,
            srcOffset.x,
            srcOffset.y,
            srcOffset.x + srcSize.width,
            srcOffset.y + srcSize.height,
            dstOffset.x,
            dstOffset.y,
            dstOffset.x + dstSize.width,
            dstOffset.y + dstSize.height,
            scaleType.value,
            scaleFactor,
            contentDescription,
        )
    }

    /** Draws a path. */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public fun drawPath(
        path: RemotePath,
        pathFillType: RemotePathFillType = RemotePathFillType.NonZero,
        paint: RemotePaint?,
    ) {
        remoteCanvas.drawPath(path, pathFillType, paint)
    }

    /** Draws a path. */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public fun drawPath(path: RemotePath, paint: RemotePaint?) {
        remoteCanvas.drawPath(path, paint = paint)
    }

    /** Draws a tween path. */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public fun drawTweenPath(
        path1: RemotePath,
        path2: RemotePath,
        tween: RemoteFloat,
        start: RemoteFloat = 0f.rf,
        stop: RemoteFloat = 1f.rf,
        paint: RemotePaint?,
    ) {
        remoteCanvas.drawTweenPath(path1, path2, tween, start, stop, paint)
    }

    /**
     * Draws [text] with its origin at `([x], [y])` on the baseline using [paint].
     *
     * @param text [RemoteString] to draw
     * @param x horizontal origin in pixels
     * @param y baseline y-coordinate in pixels
     * @param paint [RemotePaint] controlling text size, color, and typeface, or `null` for the
     *   active paint
     */
    public fun drawText(
        text: RemoteString,
        x: RemoteFloat,
        y: RemoteFloat,
        paint: RemotePaint? = null,
    ) {
        remoteCanvas.drawText(text, x, y, paint)
    }

    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public fun drawAnchoredText(
        text: RemoteString,
        anchorX: RemoteFloat,
        anchorY: RemoteFloat,
        paint: RemotePaint? = null,
        panX: RemoteFloat = 0f.rf,
        panY: RemoteFloat = 0f.rf,
        flags: Int = 0,
    ) {
        remoteCanvas.drawAnchoredText(
            text = text,
            anchorX = anchorX,
            anchorY = anchorY,
            panx = panX,
            pany = panY,
            flags = flags,
            paint = paint,
        )
    }

    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public fun drawTextOnPath(
        text: RemoteString,
        path: RemotePath,
        hOffset: RemoteFloat = 0f.rf,
        vOffset: RemoteFloat = 0f.rf,
        paint: RemotePaint?,
    ) {
        remoteCanvas.drawTextOnPath(text, path, hOffset, vOffset, paint)
    }

    /**
     * Rotates the coordinate space clockwise by [degrees] around [pivot] while running [block].
     *
     * @param degrees clockwise rotation angle in degrees
     * @param pivot coordinate around which the rotation occurs, defaults to [center]
     * @param block drawing commands to run in the rotated coordinate space
     */
    public fun rotate(
        degrees: RemoteFloat,
        pivot: RemoteOffset = center,
        block: RemoteDrawScope.() -> Unit,
    ) {
        withTransform({ rotate(degrees, pivot) }, block)
    }

    /**
     * Translates the coordinate space by [left] and [top] pixels while running [block].
     *
     * @param left horizontal translation offset in pixels
     * @param top vertical translation offset in pixels
     * @param block drawing commands to run in the translated coordinate space
     */
    public fun translate(
        left: RemoteFloat = 0f.rf,
        top: RemoteFloat = 0f.rf,
        block: RemoteDrawScope.() -> Unit,
    ) {
        withTransform({ translate(left, top) }, block)
    }

    /**
     * Scales the coordinate space uniformly by [scale] around [pivot] while running [block].
     *
     * @param scale uniform scale factor along both axes
     * @param pivot coordinate that remains unchanged after scaling, defaults to [center]
     * @param block drawing commands to run in the scaled coordinate space
     */
    public fun scale(
        scale: RemoteFloat,
        pivot: RemoteOffset = center,
        block: RemoteDrawScope.() -> Unit,
    ) {
        withTransform({ scale(scale, scale, pivot) }, block)
    }

    /**
     * Scales the coordinate space by [scaleX] and [scaleY] around [pivot] while running [block].
     *
     * @param scaleX horizontal scale factor
     * @param scaleY vertical scale factor
     * @param pivot coordinate that remains unchanged after scaling, defaults to [center]
     * @param block drawing commands to run in the scaled coordinate space
     */
    public fun scale(
        scaleX: RemoteFloat,
        scaleY: RemoteFloat,
        pivot: RemoteOffset = center,
        block: RemoteDrawScope.() -> Unit,
    ) {
        withTransform({ scale(scaleX, scaleY, pivot) }, block)
    }

    /**
     * Saves the canvas state, applies [transformBlock], runs [drawBlock], and restores the state.
     *
     * @param transformBlock transformations and clips applied on a [RemoteDrawTransform] receiver
     * @param drawBlock drawing commands run while the transformations and clips are active
     */
    public fun withTransform(
        transformBlock: RemoteDrawTransform.() -> Unit,
        drawBlock: RemoteDrawScope.() -> Unit,
    ) {
        remoteCanvas.save()
        RemoteCanvasDrawTransform(this).transformBlock()
        this.drawBlock()
        remoteCanvas.restore()
    }

    /**
     * Draws text along a circle.
     *
     * @param text The text to draw.
     * @param centerX The x-coordinate of the circle's center.
     * @param centerY The y-coordinate of the circle's center.
     * @param radius The radius of the circle.
     * @param startAngle The starting angle for the text.
     * @param warpRadiusOffset The offset of the text from the circle.
     * @param paint Paint of the text.
     * @param alignment The alignment of the text relative to start.
     * @param placement The placement inside or outside the circle.
     */
    public fun drawTextOnCircle(
        text: RemoteString,
        centerX: RemoteFloat,
        centerY: RemoteFloat,
        radius: RemoteFloat,
        startAngle: RemoteFloat,
        warpRadiusOffset: RemoteFloat,
        paint: RemotePaint? = null,
        alignment: CircularAlignment = CircularAlignment.Center,
        placement: CircularPlacement = CircularPlacement.Outside,
    ) {
        remoteCanvas.drawTextOnCircle(
            text,
            centerX,
            centerY,
            radius,
            startAngle,
            warpRadiusOffset,
            alignment.toCore(),
            placement.toCore(),
            paint,
        )
    }

    /** Alignment of text along a circular path. */
    public enum class CircularAlignment {
        Start,
        Center,
        End;

        internal fun toCore(): DrawTextOnCircle.Alignment =
            when (this) {
                Start -> DrawTextOnCircle.Alignment.START
                Center -> DrawTextOnCircle.Alignment.CENTER
                End -> DrawTextOnCircle.Alignment.END
            }
    }

    /** Placement of text relative to the circular path. */
    public enum class CircularPlacement {
        Outside,
        Inside;

        internal fun toCore(): DrawTextOnCircle.Placement =
            when (this) {
                Outside -> DrawTextOnCircle.Placement.OUTSIDE
                Inside -> DrawTextOnCircle.Placement.INSIDE
            }
    }

    /**
     * Clips the drawing area to `[left, top, right, bottom]` while running [block].
     *
     * @param left left bound of the clip rectangle in pixels
     * @param top top bound of the clip rectangle in pixels
     * @param right right bound of the clip rectangle in pixels
     * @param bottom bottom bound of the clip rectangle in pixels
     * @param block drawing commands to run within the clipped rectangle
     */
    public fun clipRect(
        left: RemoteFloat = 0f.rf,
        top: RemoteFloat = 0f.rf,
        right: RemoteFloat = width,
        bottom: RemoteFloat = height,
        block: RemoteDrawScope.() -> Unit,
    ) {
        withTransform({ clipRect(left, top, right, bottom) }, block)
    }

    /** Clips the drawing area to the specified [path] and executes [block] within it. */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public fun clipPath(path: RemotePath, block: RemoteDrawScope.() -> Unit) {
        remoteCanvas.save()
        remoteCanvas.clipPath(path)
        this.block()
        remoteCanvas.restore()
    }

    /** Executes [body] if [condition] evaluates to true. */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public fun drawConditionally(condition: RemoteBoolean, body: RemoteDrawScope.() -> Unit) {
        remoteCanvas.drawConditionally(condition) { this.body() }
    }

    /** Draws into an offscreen bitmap and executes [body]. */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public fun drawToOffscreenBitmap(bitmap: RemoteImageBitmap, body: RemoteDrawScope.() -> Unit) {
        remoteCanvas.drawToOffscreenBitmap(bitmap) { this.body() }
    }

    /** Executes [body] commands in a loop. */
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public fun loop(
        from: RemoteFloat,
        until: RemoteFloat,
        step: RemoteFloat,
        body: RemoteDrawScope.(index: RemoteFloat) -> Unit,
    ) {
        remoteCanvas.loop(from, until, step) { index -> this.body(index) }
    }

    /** Access to remote-specific utilities like time and animations. */
    public val remote: RemoteAccess = RemoteAccess(this)
}

A remote-compatible drawing scope for RemoteCompose. Unlike androidx.compose.ui.graphics.drawscope.DrawScope, this class uses remote types consistently and does not attempt to implement the standard androidx.compose.ui.graphics.drawscope.DrawScope interface to avoid API incompatibilities.

Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: