Remote Compose Storybook Русский GitHub

RemoteFloat expressions · Math functions

y = f(x) plotted from 61 player-evaluated expressions.

RemoteFloat expressions · Math functions: 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

Calculations that run on the device, every frame if needed, so things can move or resize from math or time without new data.

Good for

  • Animated charts and waves
  • Sizes computed from other sizes

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.RemoteFloat
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.abs
import androidx.compose.remote.creation.compose.state.acos
import androidx.compose.remote.creation.compose.state.asin
import androidx.compose.remote.creation.compose.state.atan
import androidx.compose.remote.creation.compose.state.atan2
import androidx.compose.remote.creation.compose.state.cbrt
import androidx.compose.remote.creation.compose.state.ceil
import androidx.compose.remote.creation.compose.state.clamp
import androidx.compose.remote.creation.compose.state.copySign
import androidx.compose.remote.creation.compose.state.cos
import androidx.compose.remote.creation.compose.state.exp
import androidx.compose.remote.creation.compose.state.floor
import androidx.compose.remote.creation.compose.state.lerp
import androidx.compose.remote.creation.compose.state.ln
import androidx.compose.remote.creation.compose.state.log
import androidx.compose.remote.creation.compose.state.mad
import androidx.compose.remote.creation.compose.state.max
import androidx.compose.remote.creation.compose.state.min
import androidx.compose.remote.creation.compose.state.pow
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.round
import androidx.compose.remote.creation.compose.state.sign
import androidx.compose.remote.creation.compose.state.sin
import androidx.compose.remote.creation.compose.state.sqrt
import androidx.compose.remote.creation.compose.state.tan
import androidx.compose.remote.creation.compose.state.toDeg
import androidx.compose.remote.creation.compose.state.toRad
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

/**
 * Applies the named RemoteFloat function to x. Every case is an expression the player evaluates.
 */
fun remoteFunction(name: String, x: RemoteFloat): RemoteFloat =
    when (name) {
        "abs" -> abs(x)
        "acos" -> acos(clamp(x, (-1f).rf, 1f.rf))
        "asin" -> asin(clamp(x, (-1f).rf, 1f.rf))
        "atan" -> atan(x)
        "atan2" -> atan2(x, 1f.rf)
        "cbrt" -> cbrt(x)
        "ceil" -> ceil(x)
        "clamp" -> clamp(x, (-0.5f).rf, 0.5f.rf)
        "copySign" -> copySign(0.5f.rf, x)
        "cos" -> cos(x * 3f.rf)
        "exp" -> exp(x) / 3f.rf
        "floor" -> floor(x)
        "lerp" -> lerp((-1f).rf, 1f.rf, (x + 1f.rf) / 2f.rf)
        "ln" -> ln(abs(x) + 0.05f.rf) / 3f.rf
        "log" -> log(abs(x) + 0.05f.rf)
        "mad" -> mad(x, 0.5f.rf, 0.25f.rf)
        "max" -> max(x, 0f.rf)
        "min" -> min(x, 0f.rf)
        "pow" -> pow(x, 2f.rf)
        "round" -> round(x * 2f.rf) / 2f.rf
        "sign" -> sign(x)
        "sqrt" -> sqrt(abs(x))
        "tan" -> tan(x) / 3f.rf
        "toDeg" -> toDeg(x) / 180f.rf
        "toRad" -> toRad(x * 90f.rf)
        else -> sin(x * 3f.rf)
    }

@Composable
@RemoteComposable
fun FloatFunctionPlot(function: String, animate: Boolean, color: Color) {
    RemoteCanvas(modifier = RemoteModifier.size(320.rdp, 220.rdp)) {
        val axis = RemotePaint {
            this.color = Color(0x44000000).rc
            style = RemotePaintingStyle.Stroke
            strokeWidth = 1f.rf
        }
        drawLine(axis, RemoteOffset(0f.rf, height / 2f.rf), RemoteOffset(width, height / 2f.rf))
        drawLine(axis, RemoteOffset(width / 2f.rf, 0f.rf), RemoteOffset(width / 2f.rf, height))
        val dot = RemotePaint { this.color = color.rc }
        val shift = if (animate) sin(RemoteTimeVariables.continuousSeconds) * 0.5f.rf else 0f.rf
        // 61 samples of y = f(x) across the plot's x range [-1, 1] (shifted over time when
        // animated);
        // the player computes each y.
        for (i in 0..60) {
            val x = -1f + i / 30f
            val y = remoteFunction(function, x.rf + shift)
            drawCircle(
                dot,
                radius = 3f.rf,
                center =
                    RemoteOffset(
                        width * ((x + 1f) / 2f).rf,
                        height / 2f.rf - y * (height / 2.4f.rf),
                    ),
            )
        }
    }
}

API

creation.compose.state.RemoteFloat

class RemoteFloat extends BaseRemoteState<java.lang.Float>
  RemoteFloat createNamedRemoteFloat(String name, RemoteFloat defaultValue, optional RemoteState.Domain domain)
  RemoteFloat createNamedRemoteFloat(String name, float defaultValue, optional RemoteState.Domain domain)
  operator RemoteFloat div(RemoteFloat other)
  RemoteBoolean isEqualTo(RemoteFloat other)
  RemoteBoolean isGreaterThan(RemoteFloat other)
  RemoteBoolean isGreaterThanOrEqualTo(RemoteFloat other)
  RemoteBoolean isLessThan(RemoteFloat other)
  RemoteBoolean isLessThanOrEqualTo(RemoteFloat other)
  RemoteBoolean isNotEqualTo(RemoteFloat other)
  RemoteFloat min(RemoteFloat other)
  operator RemoteFloat minus(RemoteFloat other)
  operator RemoteFloat plus(RemoteFloat other)
  operator RemoteFloat rem(RemoteFloat other)
  operator RemoteFloat times(RemoteFloat other)
  RemoteInt toRemoteInt()
  RemoteString toRemoteString()
  operator RemoteFloat unaryMinus()
  Float? constantValueOrNull
  RemoteFloat.Companion Companion
  RemoteFloat createNamedRemoteFloat(String name, RemoteFloat defaultValue, optional RemoteState.Domain domain)
  RemoteFloat createNamedRemoteFloat(String name, float defaultValue, optional RemoteState.Domain domain)
RemoteFloat RemoteFloat(float value)

creation.compose.state.RemoteFloatOperations

class RemoteFloatOperations
  RemoteFloat abs(RemoteFloat a)
  RemoteFloat acos(RemoteFloat a)
  RemoteFloat asin(RemoteFloat a)
  RemoteFloat atan(RemoteFloat a)
  RemoteFloat atan2(RemoteFloat a, RemoteFloat b)
  RemoteFloat cbrt(RemoteFloat a)
  RemoteFloat ceil(RemoteFloat a)
  RemoteFloat clamp(RemoteFloat value, RemoteFloat min, RemoteFloat max)
  RemoteFloat copySign(RemoteFloat a, RemoteFloat b)
  RemoteFloat cos(RemoteFloat a)
  RemoteFloat exp(RemoteFloat a)
  RemoteFloat floor(RemoteFloat a)
  RemoteFloat lerp(RemoteFloat from, RemoteFloat to, RemoteFloat tween)
  RemoteFloat ln(RemoteFloat a)
  RemoteFloat log(RemoteFloat a)
  RemoteFloat mad(RemoteFloat a, RemoteFloat b, RemoteFloat c)
  RemoteFloat max(RemoteFloat a, RemoteFloat b)
  RemoteFloat min(RemoteFloat a, RemoteFloat b)
  RemoteFloat pow(RemoteFloat a, RemoteFloat b)
  RemoteFloat round(RemoteFloat a)
  RemoteFloat sign(RemoteFloat a)
  RemoteFloat sin(RemoteFloat a)
  RemoteFloat sqrt(RemoteFloat a)
  RemoteFloat tan(RemoteFloat a)
  RemoteFloat toDeg(RemoteFloat a)
  RemoteFloat toRad(RemoteFloat a)

creation.compose.state.RemoteTimeVariables

class RemoteTimeVariables
  RemoteFloat animationDeltaTime
  RemoteFloat animationTime
  RemoteFloat continuousSeconds
  RemoteInt dayOfMonth
  RemoteInt dayOfWeek
  RemoteInt dayOfYear
  RemoteInt epochSecond
  RemoteInt hourOfDay
  RemoteInt minuteOfDay
  RemoteInt month
  RemoteInt secondOfHour
  RemoteInt utcOffsetSeconds
  RemoteInt year
  RemoteTimeVariables INSTANCE

Reference

Other stories of this component

More in State & expressions