Skip to content

Instantly share code, notes, and snippets.

@dwilliamson
Created January 17, 2012 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwilliamson/1628806 to your computer and use it in GitHub Desktop.
Save dwilliamson/1628806 to your computer and use it in GitHub Desktop.
var anim_hz = 30;
function AnimHandle(complete, callback)
{
// No parameter ensures the handle represents a completed animation
this.Complete = complete == null ? true : complete;
this.Callback = callback;
}
function AnimateUpdate(handle)
{
// Linear step the value and pass to the animation function
handle.AnimFunc(handle.Value);
handle.Value += handle.ValueInc;
if (Math.abs(handle.Value - handle.End) > 0.01)
{
window.setTimeout(handle.AnimUpdate, 1000 / anim_hz);
}
else
{
// Upon completion issue one final animation function call with the end value
handle.AnimFunc(handle.End);
handle.Complete = true;
}
if (handle.Callback)
handle.Callback(handle.Complete);
}
function Animate(anim_func, start, end, time, callback)
{
// Create a new animation handle
var handle = new AnimHandle(false, callback);
handle.Start = start;
handle.End = end;
handle.ValueInc = (end - start) / (time * anim_hz);
handle.Value = start;
// Cache the update function to prevent recreating the closure
handle.AnimFunc = anim_func;
handle.AnimUpdate = function() { AnimateUpdate(handle); };
// Call for the start value
handle.AnimUpdate();
return handle;
}
/// USE
this.AnimHandle = Animate(
function (val) { item.ChildrenNode.style.height = val; },
item.ChildrenNode.offsetHeight, 0, 0.2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment