Using jQuery's animate() Method To Power Easing-Based Iteration

Last night, I was reading about jQuery animation when I had the thought that you could use the animate() method to power iteration in which the iteration-step was implemented with an easing function rather than with a liner incrementation. A while back, I demonstrated that the step callback of the animate() method could be used to create complex, custom animation; but, I think you could abstract that concept out even further to create animate-powered iteration. Now, why would you want to do this? I have no idea; but, it seemed like a fun thing to try.

In the following code, I am adding the ease() function to the jQuery namespace:

jQuery.ease( start, end, duration, easing, callback )

To relate this concept back to for-loop iteration, you can sort of think of the above as being equivalent to the following pseudo code:

for (var i = start ; i <= end ; i = easing()){

callback( i );

}

When the callback gets invoked for each step of the iteration, I am passing in the following parameters:

Index: The current value of the iteration index.

StepIndex: The current iteration (increments 1 for each callback invocation).

EstimatedSteps: The rough estimation of how many times the callback will be invoked.

Start: The original start value.

End: The original end value.

To see this in action, take a look at the following code:

<!DOCTYPE html>

<html>

<head>

<title>Using jQuery's Animate / Easing For Iteration</title>

<script type="text/javascript" src="./jquery-1.4.2.js"></script>

<script type="text/javascript">

// I am the easing iteration funciton. This is built on top

// of the core animate function so that it can leverage the

// built-in timer optimization.

jQuery.ease = function( start, end, duration, easing, callback ){

// Create a jQuery collection containing the one element

// that we will be animating internally.

var easer = $( "<div>" );

// Keep track of the iterations.

var stepIndex = 0;

// Get the estimated number of steps - this is based on

// the fact that jQuery appears to use a 13ms timer step.

//

// NOTE: Since this is based on a timer, the number of

// steps is estimated and will vary depending on the

// processing power of the browser.

var estimatedSteps = Math.ceil( duration / 13 );

// Set the start index of the easer.

easer.css( "easingIndex", start );

// Animate the easing index to the final value. For each

// step of the animation, we are going to pass the

// current step value off to the callback.

easer.animate(

{

easingIndex: end

},

{

easing: easing,

duration: duration,

step: function( index ){

// Invoke the callback for each step.

callback(

index,

stepIndex++,

estimatedSteps,

start,

end

);

}

}

);

};

// -------------------------------------------------- //

// -------------------------------------------------- //

// Ease from star (1) to finish (100).

$.ease(

1,

100,

1000,

"swing",

function(){

console.log( arguments );

}

);

</script>

</head>

<body>

<!-- Intentionally left blank. -->

</body>

</html>

As you can see, I am defining an iteration from 1 to 100 over the course of 1000ms using the "swing" easing logic that comes with the jQuery core library. When I execute this iteration, the following arguments are logged to the console:

Internally, the ease() function creates a detached DOM node and then uses jQuery's animate() method to "animate" a fake CSS property that will act as our iteration index. Then, we simply hook into the step-callback in order to invoke the given iteration callback, passing in our current iteration value.

It would have been simple enough to bypass the animate() method and set up my own timer using setInterval(). However, Javascript timers are expensive to use. jQuery handles this cost quite gracefully by creating a central timer from which all animate() methods are powered. As such, I figured it would make the most sense just to leverage the optimized infrastructure that jQuery already had in place.

Again, I am not sure why anyone would want to use this; but, it popped into my head last night and I wanted to see if I could make it happen. Of course, this implementation presupposed that one would want to use a duration over which the iteration would take place. We could remove the duration aspect altogether, which gives me a new idea... but more on that later.

Oh cool - glad you found it useful. I think the hardest thing about this, as far as generic usefulness, is that it requires the concept of a "duration" in addition to a start/end. Although, I suppose you could just think of this as the "step" portion of a traditional for-loop.

I stumbled upon your post on my search for a way to ease the changes on a temperature dial I'm trying to create for my Arduino weather station. I tried to figure it out on my own, but because I was using setTimeout and setInterval I couldn't incorporate a duration into the ewuation. So, first of all - THANK YOU :)

I have a small problem though - the easing always starts from 0, no matter what number I set as the start parameter.

I've set up a jsfiddle for testing, so maybe you can check it out and tell me what's wrong?

I am the co-founder and lead engineer at InVision App, Inc — the world's leading prototyping,
collaboration & workflow platform. I also rock out in JavaScript and ColdFusion 24x7 and I dream about
promise resolving asynchronously.