Skip to content

Instantly share code, notes, and snippets.

@katspaugh
Created November 18, 2011 11:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katspaugh/1376200 to your computer and use it in GitHub Desktop.
Save katspaugh/1376200 to your computer and use it in GitHub Desktop.
Animation with pluggable easing functions.
/**
* Usage:
*
* animate(document.querySelector('#animatedElement'), {
* styles: {
* height: {
* start : 100
* end : 300,
* units : 'px'
* }
* },
* easing: 'easeOutBack',
* duration: 600,
* callback: function () { console.log('Animation stopped') }
* })
*/
var animate = (function (globals) {
'use strict'
var requestFrame = globals.requestAnimationFrame ||
globals.mozRequestAnimationFrame ||
globals.webkitRequestAnimationFrame ||
globals.msRequestAnimationFrame ||
(function (delay) {
return function (callback) {
setTimeout(function () {
callback((new Date).getTime())
}, delay)
}
}(1000 / 60))
/* Formulae by Robert Penner, http://robertpenner.com/easing/ */
var easings = {
easeInQuad: function (t, b, c, d) {
return c*(t/=d)*t + b;
},
easeOutBack: function (t, b, c, d, s) {
if (s == undefined) s = 1.70158;
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}
}
function setStyle(el, prop, value, units) {
if (prop in el.style) {
el.style[prop] = value + (units || 'px')
} else if (prop in el) {
el[prop] = value // e.g. el.scrollTop
}
}
function animate(el, params) {
var ease = easings[params.easing],
startDate = (new Date).getTime(),
state = {}
var frame = function (date) {
var progress = date - startDate
var next = progress < params.duration &&
(!('condition' in params) || params.condition(state))
var prop, style, value
for (prop in params.styles) {
if (params.styles.hasOwnProperty(prop)) {
style = params.styles[prop]
if (next) {
value = ease(
progress,
style.start,
style.end - style.start,
params.duration
)
} else {
value = style.end
}
value = ~~value
state[prop] = value
setStyle(el, prop, value, style.units)
}
}
if (next) {
requestFrame(frame)
} else {
params.callback && params.callback()
}
}
/* Launch animation. */
frame(startDate)
}
return animate
}(window))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment