Quick and easy spring animation. Works with other animation libraries (gsap, animejs, framer motion, motion one, @okikio/animate, etc...) or the Web Animation API (WAAPI).

Overview

spring-easing

Open Bundle

NPM | Github | Docs | Licence

Quick and easy spring animations. Works with other animation libraries (gsap, animejs, @okikio/animate, motion one, framer motion, etc...) or the Web Animation API (WAAPI), you can learn more in the Usage section.

spring-easing works by generating arrays of frame's which when placed in linear order creates a smooth spring like animation.

A frame represent a single frame of an animation

Note: the spring-easing package also supports 4 extra variants of spring, namely spring-in, spring-out, spring-out-in, and spring-in-out, you can use these easing to create some really unique spring like animations.

You can create animation's like this with spring-easing,

A demo of the various spring-easings available

Check out the spring easing variants on Codepen.

Attention: This entire library is a lightweight version of the CustomEasing implemented in @okikio/animate, which supports only string and number interpolation. If you'd like the complete CustomEasing with color interpolation, complex value interpolation, and more, go through the source code as a Github Gist, which is licensed under the MIT license.


## Installation
npm install spring-easing
Others
yarn add spring-easing

or

pnpm install spring-easing

Usage

import { SpringEasing } from "spring-easing";
// or
import SpringEasing from "spring-easing";

You can also use it directly through a script tag:

<script src="https://unpkg.com/spring-easing" type="module"></script>
<script type="module">
    // You can then use it like this
    const { SpringEasing } = window.SpringEasing;
</script>

You can also use it via a CDN, e.g.

import SpringEasing from "https://cdn.skypack.dev/spring-easing";
// or
import SpringEasing from "https://cdn.jsdelivr.net/npm/spring-easing";
// or any number of other CDN's

Use with Animation Libraries

Note: I cannot guarantee that every animation library works with spring-easing, for example, if an animation library doesn't support array values as keyframes, it won't work well with spring-easing.

The libraries that have been tested are:

Animation Library Support Demo
GSAP Yes - Wrap Method Codepen
animejs Yes - Array Keyframes Codepen
Framer Motion Yes - Array Keyframes Codepen
Motion One Yes - Array Keyframes Codepen
@okikio/animate Yes - Array Keyframes Codepen
Web Animation API (WAAPI) Yes - Array Keyframes Codepen

e.g.

import anime from "animejs";
import { SpringEasing, SpringOutFrame } from "spring-easing";

// Note: this is the return value of `SpringEasing` and `GenerateSpringFrames`
let [translateX, duration] = SpringEasing([0, 250], {
    easing: "spring-out-in(1, 100, 10, 0)",
    // You can change the size of Array for the SpringEasing function to generate
    numPoints: 200,
    // The number of decimal places to round, final values in the generated Array
    // This option doesn't exist on `GenerateSpringFrames`
    decimal: 5,
});

anime({
    targets: "div",

    // Using spring easing animate from [0 to 250] using `spring-out-in`
    translateX,

    // You can interpolate between strings
    // You can set the easing without an easing options object
    // You can interpolate between more than 2 values
    // Remember the `0` index of `SpringEasing` is an array of spring animation keyframes
    rotate: SpringEasing(
        ["0turn", 1, 0, 0.5],
        [SpringOutFrame, 1, 100, 10, 0]
    )[0],

    // TIP... Use linear easing for the proper springy effect
    easing: "linear",

    // The optimal duration for this specific spring configuration, e.g. mass, velocity, damping, etc...
    duration,
});

Note: make sure to read the comments above, as they are valuable resources for understanding what is happening.

Check out this demo on Codepen

Showcase

A couple sites/projects that use spring-easing:

  • Your site/project here...

API

What's New...

NEW SpringEasing now support interpolating between strings. It treats the units of the first value as the units for the rest of the values to interpolate between. e.g.

SpringEasing(["0turn", "1px", "18rem", "125deg", 25], ...)

Important All the values above get transformed to ["0turn", "1turn", "18turn", "125turn", "25turn"], before being interpolated.

NEW interpolateStrings, interpolateUsingIndex, and interpolateComplex, are now built-in, they allow for supporting string keyframes.

NEW Custom interpolation functions are now supported. e.g.

import { interpolateNumber } from "spring-easing";
// ...
export const interpolateColor = (t, values, decimal) => {
    return transpose(...values.map((v) => toRGBAArr(v))).map(
        (colors, i) => {
            let result = interpolateNumber(t, colors);
            return i < 3 ? Math.round(result) : toFixed(result, decimal);
        }
    );
};

SpringEasing(["red", "green", "#4f4"], "spring", interpolateColor)

Important The logic for color interpolation is defined in this Github Gist.

The API of spring-easing is pretty straight forward, the SpringEasing function generates an array of values using a frame functions, which in turn creates the effect of spring easing.

To use this properly make sure to set the easing animation option to "linear". Check out a demo of SpringEasing at https://codepen.io/okikio/pen/MWEdzNg

SpringEasing has 3 properties they are easing (all the easings from EasingFunctions are supported on top of frame functions like SpringFrame, SpringFrameOut, etc..), numPoints (the size of the Array the frame function should create), and decimal (the number of decimal places of the values within said Array).

Properties Default Value
easing spring(1, 100, 10, 0)
numPoints 50
decimal 3

By default, Spring Easing support easings in the form,

constant accelerate decelerate accelerate-decelerate decelerate-accelerate
spring / spring-in spring-out spring-in-out spring-out-in

All Spring easing's can be configured using theses parameters,

spring-*(mass, stiffness, damping, velocity)

Each parameter comes with these defaults

Parameter Default Value
mass 1
stiffness 100
damping 10
velocity 0

To understand what each of the parameters of SpringEasing mean and how they work I suggest looking through the SpringEasing API Documentation

Note: the return value of the SpringEasing function is actually [Array of keyframes , duration], in that order.

For a full understanding of what is happening in the library out the API site for detailed API documentation.

Browser Support

Chrome Edge Firefox Safari IE
4+ 12+ 4+ 4+ 10+

Native support for spring-easing is great as it doesn't use any browser specific or nodejs specific API's, you should be good to use spring-easing in any environment.

Contributing

I encourage you to use pnpm to contribute to this repo, but you can also use yarn or npm if you prefer.

Install all necessary packages

npm install

Then run tests

npm test

Build project

npm run build

Preview API Docs

npm run typedoc && npm run preview

Note: this project uses Conventional Commits standard for commits, so, please format your commits using the rules it sets out.

Licence

See the LICENSE file for license rights and limitations (MIT).

You might also like...

Snowfall effect written in pure JavaScript. No additional libraries, no dependencies. Works in every modern browser.

pureSnow.js Snow falling slowly on a winter night. Probably the most calming and peaceful snowfall effect written in pure JS/CSS. (No SCSS). Inspired

Dec 29, 2022

This fictive project was created to showcase my current skills using Three.js and GSAP.

This fictive project was created to showcase my current skills using Three.js and GSAP.

Bike Demo Three.js This fictive project was created to showcase my current skills using Three.js and GSAP. It was inspired by the tutorial created by

Dec 29, 2022

A template for buildind scrollable landing pages with Gsap, ScrollTrigger and webgi engine in typescript using parcel bundler.

A template for buildind scrollable landing pages with Gsap, ScrollTrigger and webgi engine in typescript using parcel bundler.

Threejs + GSAP + WEBGi 100% Free Course This is a template used in my fast course "building scrolable pages with ScrollTrigger and Threejs" for a vani

Dec 17, 2022

Hacktoberfest is all about meeting up all brains. In this repository we are planning to come with many ideas and works. You all can share your ides/works here.

Hacktoberfest is all about meeting up all brains. In this repository we are planning to come with many ideas and works. You all can share your ides/works here.

Hacktoberfest Submit your Work Hacktoberfest is all about meeting up all brains. In this repository we are planning to come with many ideas and works.

Oct 5, 2022

I made countdown birthday and fireworks animation using HTML Canvas, CSS, JS. The fireworks animation gonna come out once the countdown is finished or in other words, "Birthday Time".

Countdown-Birthday-Fireworks-Animation I made countdown birthday and fireworks animation using HTML Canvas, CSS, JS. The fireworks animation gonna com

Dec 31, 2022

Some ideas for grid to slideshow switch animations using GSAP's Flip plugin.

Some ideas for grid to slideshow switch animations using GSAP's Flip plugin.

Grid to Slideshow Switch Animations Some ideas for grid to slideshow switch animations using GSAP's Flip plugin. Article on Codrops Demo Installation

Jun 22, 2023

An easy way to animate SVG elements.

Walkway I loved the animations for the polygon ps4 review a few months back and decided to create a small library to re-create them (simple demo). It

Jan 2, 2023

Fullstack Spotify clone app using NextJS and many other libraries.

Fullstack Spotify clone app using NextJS and many other libraries.

Spotify Clone Spotify clone made with NextJS on both the client and server sides. For the database, I utilized Postgresql and Prisma ORM. I used Chakr

Dec 7, 2022

Download all Moodle files with one click. This is a Chrome extension built to save time and effort from downloading files manually one by one!

Download all Moodle files with one click. This is a Chrome extension built to save time and effort from downloading files manually one by one!

Moodle Downloader Extension Moodle downloader extension for Chrome. The extension is tested with both the TUM moodle and the official moodle demo. Not

Nov 15, 2022
Comments
  • [BUG] Latest publish broke my code by changing the API of interpolateNumber without bumping the major version

    [BUG] Latest publish broke my code by changing the API of interpolateNumber without bumping the major version

    Describe the bug interpolateNumber API has changed

    From: export function interpolateNumber (t: number, values: number[], decimal = 3) { To: export function interpolateNumber(frames: number[], values: number[], decimal = 3) {

    To Reproduce Steps to reproduce the behavior:

    1. Run an app that still uses the old API

    Expected behavior Without a major version bump I expect interpolateNumber to not have a breaking change. I see the idea was to batch interpolate numbers.

    If that's the case I think adding a new method interpolateNumbers implement the new behaviour and possibly having the old function call out to it.

    bug released 
    opened by allain 7
Releases(v2.1.0)
Owner
Okiki Ojo
Independent Web Developer, Maintainter on @withastro + Creator of https://bundle.js.org. My student projects are located at @okikio-school
Okiki Ojo
A react and framer motion app to play some quizzes !

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: ya

Belkacem Berras 2 Jan 7, 2022
My personal website built with Next.js, TypeScript, twin.macro, Framer Motion, MDX and deployed on Vercel.

chrvaskos.com My personal website / blog built with some of my favorite technologies where I can showcase my work and write articles about anything ne

Vasilis Christoforidis 4 Mar 25, 2022
Portfólio de projetos pessoais feito com NextJs e lib de animação Framer Motion

Portfólio - Carlos Amorim Esse portfólio foi criado para mostrar meus projetos e habilidades. ?? Projeto criado com as seguintes tecnologias: ✔️ NextJ

Carlos Amorim 13 May 12, 2022
Careers page made with Next.js, Framer Motion & Tailwind CSS

Careers Page Tech Stack: Next.js / Framer Motion / Tailwind CSS This is a Next.js project bootstrapped with create-next-app. Getting Started First, ru

Jack Latimer 7 Nov 16, 2022
spartacus是一个基于Spring Boot 2.3.x、Spring Cloud Hoxton.SR5、Spring Security 2.3.x、OAuth2.0、Python3等开源框架构建的分布式系统

spartacus是一个基于Spring Boot 2.3.x、Spring Cloud Hoxton.SR5、Spring Security 2.3.x、OAuth2.0、Python3等开源框架构建的分布式系统,亦是一个功能完备的微服务脚手架。

xlvchao 108 Dec 24, 2022
Build Schema.org graphs for JavaScript Runtimes (Browser, Node, etc). Improve your sites SEO with quick and easy Rich Results.

schema-org-graph-js The quickest and easiest way to build Schema.org graphs for JavaScript Runtimes (Browser, Node, etc). Status: ?? In Development Pl

Harlan Wilton 17 Dec 21, 2022
A jQuery plugin that works in harmony with animate.css in order to enable animations only when content comes into view.

jQuery AniView A jQuery plugin that works in harmony with animate.css in order to enable animations only when content comes into view. Now supports v4

Jonathan James Cosgrove 216 Sep 10, 2022
Vite plugin to client bundle i18next locales composited from one to many json/yaml files from one to many libraries. Zero config HMR support included.

vite-plugin-i18next-loader yarn add -D vite-plugin-i18next-loader Vite plugin to client bundle i18next locales composited from one to many json/yaml f

AlienFast 4 Nov 30, 2022
useOverlay uses floating-ui and framer-moting under the hood, giving you an API that provides great usability.

Create floating things easily useOverlay uses floating-ui and framer-moting under the hood, giving you an API that provides great usability. Warning D

Nedim Arabacı 8 Oct 24, 2022