npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tiny-spring

v1.0.0

Published

Simple spring dynamics for physics-driven animations

Downloads

10,083

Readme

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.