2013-11-17 00:41:27 +00:00
|
|
|
"use strict"
|
|
|
|
|
2012-09-02 12:36:08 +00:00
|
|
|
var ease = require('./easing.js').ease
|
2013-11-17 00:41:27 +00:00
|
|
|
var resolution = 25
|
|
|
|
|
|
|
|
function Anim() {
|
|
|
|
this.fx_stack = []
|
2017-11-22 18:05:22 +00:00
|
|
|
this.interval = null
|
2013-11-17 00:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Anim.prototype.add = function(to, duration, options) {
|
|
|
|
var options = options || {}
|
|
|
|
var duration = duration || resolution
|
2018-11-09 10:29:51 +00:00
|
|
|
options.easing = options.easing || 'linear';
|
|
|
|
|
2013-11-17 00:41:27 +00:00
|
|
|
this.fx_stack.push({'to': to, 'duration': duration, 'options': options})
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
Anim.prototype.delay = function(duration) {
|
|
|
|
return this.add({}, duration)
|
|
|
|
}
|
|
|
|
|
2017-11-22 18:05:22 +00:00
|
|
|
Anim.prototype.stop = function () {
|
|
|
|
if(this.interval) {
|
|
|
|
clearInterval(this.interval)
|
|
|
|
}
|
|
|
|
this.fx_stack = []
|
|
|
|
}
|
|
|
|
|
2013-11-17 00:41:27 +00:00
|
|
|
Anim.prototype.run = function(universe, onFinish) {
|
|
|
|
var config = {}
|
2018-11-09 10:29:51 +00:00
|
|
|
var ticks = 0
|
|
|
|
var duration = 0
|
|
|
|
var animationStep
|
2013-11-17 00:41:27 +00:00
|
|
|
|
|
|
|
var fx_stack = this.fx_stack;
|
|
|
|
var ani_setup = function() {
|
2018-11-09 10:29:51 +00:00
|
|
|
animationStep = fx_stack.shift()
|
|
|
|
ticks = 0
|
|
|
|
duration = animationStep.duration
|
|
|
|
|
2013-11-17 00:41:27 +00:00
|
|
|
config = {}
|
2018-11-09 10:29:51 +00:00
|
|
|
for (var k in animationStep.to) {
|
2013-11-17 00:41:27 +00:00
|
|
|
config[k] = {
|
|
|
|
'start': universe.get(k),
|
2018-11-09 10:29:51 +00:00
|
|
|
'end': animationStep.to[k],
|
|
|
|
'options': animationStep.options
|
2012-09-02 12:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-17 00:41:27 +00:00
|
|
|
}
|
|
|
|
var ani_step = function() {
|
2018-11-09 10:29:51 +00:00
|
|
|
var newValues = {}
|
|
|
|
for (var k in config) {
|
|
|
|
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))
|
2013-11-17 00:41:27 +00:00
|
|
|
}
|
2018-11-09 10:29:51 +00:00
|
|
|
|
|
|
|
ticks = ticks + resolution
|
|
|
|
universe.update(newValues)
|
|
|
|
if (ticks > duration) {
|
|
|
|
if (fx_stack.length > 0) {
|
2013-11-17 00:41:27 +00:00
|
|
|
ani_setup()
|
|
|
|
} else {
|
|
|
|
clearInterval(iid)
|
2018-11-09 10:29:51 +00:00
|
|
|
if(onFinish) {
|
|
|
|
onFinish()
|
|
|
|
}
|
2012-09-02 12:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-17 00:41:27 +00:00
|
|
|
|
|
|
|
ani_setup()
|
2017-11-22 18:05:22 +00:00
|
|
|
var iid = this.interval = setInterval(ani_step, resolution)
|
2012-09-02 12:36:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 18:05:22 +00:00
|
|
|
module.exports = Anim
|