mwg API
    Preparing search index...

    Class Spring

    A damped chase spring for secondary motion: one bob pulled toward a moving anchor, the spring-damper complement to SpriteMotion (roadmap 424). Hair, hems, accessories and interface panels follow their anchor with lag, overshoot on a stop and settle; the anchor teleports (a head bone, a pointer, a panel edge) and the dynamics supply the lag, so reacting to an acceleration, a stop or an impact is one setAnchor call.

    Renderer-free arithmetic: semi-implicit Euler, no randomness, no engine, deterministic for a fixed update(dt) sequence. One update is O(1), about a dozen flops, which is the frame budget stated rather than hoped for.

    import { Spring } from '@datamoc/mwg/physics';

    const hair = new Spring({ stiffness: 120, damping: 8 });
    hair.setAnchor(10, 0); // the head moved; the bob is still behind
    hair.update(1 / 60);
    console.log(hair.offset().x > 0); // true - the bob lags behind the anchor
    for (let i = 0; i < 240; i++) hair.update(1 / 60);
    console.log(hair.settled(0.01)); // true - a damped spring comes to rest at its anchor
    Index
    damping: number
    stiffness: number
    • get anchorPosition(): { x: number; y: number }

      where the anchor currently stands

      Returns { x: number; y: number }

    • shove the bob by a delta without touching its velocity: an impact, a pluck, a shove

      Parameters

      • dx: number
      • dy: number

      Returns void

    • the bob minus the anchor: the lag a follower draws

      Returns { x: number; y: number }

    • put a display object where the bob stands; a SpriteMotion target qualifies

      Parameters

      • target: { x: number; y: number }

      Returns void

    • move the anchor; the bob follows through the dynamics, which is the whole effect

      Parameters

      • x: number
      • y: number

      Returns void

    • true once the lag and the velocity both fit inside tolerance

      Parameters

      • tolerance: number

      Returns boolean

    • Advance the bob by dt seconds. Under reduced motion a decorative spring snaps to its anchor instead of swinging, the same collapse SpriteMotion applies. Keep dt small (a FixedStepper at 60 Hz): past roughly two radians of natural frequency per step, semi-implicit Euler blows up instead of settling. A zero dt holds still; a negative or non-finite one throws a RangeError.

      Parameters

      • dt: number

      Returns void