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

step-motion

v1.0.0

Published

Basic animation function to animate any value or element with a step function

Downloads

6

Readme

Step Motion

NPM version Bundle size GitHub license

Basic animation function to animate any value or element with a step function. It uses requestAnimationFrame for the step function and the Bezier Easing package for easing functions. It is a simple, but very flexible and powerful package, because you can animate everything with it with no limits. It could also be used as a base for a more complex animation library.

Installation

npm i step-motion

or

yarn add step-motion

Usage

Import the animate() function from step-motion and apply a step function.

import animate from 'step-motion'

animate({
    step: (progress) => console.log(progress)
})

API

Parameters:

  • step … step function that is called on every frame of the animation, with a parameter progress … the eased progress (value between 0 and 1) of the animation (required)
  • duration … the duration of the animation in ms (default: 1000)
  • delay … the delay before the animation starts in ms (default: 0)
  • easing … the bezier easing array of the animation that describes the easing curve (default: [0, 0, 1, 1] which is linear) – more information here
  • start … callback function that is called as soon as the animation starts (optional)
  • stop … callback function that is called as soon as the animation stops (optional)
  • cancel … callback function that is called as soon as the animation is cancelled (optional)
  • enableReducedMotion … accessibility feature that prevents animations for users who prefer reduced motion on their OS (default: true)

Methods:

  • cancel … cancels the running animation
  • running … returns a boolean that indicates if the animation is running or not

Examples

Example 1 — Animate a value of an element with easing:

const element = document.querySelector('div');

animate({
    step: (progress) => {
        element.style.opacity = progress;
    },
    easing: [0.25, 0, 0.75, 1],
});

Example 2 — Same as above but a bit more complex step function:

const element = document.querySelector('div');
const startValue = 20;
const endValue = 80;
const diffValue = endValue - startValue;

animate({
    step: (progress) => {
        element.style.left = startValue + diffValue * progress;
    },
    easing: [0.25, 0, 0.75, 1],
});

Example 3 — Animate two values of an element and set a custom duration:

const element = document.querySelector('div');
const startValue = 20;
const endValue = 80;
const diffValue = endValue - startValue;

animate({
    step: (progress) => {
        element.style.opacity = 1 - progress;
        element.style.left = startValue + diffValue * progress;
    },
    duration: 500,
    easing: [0.75, 0, 0.25, 1],
});

Example 4 — Add a delay before the animation starts:

const element = document.querySelector('div');

animate({
    step: (progress) => element.style.opacity = progress,
    duration: 750,
    delay: 500,
    easing: [0.25, 0, 0.75, 1],
});

Example 5 — Add callbacks for starting, stopping or cancelling the animation:

const element = document.querySelector('div');

animate({
    step: (progress) => element.style.opacity = progress,
    duration: 750,
    delay: 500,
    easing: [0.25, 0, 0.75, 1],
    start: () => console.log('animation has started'),
    cancel: () => console.log('animation was cancelled'),
    stop: () => console.log('animation has stopped'),
});

Example 6 — Animate a value without changing DOM elements directly – interesting for manipulating reactive variables (vue, react, svelte, …):

let counter = 0;
const counterMax = 100;

animate({
    step: (progress) => {
        counter = counterMax * progress;
    },
    easing: [0.25, 0, 0.75, 1],
});

Example 7 — Cancelling an animation that is running with methods:

const element = document.querySelector('div');

const myAnimation = animate({
    step: (progress) => element.style.opacity = progress,
    cancel: () => console.log('animation was cancelled by a click of a button'),
});

const myButton = document.querySelector('button');

myButton.addEventListener('click', () => {
    if (myAnimation.running()){
        myAnimation.cancel();
        return;
    }
    console.log('animation is not running');
});

Example 8 — Disable prefers-reduced-motion accessibility feature:

let counter = 0;
const counterMax = 100;

animate({
    step: (progress) => counter = counterMax * progress,
    enableReducedMotion: false,
});