Merge pull request #68 from seriousManual/master

Consider easing options in animation.
This commit is contained in:
Frédéric Bolvin 2018-11-12 11:55:46 +00:00 committed by GitHub
commit 1892641de7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

39
anim.js
View file

@ -11,7 +11,8 @@ function Anim() {
Anim.prototype.add = function(to, duration, options) {
var options = options || {}
var duration = duration || resolution
options['easing'] = options['easing'] || 'linear'
options.easing = options.easing || 'linear';
this.fx_stack.push({'to': to, 'duration': duration, 'options': options})
return this
}
@ -29,36 +30,44 @@ Anim.prototype.stop = function () {
Anim.prototype.run = function(universe, onFinish) {
var config = {}
var t = 0
var d = 0
var a
var ticks = 0
var duration = 0
var animationStep
var fx_stack = this.fx_stack;
var ani_setup = function() {
a = fx_stack.shift()
t = 0
d = a.duration
animationStep = fx_stack.shift()
ticks = 0
duration = animationStep.duration
config = {}
for(var k in a.to) {
for (var k in animationStep.to) {
config[k] = {
'start': universe.get(k),
'end': a.to[k]
'end': animationStep.to[k],
'options': animationStep.options
}
}
}
var ani_step = function() {
var new_vals = {}
var newValues = {}
for (var k in config) {
new_vals[k] = Math.round(config[k].start + ease['linear'](t, 0, 1, d) * (config[k].end - config[k].start))
var entry = config[k]
var easing = ease[entry.options.easing]
newValues[k] = Math.round(entry.start + easing(ticks, 0, 1, duration) * (entry.end - entry.start))
}
t = t + resolution
universe.update(new_vals)
if(t > d) {
ticks = ticks + resolution
universe.update(newValues)
if (ticks > duration) {
if (fx_stack.length > 0) {
ani_setup()
} else {
clearInterval(iid)
if(onFinish) onFinish()
if(onFinish) {
onFinish()
}
}
}
}