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

vq

v1.3.1

Published

A light-weight and functional animation helper for Velocity.js

Downloads

35

Readme

vq

npm version Build Status

A light-weight and functional animation helper for Velocity.js

Dependencies

vq requires Velocity.js. You have to load Velocity.js before using vq.

Installation

$ npm install vq

or download from release page.

Demo

Example

/* Animation behaviors
----------------------------------------*/
var fadeIn = {
  p: { opacity: [1, 0] },
  o: { duration: 500, easing: 'easeOutQuad' }
};

var fadeOut = {
  p: { opacity: [0, 1] },
  o: { duration: 500, easing: 'easeInQuad' }
};

/* Animation elements
----------------------------------------*/
var $foo = $('.foo');
var $bar = $('.bar');

/* Animation sequence
----------------------------------------*/
var seq = vq.sequence([
  vq($foo, fadeIn),
  vq($bar, fadeIn).delay(1000).stagger(30),
  vq($bar, fadeOut),
  vq($foo, fadeOut),
  function() { console.log('complete!') }
]);

seq();

API

vq provides global function vq(el, props, opts), vq.sequence(funcs) and vq.parallel(funcs).

vq(el, props, opts)

This function returns a function that executes animation by given element, property and options.

var el = document.getElementById('element');
var func = vq(el, { width: 400 }, { duration: 600 }); // generate a function
func(); // <-- The animation is executed on this time

You can combine the props and opts to Velocity-like single object.

var fadeIn = {
  p: {
    opacity: [1, 0]
  },
  o: {
    duration: 500
  }
};

var func = vq(el, fadeIn);
func();

You can pass a Velocity's progress callback to the 2nd argument or the value of p.

var swing = {
  p: function(el, t, r, t0, tween) {
    var offset = 100 * Math.sin(2 * tween * Math.PI);
    el[0].style.transform = 'translate3d(' + offset + 'px, 0, 0)';
  },
  o: {
    duration: 1000,
    easing: 'easeOutCubic'
  }
}

var func = vq(el, swing);
func();

The function receives 1st argument as completion callback. You can handle the animation completion from the callback;

var func = vq(el, props, opts);
func(function() {
  console.log('animation is completed');
});

vq.sequence(funcs)

This function receives the array of functions and returns a function to execute them sequentially. If the given function returns Promise object or has callback function as 1st argument, vq.sequence waits until the asynchronous processes are finished.

var seq = vq.sequence([
  function(done) {
    setTimeout(function() {
      console.log('1')
      done();
    }, 1000);
  },
  function() {
    return new Promise(function(resolve, reject) {
      setTimeout(function() {
        console.log('2');
        resolve();
      }, 500);
    });
  },
  function() { console.log('3'); }
]);

seq();
// The output order is 1 -> 2 -> 3

The function is useful with using vq(el, props, opts).

var animSeq = vq.sequence([
  vq(el1, animation1),
  vq(el2, animation2),
  vq(el3, animation3),
  function() {
    console.log('Three animations are completed');
  }
]);

vq.parallel(funcs)

This function is same as vq.sequence except to execute in parallel.

var para = vq.parallel([
  function(done) {
    setTimeout(function() {
      console.log('1')
      done();
    }, 1000);
  },
  function() {
    return new Promise(function(resolve, reject) {
      setTimeout(function() {
        console.log('2');
        resolve();
      }, 500);
    });
  },
  function() { console.log('3'); }
]);

para();
// The output order may be 3 -> 2 -> 1

vq.stop(els)

This function creates animation stopper for given elements. If you execute returned function, all animated elements is stopped.

var stopFn = vq.stop([el1, el2, el3]);

var seq = vq.sequence([
  vq(el1, animation1),
  vq(el2, animation2),
  vq(el3, animation3)
])();

setTimeout(stopFn, 3000); // stop `seq` animations after 3000msec

Event helpers

DOM Event helpers

vq can handle DOM events by event helpers and execute some animation after receiving the events.
vq has following event helpers.

  • click
  • dblclick
  • mousedown
  • mouseup
  • mousemove
  • mouseenter
  • mouseleave
  • focus
  • blur
  • change
  • input
  • scroll
  • load

The helper functions expect a DOM element for the argument and returns a new function. The returned function expect an animation function that you will get from vq, vq.sequence or vq.parallel functions.

var click = vq.click(window);

var fade = vq.sequence([
  click(vq(el, fadeIn)),
  click(vq(el, fadeOut)),
  function(done) { fade(done); }
]);

fade();

You can create a new event helper by using vq.element function. vq.element receives an element, a DOM event name and an optional filter function. If you specify the filter function, your event helper does not execute an animation function until the filter function returns true.

var helloWorld = vq.element(input, 'input', function(event) {
  return event.target.value === 'Hello World';
});

var seq = vq.sequence([
  helloWorld(vq(input, fadeOut)),
  // some other animations
]);

seq();

Delay Helper

vq.delay(msec) returns the function that delays given callback for msec. This function can use for not only animation function but also normal functions.

var delay1s = vq.delay(1000);

var delayedFn = delay1s(function() {
  console.log('This message is delayed 1sec');
});

delayedFn();

vq chainable helper methods

The function returned by vq(el, props, opts) has some chainable helper methods. The helpers can modify animation options and behaviors.

vq.sequence([
  vq(el1, animation1),
  vq(el2, animation2).delay(100).loop(3), // Add 100ms delay and three times loop
  vq(el3, animation3)
]);

.delay(msec)

Set the delay of animation start to msec.

.duration(msec)

Set the duration of animation to msec.

.easing(name)

Set the easing of animation to name. name should be easing function name that allowed on Velocity.js.

.progress(func, tween)

Set the progress function to func.

tween is optional argument and it is set to props.tween. [1, 0] is set if tween is omitted and props.tween is not set previously.

See Velocity.js documentation to learn tween property.

.display(value)

Set the display option to value.

.visibility(value)

Set the visibility option to value.

.loop(count)

Set the loop option to count.

.stagger(msec)

Set the delay between each element animation to msec.

Contribution

Contribution is welcome! Feel free to open an issue or a pull request.

License

MIT