Primitives / foldkit-primitives/motion

Spring

const · packages/primitives/src/motion/spring.ts:41

Args{ from: number, to: number, stiffness: number, damping: number }
Model{ value: number, velocity: number, running: boolean }
MessagesStarted, Stopped, Ticked { value: number, velocity: number }, Finished { value: number }
Its full type
const Spring: Bundle.Bundle<
  'Spring',
  {
    readonly from: number
    readonly to: number
    readonly stiffness: number
    readonly damping: number
  },
  {
    readonly value: number
    readonly velocity: number
    readonly running: boolean
  },
  | { readonly _tag: 'Started' }
  | { readonly _tag: 'Stopped' }
  | {
      readonly _tag: 'Ticked'
      readonly value: number
      readonly velocity: number
    }
  | { readonly _tag: 'Finished'; readonly value: number },
  never,
  never,
  never,
  void,
  {},
  {}
>

A spring as a bundle: a number pulled toward a target with stiffness and damping, in the Model. Semi-implicit Euler at a fixed 16ms step, so the trajectory is deterministic under TestClock; the stream ends with Finished, and the value rests exactly at to. Underdamped springs overshoot — that is the point — but the rest value is still exact.

In the guides