@pilotlab/lux-tools
Version:
A luxurious user experience framework, developed by your friends at Pilot.
56 lines • 2.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const is_1 = require("@pilotlab/is");
const result_1 = require("@pilotlab/result");
const signals_1 = require("@pilotlab/signals");
const timer_1 = require("@pilotlab/timer");
class Oscillator {
constructor(baseValue = 0, amplitude = 20, cycleDuration = 1, isEnabled = true, animationManager) {
this._baseValue = 0;
this._amplitude = 40;
this._cycleDuration = 1;
this._timer = new timer_1.HiResTimer(new timer_1.HiResTimerCounterBrowser());
this.ticked = new signals_1.Signal();
this._baseValue = baseValue;
this._amplitude = amplitude;
this._cycleDuration = cycleDuration;
if (this._cycleDuration <= 0)
this._cycleDuration = 0.0001;
if (is_1.is.notEmpty(animationManager)) {
this._animationManager = animationManager;
}
this.isEnabled = isEnabled;
}
get isEnabled() { return this._timer.isRunning; }
set isEnabled(value) {
if (this._timer.isRunning === value)
return;
if (value)
this.start();
else
this.stop();
}
start(isReset = false) {
if (is_1.is.empty(this._animationManager))
return;
if (this._timer.isRunning)
this._animationManager.ticked.listen(this.tick, this);
this._timer.start();
}
stop() {
if (!this._timer.isRunning)
return;
this._timer.stop();
this._animationManager.ticked.delete(this.tick);
}
tick(elapsed) {
if (!this._timer.isRunning)
return result_1.Result.resolve(null);
let value = this._baseValue + (this._amplitude * Math.sin(((2 * Math.PI) / this._cycleDuration) * (this._timer.elapsedMilliseconds / 1000)));
this.ticked.dispatch(value);
return result_1.Result.resolve(value);
}
}
exports.Oscillator = Oscillator;
exports.default = Oscillator;
//# sourceMappingURL=index.js.map