avocado-old/packages/mixins/lfo/result.js
2019-03-17 23:45:48 -05:00

67 lines
1.2 KiB
JavaScript

import ModulatedProperty from './modulated-property';
export default class LfoResult {
constructor(object, properties, duration = 0) {
this.duration = duration;
this.elapsed = 0;
this.isRunning = false;
this.object = {};
this.properties = {};
this.start();
this.deferred = {};
this.promise = this.deferred.promise = new Promise((resolve, reject) => {
this.deferred.resolve = resolve;
this.deferred.reject = reject;
});
for (const key in properties) {
this.properties[key] = new ModulatedProperty(object, key, {
frequency: this.duration,
...properties[key]
});
}
}
property(key) {
return this.properties[key];
}
start() {
this.elapsed = 0;
this.isRunning = true;
}
stop() {
this.isRunning = false;
}
tick(elapsed) {
if (!this.isRunning) {
return;
}
let finished = false;
if (this.duration > 0 && this.duration <= (this.elapsed += elapsed)) {
finished = true;
elapsed = this.elapsed - this.duration;
this.elapsed = this.duration;
}
for (const key in properties) {
properties[key].tick(elapsed);
}
if (finished) {
this.deferred.resolve();
this.stop();
}
}
}