Signals

Loading Ebi…
mut level = 0.25
let track_width = 252pt
sig bar_width = track_width * level
sig diameter = 32pt + 88pt * level
sig angle = 180deg * level

<Box flex-col items-center gap-12 x_align={Align.Center} y_align={Align.Center}>
    <Box width={track_width} h-120>
        <Box absolute width={diameter} height={diameter} rounded-full bg-green3
            position={[-25%, 0%]} x_align={Align.Center} y_align={Align.Center} />
        <Box absolute w-84 h-54 rounded-6 bg-orange2 rotation={angle}
            position={[25%, 0%]} x_align={Align.Center} y_align={Align.Center} />
    </Box>
    <Text font_size={36pt} font_weight={650} fill="light1">{level:.0%}</Text>
    <Box width={track_width} h-8 rounded-full bg-dark3>
        <Box absolute width={bar_width} h-full rounded-full bg-blue3
            x_align={Align.Start} y_align={Align.Center} />
    </Box>
</Box>

animate(level, to=0.9, duration=1.4s, ease="smootherstep")
animate(level, to=0.25, duration=1.4s, ease="smootherstep")

Change the target level to see the circle zoom, the rectangle turn, and the progress meter update together.

Bindings and reactivity

  • let stores a value as it is now.
  • mut creates a value you can assign to or animate.
  • sig derives a value that updates when its inputs change.

Element attributes keep live expressions. Use let saved = level when you need a snapshot. Signal expressions must be pure, and two animations cannot write to the same input at the same time.

Signals in functions

fn<sig> twice(sig value: Number): sig Number {
    return value * 2
}

mut count = 1
sig doubled = twice(count)
let saved = doubled
count = 2
assert(doubled == 4)
assert(saved == 2)

A regular parameter receives the current value. Use sig on a parameter to keep it live and on the return type to return a live value. A signal parameter also accepts a constant. Write : sig List<Number> to return a live list.

Use reflected inputs to let people change a value in Shrimply, or curves to animate a signal through keyframes.