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

spring-animation-keyframes

v0.3.0

Published

Generates a keyframe list to be used inside the css `@keyframes` rule, using physically based spring motion.

Downloads

3,362

Readme

Spring animation keyframes

Generates a keyframe list to be used inside the css @keyframes rule, using physically based spring motion.

Similar and inspired by react-spring, but with the objective of not relying on a Javascript runtime. That has the advantage of a much more performant animation, even when running blocking javascript. As the keyframes are pre-generated, this library does not work for dynamic animations, for example involving drag-and-drop.

Install

yarn add spring-animation-keyframes

Usage

import { generateKeyframes, presets } from 'spring-animation-keyframes'

const keyframes = generateKeyframes([
  {
    tension: 180,
    friction: 12,
    from: 0,
    to: 100,
    unit: 'px',
    property: 'translateX',
  },
  {
    ...presets.stiff, // presets include tension and friction.
    from: 1,
    to: 0,
    property: 'opacity',
  },
],
{
  // Length of the animation. Note that this does not change the animation,
  // it only defines how many seconds of animation to generate. It should
  // be a value sufficiently long so that the "spring" had time to settle.
  // For non-extreme values of tension and friction, the default should be
  // enough.
  time: 0.8 // default: 1
})

This will generate something like:

0% {
  transform: translateX(0px);
  opacity: 1;
}

1% {
  transform: translateX(1px);
  opacity: 0.99;
}

2% {
  transform: translateX(3px);
  opacity: 0.95;
}

...

When using this animation, make sure to use linear interpolation and a duration of the same value of the time argument of generateKeyframes, like this:

animation: spring-animation 0.8s linear;

Example with styled-components

const appearBottom = keyframes`${
  generateKeyframes([
    {
      tension: 180,
      friction: 20,
      from: 30,
      to: 0,
      unit: 'px',
      property: 'translateY',
    },
    {
      tension: 200,
      friction: 40,
      from: 0,
      to: 1,
      property: 'opacity',
    },
  ])
}`

export const Wrapper = styled.div`
  animation: ${appearBottom} 1s linear;
`

Presets

default: { tension: 170, friction: 26 }

gentle: { tension: 120, friction: 14 }

wobbly: { tension: 180, friction: 12 }

stiff: { tension: 210, friction: 20 }

slow: { tension: 280, friction: 60 }

molasses: { tension: 280, friction: 120 }

Theory

All the animations produced by this library follow the following spring equations:

f(0) = x0
f'(0) = v0
f''(t) = -k*(f(t) - x1) - μ*f'(t)

f(t): position
f'(t): velocity
f''(t): acceleration
μ: friction coeficient
v0: starting velocity
k: stiffness/tension
x0: starting position
x1: end position

What this library outputs are the values of f(t), by using a pre-calculated solution to the differential equation.