Skip to content

Programmatic API

Declarative animation covers the common case: describe the state, let the change drive the motion. Some things do not fit that shape - a timeline whose steps overlap, a value that is not a DOM property, an animation you need to pause halfway. BmotionAnimateService is the imperative half of the library: animate by CSS selector or ElementReference, with no <Bmotion> wrapper anywhere in sight, and get back playback controls you can hold on to.

Animating by selector

AnimateAsync(selector, target, transition, stagger) resolves every matching element and animates them together, with the optional stagger spreading their start times. The call returns BmotionAnimationControls: awaitable for completion, and holding Pause, Play, SetSpeed, Stop (freeze where it is) and Complete (jump to the end). It also knows where it is - Duration, Time, Progress and State - and Seek moves it: the slider below scrubs the run. Start the slow run and then interfere with it.

idle

One element, by id or by reference

A selector can name one element as easily as many. Give a <Bmotion> a stable Id and "#id" reaches it from anywhere - from code, from your own CSS, from a test - instead of the generated id it otherwise gets. An element with no Bmotion around it at all is reached through an ElementReference: capture it with @ref and hand it to AnimateAsync, which registers it with the engine on first use. Both return the same controls a selector does.

<Bmotion Id="promo-card">
a plain <div @ref>

Animating a number

Nothing about the engine requires a DOM element. AnimateAsync(from, to, onUpdate, transition) interpolates a bare double and hands you every value on the way - which is how you get a counter that eases rather than ticks, and equally how you would drive a canvas, an audio gain or anything else outside the render tree.

0 interpolated, not incremented

Sequences

A BmotionSequence is a timeline: each step names its own target, and at places it relative to what came before. "+0.2" and "-0.1" are offsets from the previous step's end (so a negative value overlaps it), "<" aligns with the previous step's start, a bare number is absolute, and Label marks a position several later steps can share. That is what lets a sequence overlap without you hand-computing a single delay.

The whole timeline runs on one playhead, driven by the animation clock rather than by wall-clock timers. So the controls it returns govern the gaps between the steps as well as the steps themselves: pause it mid-run and the sequence genuinely holds instead of quietly starting its later steps on time, and SetSpeed(3) compresses the silences by the same factor it compresses the movement.

Motion values

A motion value is a number that other things can watch. Bind one with the Values parameter and it writes to the DOM every frame without re-rendering the component; derive from it with Transform and the derived value updates in lockstep; wrap it in Motion.Spring and you get a follower that lags behind with physics rather than tracking exactly. The slider below sets one value - everything else on the row is downstream of it.

bound directly
spring follower
Transform → 0-360°
Value 20 · MapRange(0..100 → 0..1) 0.20
Clamp(25, 75) 25 · Wrap(0, 40) 20 · Mix(-60, 60, v/100) -36

Read a value back with Value, or Subscribe to every change - the readout above does, throttled to ten renders a second, and the returned handle unsubscribes when disposed. Motion.AnimateAsync(value, to, transition) animates the value itself, and every binding downstream follows; a spring picks up the value's current velocity. Jump teleports it instead, with no velocity for a spring to carry on. The Bm helpers are the small maths motion values are made of: MapRange maps a number from one range onto another - what Transform does continuously - Clamp bounds it, Wrap loops it round a range, which is the infinite-carousel helper, and Mix interpolates between two numbers.

Driving one element by reference

When the element is a <Bmotion>, a @ref gives you the same controls without a selector. AnimateAsync plays a one-off target over whatever is running, Set applies one instantly with no animation (SetAsync also writes it to the element before returning), Pause / Resume / SetPlaybackRate control whatever is currently running, and Stop freezes it where it stands - all of it, or only the properties you name. Every async call here and on the service takes a CancellationToken, which stops the animation when cancelled.

Global defaults

BmotionConfig cascades defaults to everything beneath it, which is the cheapest way to change the feel of a whole surface at once. Transition is the default for every element under it that doesn't set its own - the right-hand box below has none, so it plays the config's spring. TransitionSpeed is a playback multiplier - 0.25 is the slow-motion switch every animation developer wants while tuning, and 0 makes every transition instant. It also carries ColorSpace (see Color) and ReduceMotion, the prefers-reduced-motion policy for its subtree — covered in full on the Accessibility page.

All three inherit the speed below; the last inherits the config's spring too
Bmotion 0 el · 0 active ▸
An unhandled error has occurred. Reload 🗙