2018-12-19 19:24:06 +00:00
|
|
|
const ease = require('./easing.js').ease;
|
|
|
|
const resolution = 25;
|
2013-11-17 00:41:27 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
class Anim {
|
|
|
|
constructor() {
|
|
|
|
this.fxStack = [];
|
|
|
|
this.interval = null;
|
|
|
|
}
|
2013-11-17 00:41:27 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
add(to, duration = resolution, options = {}) {
|
|
|
|
options.easing = options.easing || 'linear';
|
2013-11-17 00:41:27 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
this.fxStack.push({ 'to': to, 'duration': duration, 'options': options });
|
|
|
|
return this;
|
|
|
|
}
|
2013-11-17 00:41:27 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
delay(duration) {
|
|
|
|
this.add({}, duration);
|
|
|
|
return this;
|
|
|
|
}
|
2013-11-17 00:41:27 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
stop() {
|
|
|
|
if (this.interval) {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
|
|
|
this.fxStack = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
run(universe, onFinish) {
|
|
|
|
let config = {};
|
|
|
|
let ticks = 0;
|
|
|
|
let duration = 0;
|
|
|
|
let animationStep;
|
|
|
|
let iid = null;
|
|
|
|
|
|
|
|
const stack = [ ...this.fxStack ];
|
|
|
|
|
|
|
|
const aniSetup = () => {
|
|
|
|
animationStep = stack.shift();
|
|
|
|
ticks = 0;
|
|
|
|
duration = animationStep.duration;
|
|
|
|
|
|
|
|
config = {};
|
|
|
|
for (const k in animationStep.to) {
|
|
|
|
config[k] = {
|
|
|
|
'start': universe.get(k),
|
|
|
|
'end': animationStep.to[k],
|
|
|
|
'options': animationStep.options,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const aniStep = () => {
|
|
|
|
const newValues = {};
|
2017-11-22 18:05:22 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
for (const k in config) {
|
|
|
|
const entry = config[k];
|
|
|
|
const easing = ease[entry.options.easing];
|
2013-11-17 00:41:27 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
newValues[k] = Math.round(entry.start + easing(ticks, 0, 1, duration) * (entry.end - entry.start));
|
|
|
|
}
|
2018-11-09 10:29:51 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
ticks = ticks + resolution;
|
|
|
|
universe.update(newValues);
|
|
|
|
if (ticks > duration) {
|
|
|
|
if (stack.length > 0) {
|
|
|
|
aniSetup();
|
|
|
|
} else {
|
|
|
|
clearInterval(iid);
|
|
|
|
if (onFinish) {
|
|
|
|
onFinish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-11-17 00:41:27 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
aniSetup();
|
|
|
|
iid = this.interval = setInterval(aniStep, resolution);
|
2018-11-09 13:00:00 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
return this;
|
|
|
|
}
|
2012-09-02 12:36:08 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
module.exports = Anim;
|