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