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

mation

v0.0.2

Published

A small animation library

Downloads

177

Readme

Mation

Mation is a simple library to provide simple smooth animations. Animations can be interrupted or redirected, and still look natural.

Demo using mation with react-sticky

Demo using mation / react-mation with react-inform

Installation

npm install --save mation

Tutorial (Demo)

Lets make a very simple animation. Our goal is to have a button animate the growing and shrinking of a div.

To start lets make a growing and shrinking div without animations.

const { Mation, preset, spring } = mation;

const container = document.getElementById('container');
const button = document.getElementById('button');

function modifyContainer(dimensions) {
  container.style.height = `${dimensions}px`
  container.style.width = `${dimensions}px`;
}

let dimensions = 100;
button.onclick = () => {
  dimensions = dimensions > 100 ? 100 : 500;
  modifyContainer(dimensions);
};

modifyContainer(dimensions);

JSFiddle Link

Simple enough. Now lets let Mation manage the size of the box. To do this, we just create a new Mation, then whenever we want to change the dimensions of the box, we call Mation#moveTo. We also must listen for value changes using Mation#on.

...
let dimensions = 100;
const boxMation = Mation(dimensions);

button.onclick = () => {
  dimensions = dimensions > 100 ? 100 : 500;
  boxMation.moveTo(dimensions);
};

boxMation.on(modifyContainer);

JSFiddle Link

There are still no animations! Since you are moving directly to the dimensions, Mation jumps to that value. To create an animation, you can add a spring.

  boxMation.moveTo(spring(dimensions));

JSFiddle Link

Click that button, and watch the box move. You can click many times and the animation still looks natural as it changes courses!

Thats great, but we want that animation to be more wobbly. To do this, our spring takes a configuration value. This value is an object with damping and stiffness properties. There are presets provided with Mation for some nice effects. These presets include noWobble (the default), wobbly, gentle, and stiff.

  boxMation.moveTo(spring(dimensions, presets.wobbly));

JSFiddle Link

Much more wobbly, and spamming the button still doesn't cause the animation to jerk unnaturally.

Mation can handle more complex movements by accepting not just numbers, but also arrays and objects. Each entry can also have their own spring configurations.

function modifyContainer({width, height}) {
  container.style.height = `${height}px`
  container.style.width = `${width}px`;
}

let dimensions = 100;
const boxMation = Mation({width: dimensions, height: dimensions});

button.onclick = () => {
  dimensions = dimensions > 100 ? 100 : 500;
  boxMation.moveTo({
    width: spring(dimensions, presets.gentle),
    height: spring(dimensions, presets.wobbly)
  });
};

JSFiddle Link

Api

Mation(initialValue, [config])

Creates a new Mation. Initial value is the initial value of the animation. Config is the default config to use for all movements without springs. If left blank, movements without springs jump to the destination immediately. See this JSFiddle for an example.

A config object is just an object with numeric damping and stiffness properties.

Mation#moveTo(value)

Creates a movement with the new destination set to the passed value.

Mation#on(listener)

Registers a listener for value changes. The listener should take one argument.

Mation#off(listener)

Removes a listener previously registered for this Mation.

Mation#onSettle(listener)

Registers a listener that is called each time this Mation settles (stops moving).

Mation#offSettle(listener)

Removes a settle listener previously registered for this Mation.

Mation#onMove(listener)

Registers a listener that is called each time this Mation starts moving.

Mation.offMove(listener)

Removes a move listener previously registered for this Mation.

spring(destination, [config])

Creates a spring movement to be passed to Mation#moveTo. The config defaults to presets.noWobble.

presets

Provides preset config objects to be used with spring or Mation:

  • noWobble
  • wobbly
  • stiff
  • gentle

Thanks

License

MIT