Remote Compose Storybook Русский GitHub

RemoteCanvas · Time-driven drawing

Positions computed from RemoteTimeVariables every frame.

RemoteCanvas · Time-driven drawing: 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

A free drawing surface: shapes, lines, text and transformations drawn exactly where you place them, including animated drawings.

Good for

  • Charts, gauges and progress rings
  • Custom illustrations
  • Watch-face style animated graphics

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.size
import androidx.compose.remote.creation.compose.state.RemotePaint
import androidx.compose.remote.creation.compose.state.RemotePaintingStyle
import androidx.compose.remote.creation.compose.state.RemoteTimeVariables
import androidx.compose.remote.creation.compose.state.cos
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.remote.creation.compose.state.sin
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

@Composable
@RemoteComposable
fun CanvasAnimated(speed: Float, amplitude: Float) {
    // RemoteTimeVariables are evaluated by the player every frame: the document never changes.
    RemoteCanvas(modifier = RemoteModifier.size(300.rdp, 200.rdp)) {
        val t = RemoteTimeVariables.continuousSeconds * speed.rf
        val x = 150f.rf + cos(t) * amplitude.rf
        val y = 100f.rf + sin(t * 2f.rf) * (amplitude / 2f).rf
        drawCircle(
            RemotePaint {
                color = Color(0x558E24AA).rc
                style = RemotePaintingStyle.Stroke
                strokeWidth = 2f.rf
            },
            radius = amplitude.rf,
            center = RemoteOffset(150f.rf, 100f.rf),
        )
        drawCircle(
            RemotePaint { color = Color(0xFF8E24AA).rc },
            radius = 18f.rf,
            center = RemoteOffset(x, y),
        )
    }
}

API

creation.compose.layout.RemoteCanvas

void RemoteCanvas(optional RemoteModifier modifier, RemoteDrawScope.() -> Unit onDraw)

creation.compose.layout.RemoteDrawScope

class RemoteDrawScope implements RemoteStateScope
  void clipRect(optional RemoteFloat left, optional RemoteFloat top, optional RemoteFloat right, optional RemoteFloat bottom, RemoteDrawScope.() -> Unit block)
  void drawArc(RemotePaint? paint, RemoteFloat startAngle, RemoteFloat sweepAngle, boolean useCenter, optional RemoteOffset topLeft, optional RemoteSize size)
  void drawCircle(RemotePaint? paint, RemoteFloat radius, optional RemoteOffset center)
  void drawImage(RemoteImageBitmap image, optional RemoteOffset topLeft, optional RemotePaint? paint)
  void drawLine(RemotePaint? paint, RemoteOffset start, RemoteOffset end)
  void drawOval(RemotePaint? paint, optional RemoteOffset topLeft, optional RemoteSize size)
  void drawRect(RemotePaint? paint, optional RemoteOffset topLeft, optional RemoteSize size)
  void drawRoundRect(RemotePaint? paint, optional RemoteOffset topLeft, optional RemoteSize size, optional RemoteOffset cornerRadius)
  void drawText(RemoteString text, RemoteFloat x, RemoteFloat y, optional RemotePaint? paint)
  void drawTextOnCircle(RemoteString text, RemoteFloat centerX, RemoteFloat centerY, RemoteFloat radius, RemoteFloat startAngle, RemoteFloat warpRadiusOffset, optional RemotePaint? paint, optional RemoteDrawScope.CircularAlignment alignment, optional RemoteDrawScope.CircularPlacement placement)
  void rotate(RemoteFloat degrees, optional RemoteOffset pivot, RemoteDrawScope.() -> Unit block)
  void scale(RemoteFloat scale, optional RemoteOffset pivot, RemoteDrawScope.() -> Unit block)
  void scale(RemoteFloat scaleX, RemoteFloat scaleY, optional RemoteOffset pivot, RemoteDrawScope.() -> Unit block)
  void translate(optional RemoteFloat left, optional RemoteFloat top, RemoteDrawScope.() -> Unit block)
  void usePaint(RemotePaint paint, () -> Unit block)
  void withTransform(RemoteDrawTransform.() -> Unit transformBlock, RemoteDrawScope.() -> Unit drawBlock)
  RemoteOffset center
  RemoteFloat height
  RemoteLayoutDirection layoutDirection
  RemoteDensity remoteDensity
  RemoteSize size
  RemoteFloat width
  RemoteDrawScope.CircularAlignment Center
  RemoteDrawScope.CircularAlignment End
  RemoteDrawScope.CircularAlignment Start
  RemoteDrawScope.CircularPlacement Inside
  RemoteDrawScope.CircularPlacement Outside

creation.compose.state.RemotePaint

sealed nonexhaustive interface RemotePaint
  RemoteBlendMode blendMode
  RemoteColor color
  String? fontFeatureSettings
  RemoteFontVariation.Settings? fontVariationSettings
  RemoteStrokeCap strokeCap
  RemoteStrokeJoin strokeJoin
  RemoteFloat strokeWidth
  RemotePaintingStyle style
  RemoteFloat textSize
  RemoteTypeface? typeface
  RemotePaint.Companion Companion
  operator RemotePaint invoke(optional RemotePaint.() -> Unit init)

Reference

Other stories of this component

More in Canvas