Skip to content

Simple spring dynamics for physics-driven animations

License

Notifications You must be signed in to change notification settings

asbjornh/tiny-spring

Repository files navigation

tiny-spring

npm version

Simple spring dynamics for physics-driven animations. Also it's tiny (2kB before minification and gzip).

npm install tiny-spring

This small library helps you interpolate values using spring dynamics. There's no DOM-stuff or framework specifics included so you can use it with whatever framework or library you want.

If you're using React.js, there is use-spring-effect which uses tiny-spring internally.

Browser support

tiny-spring uses requestAnimationFrame, so if you need to support older browsers make sure you use a polyfill.

TLDR usage

import Spring from 'tiny-spring';

const spring = Spring();
const element = document.getElementById('some-id');

spring.onUpdate(val => {
  element.style.transform = `translateX(${val}px)`;
});

// Set value to 50 without animating
spring.setValue(50);

// Transition from current value (50) to 100
spring.transitionTo(100);

// Transition again from whatever the current value is to 200
spring.transitionTo(200);

// Stop animation and remove callback
spring.destroy();

API

Spring(initialValue: number, config: SpringConfig): spring

A factory function that returns a new spring object.

SpringConfig (object)

stiffness: number = 200

Stiffness controls how "fast" your animation will be. Higher values result in faster motion.

damping: number = 10

Damping controls how much friction is applied to the spring. You can think of this as how "wobbly" the resulting motion is. Lower values result in more wobblyness.

precision: number = 100

Used to determine when to stop animating. With a precision of 0 the spring will reach its end value immediately. With really high values it might keep animating fractions of a pixel for a long time. Tweak this value if animations end abruptly or linger for too long. When tweaking this value you'll want to make big changes in order to see an effect (like adding/removing zeros).

spring (object)

onUpdate(fn: (value: number): void)

Add a callback which will be called every time the value of the spring changes.

NOTE: spring only supports one callback, so calling this method more than once results in previously attached callbacks being overwritten.

spring.onUpdate(value => {
  /* do something with value */
});

onRest(fn: (value: number): void)

Add a callback which will be called when the spring comes to a rest.

NOTE: spring only supports one callback, so calling this method more than once results in previously attached callbacks being overwritten.

spring.onRest(value => {
  /* do something with value */
});

setValue(value: number)

Set the value of the spring without animating.

transitionTo(value: number)

Start animating to value from the current value of the spring.

destroy()

Aborts animation and removes the callback.

About

Simple spring dynamics for physics-driven animations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published