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

dom-animate

v7.0.1

Published

Dead-simple helper function to do animations in DOM or other environments. Bezier curve support. Animations respect clock. Pause, resume, and cancel. Custom timing and render functions.

Downloads

149

Readme

dom-animate

Dead-simple helper function to perform any animation in the DOM or other environments.

Features:

  • Really small filesize, only 1 dependency (~2.5KB uncompressed, including dependency).
  • Straightforward, simple API.
  • Supports Beizer Curves and custom easing functions, with predefined values.
  • Custom timing function (uses RAF by default if available)
  • Pause/resume/stop/restart animations.
  • Supports delays that still work with pause/resume/stop/restart.
  • Animations respect the actual clock, so no matter the frame rate, the animation will still properly last the appropriate amount of time.
  • Lambda render functions.

Installation

NPM:

npm i dom-animate

browser:

You can use either dom-animate.umd.js or dom-animate.min.umd.js from the latest release in a script tag. This includes all dependencies.

<script src="dom-animate.min.umd.js"></script>
<script>
    let el = document.querySelector('.myElement');

    let animation = new DOMAnimate(0, 200, (x) => {
      el.style.height = x + 'px';
    });
</script>

Example Usage

const DOMAnimate = require('dom-animate');
const el = document.querySelector('.myElement');

//animate height from 0 to 200 with all defaults
const animation = new DOMAnimate(0, 200, x => {
  el.style.height = x + 'px';
});

//animate scale (with cross-browser support) from 1 to 2 with some options
const animation = new DOMAnimate(1, 2, x => {
  el.style.transform = `scale(${x}, ${x})`;
  el.style.webkitTransform = `scale(${x}, ${x})`;
}, {
  duration: 200,
  easing: [0.42, 0.0, 0.58, 1.0],
  onComplete: () => alert('done!')
});

//animate with pre-defined easing constant
const animation = new DOMAnimate(0, 200, x => {
  el.style.height = x + 'px';
}, {
  easing: DOMAnimate.EASING.LINEAR
});

//stops animation. `play() or resume()` both play from the beginning.
animation.stop();

//restarts animation after stopping
animation.play();

//pauses animation
animation.pause();

//resumes animation after pausing
animation.resume();

//don't animate right away. create animation object, then play after 1s
const animation = new DOMAnimate(0, 200, x => {
  el.style.height = x + 'px';
}, {
  autoplay: false
});
setTimeout(animation.play, 1000);

//provide a custom timing function instead of the default `window.requestAnimationFrame`
//in this example, it tries to render at exactly 24fps
const animation = new DOMAnimate(0, 200, (x) => {
  el.style.height = x + 'px';
}, {
  timingFunction: callback => { window.setTimeout(callback, 1000 / 24); }
});

Animation with delays

You can think of a delay as just a part of the animation. It is respected by the pause/resume functionality all the same.

const animation = new DOMAnimate(0, 1, x => {
    document.getElementById('id').style.opacity = x;
  }, {
    easing: [0.25, 0.46, 0.45, 0.9],
    delay: 1000 //1s
  });

animation.pause(); //delay is paused.
animation.resume(); //delay is resumed

/* without autoplay */

const animation = new DOMAnimate(0, 1, x => {
    document.getElementById('id').style.opacity = x;
  }, {
    easing: [0.25, 0.46, 0.45, 0.9],
    delay: 1000, //1s
    autoplay: false
  });

animation.play(); //delay starts now, along with rest of animation

API

constructor({number} start, {number} end, {function} lamda, {object=} options)

{int} start

The start value of the animation.

{int} end

The end value of the animation.

{function} lambda({number} x)

The function that sets the styles on frame update. x is a number that represents the current frame's animation value.

{object=} options

An optional map of parameters:

{boolean} autoplay

If true, the animation will start as soon as animation() is called. [Default: true]

{integer} duration

Animation duration in milliseconds. (Default: 400)

{array} easing

An array to pass to the cubic-bezier easing function. (Default: Animator.EASE_IN_OUT)

{function} onComplete

A callback function that is called when the animation is finished.

{function} timingFunction

Lambda function that is used to call the tick() method. By default, if in a browser environment, this will be window.requestAnimationFrame, window.webkitRequestAnimationFrame, etc. If your environment doesn't support these methods, and you don't provide your own, a default timing function will be used that is called at 60fps.

play()

Starts the animation.

stop()

Ends any current animation.

pause()

Pauses any current animation.

resume()

Resumes any current animation.

Constants

EASING

dom-animate ships with a small Bezier Curve library:

  • EASING.EASE
  • EASING.EASE_IN
  • EASING.EASE_OUT
  • EASING.EASE_IN_OUT
  • EASING.LINEAR

Additional Resources

License

MIT. Free to use in all your things!

Contribution

DO IT! PR's welcome. Need to add testing and linting.