diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-09-23 19:58:17 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-09-23 19:58:17 -0400 |
| commit | e273c92281e10c81f0605a0bf876d9a14bbe24d1 (patch) | |
| tree | 9a9d3820081c603545c59250402129a96255a0e7 /public/assets/js/vendor/oktween.js | |
| parent | 695417f6b029b3c5f752cc9cfff6223b201d476a (diff) | |
keydown
Diffstat (limited to 'public/assets/js/vendor/oktween.js')
| -rw-r--r-- | public/assets/js/vendor/oktween.js | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/public/assets/js/vendor/oktween.js b/public/assets/js/vendor/oktween.js new file mode 100644 index 0000000..d0d2b7c --- /dev/null +++ b/public/assets/js/vendor/oktween.js @@ -0,0 +1,124 @@ +/* + oktween.add({ + obj: el.style, + units: "px", + from: { left: 0 }, + to: { left: 100 }, + duration: 1000, + easing: oktween.easing.circ_out, + finish: function(){ + console.log("done") + } + }) +*/ + +var oktween = (function(){ + var oktween = {} + var tweens = oktween.tweens = [] + var last_t = 0 + var id = 0 + oktween.speed = 1 + oktween.then = oktween.add = function(tween){ + tween.id = id++ + tween.obj = tween.obj || {} + if (tween.easing) { + if (typeof tween.easing == "string") { + tween.easing = oktween.easing[tween.easing] + } + } + else { + tween.easing = oktween.easing.linear + } + if (! ('from' in tween) ) { + tween.from = {} + tween.keys = Object.keys(tween.to) + tween.keys.forEach(function(prop){ + tween.from[prop] = parseFloat(tween.obj[prop]) + }) + } + else { + tween.keys = Object.keys(tween.from) + } + tween.delay = tween.delay || 0 + tween.start = last_t + tween.delay + tween.done = false + tween.then = function(fn){ tween.after = [fn] } + tween.finish = function(){ tween.start = 0 } + tween.cancel = function(){ + var idx = tweens.indexOf(tween) + if (~idx) { tweens.splice(idx, 1) } + } + tween.tick = 0 + tween.skip = tween.skip || 1 + tweens.push(tween) + return tween + } + oktween.update = function(t) { + requestAnimationFrame(oktween.update) + last_t = t * oktween.speed + if (tweens.length == 0) return + var done = false + tweens.forEach(function(tween, i){ + var dt = Math.min(1.0, (t - tween.start) / tween.duration) + tween.tick++ + if (dt < 0 || (dt < 1 && (tween.tick % tween.skip != 0))) return + var ddt = tween.dt = tween.easing(dt) + tween.keys.forEach(function(prop){ + val = lerp( ddt, tween.from[prop], tween.to[prop] ) + if (tween.round) val = Math.round(val) + if (tween.units) val = (Math.round(val)) + tween.units + tween.obj[prop] = val + }) + tween.update && tween.update(tween.obj, dt) + if (dt == 1) { + tween.finished && tween.finished(tween) + tween.after && tween.after.forEach(function(twn){ + if (typeof twn == "function") { return twn() } + if (! twn.obj) { twn.obj = tween.obj } + oktween.add(twn) + }) + if (tween.loop) { + tween.start = t + tween.delay + } + else { + done = tween.done = true + } + } + }) + if (done) { + tweens = tweens.filter(function(tween){ return ! tween.done }) + } + } + function lerp(n,a,b){ return (b-a)*n+a } + + requestAnimationFrame(oktween.update) + + oktween.easing = { + linear: function(t){ + return t + }, + circ_out: function(t) { + return Math.sqrt(1 - (t = t - 1) * t) + }, + circ_in: function(t){ + return -(Math.sqrt(1 - (t * t)) - 1) + }, + circ_in_out: function(t) { + return ((t*=2) < 1) ? -0.5 * (Math.sqrt(1 - t * t) - 1) : 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1) + }, + quad_in: function(n){ + return Math.pow(n, 2) + }, + quad_out: function(n){ + return n * (n - 2) * -1 + }, + quad_in_out: function(n){ + n = n * 2 + if(n < 1){ return Math.pow(n, 2) / 2 } + return -1 * ((--n) * (n - 2) - 1) / 2 + }, + + } + + return oktween +})() |
