carlsson87/spring/1.0.0

NOTE: the latest version is 1.0.1

Spring

A simple spring simulation for smooth, physically-inspired animations.

This module provides a spring model that moves a value toward a target over time, based on configurable tension and friction. It can be used to animate values like positions, sizes, or any other continuous quantity.

Spring

type Spring

A Spring represents a value that moves toward a target using spring dynamics.

It holds the current value, the target value, and the velocity of the spring.

{ value : Float
, target : Float
}
-> Spring

Create a new Spring given an initial value and a target.

The initial velocity will be zero.

create { value = 0, target = 100 }
update : Config -> Float -> Spring -> Spring

Step the spring simulation forward by a given number of milliseconds.

Applies the configured spring dynamics to update the value and velocity.

If the spring is close enough to equilibrium (as defined by the threshold in the config), it snaps to rest.

update myConfig 16 mySpring
value : Spring -> Float

Get the current value of a Spring.

value mySpring

Check if a Spring is at equilibrium.

This returns True if the value equals the target and the velocity is zero.

equilibrium mySpring

Configuration

type Config

Configuration for a spring simulation.

Includes:

  • tension: how strongly the spring is pulled toward the target
  • friction: how much resistance slows the spring down
  • equilibriumThreshold: how close is “close enough” to be considered settled
{ tension : Float
, friction : Float
}
-> Config

Create a basic Config using the given tension and friction values.

The equilibriumThreshold is set to a default of 0.05, which defines how close the spring’s value and velocity must be to the target and zero, respectively, before it is considered at rest.

config { tension = 100, friction = 15 }

Override the equilibriumThreshold of a given Config.

This can be useful if you want to change how strictly the spring checks for rest.

config { tension = 100, friction = 15 }
    |> setEquilibriumThreshold 0.01