Remote Compose Storybook Русский GitHub

Shapes as outlines

A rounded-corner shape drawn on a canvas.

Shapes as outlines: the document drawn by the Remote Compose player
Open the live, interactive demo →

Change its controls, read the remote document it produces and the diff after every change.

What it is

Turns a shape such as a rounded rectangle into something that can be filled or used as a mask while drawing.

Good for

  • Custom cards and frames drawn on a canvas

Code

The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.

import androidx.compose.remote.creation.compose.layout.RemoteCanvas
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteOffset
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.height
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.shapes.RemoteRoundedCornerShape
import androidx.compose.remote.creation.compose.shapes.clipOutline
import androidx.compose.remote.creation.compose.shapes.drawOutline
import androidx.compose.remote.creation.compose.state.RemotePaint
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rf
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

@Composable
@RemoteComposable
fun CanvasOutline(corner: Int, color: Color) {
    RemoteCanvas(modifier = RemoteModifier.size(260.rdp, 180.rdp)) {
        // A shape turns into an outline for the canvas's size, which drawOutline paints…
        val outline =
            RemoteRoundedCornerShape(corner.rdp).createOutline(size, layoutDirection, remoteDensity)
        drawOutline(outline, RemotePaint { this.color = color.rc })
        // …and clipOutline clips the drawing in its block.
        clipOutline(outline) {
            drawCircle(
                RemotePaint { this.color = Color(0x88FFFFFF).rc },
                radius = height / 2f.rf,
                center = RemoteOffset(0f.rf, 0f.rf),
            )
        }
    }
}

API

creation.compose.shapes.RemoteShape

interface RemoteShape
  RemoteOutline createOutline(RemoteSize size, RemoteLayoutDirection layoutDirection, RemoteDensity density)

creation.compose.shapes.RemoteOutline

sealed nonexhaustive class RemoteOutline
  RemoteOutline.Generic(RemotePathScope.() -> Unit block)
  RemoteOutline.Rectangle(RemoteOffset topLeft, RemoteSize size)
  RemoteSize size
  RemoteOffset topLeft
  RemoteOutline.Rounded(RemoteFloat topStart, RemoteFloat topEnd, RemoteFloat bottomEnd, RemoteFloat bottomStart, optional RemoteOffset offset, optional RemoteSize? size)
  RemoteFloat bottomEnd
  RemoteFloat bottomStart
  RemoteOffset offset
  RemoteSize? size
  RemoteFloat topEnd
  RemoteFloat topStart

creation.compose.shapes.drawOutline

void drawOutline(RemoteDrawScope, RemoteOutline outline, RemotePaint paint)

Reference

More in Canvas