motion-and-tween
Version:
Motion and Tween
151 lines (150 loc) • 5.09 kB
JavaScript
import RealWorldTimer from "../timing/RealWorldTimer.js";
var Motion = /** @class */ (function () {
function Motion(_a) {
var obj = _a.obj, propertyToChange = _a.propertyToChange, beginValue = _a.beginValue, actionDuration = _a.actionDuration, valueChange = _a.valueChange;
this._isLooping = false;
this._valueChange = null;
this.obj = obj;
this.propertyToChange = propertyToChange;
this._beginValue = beginValue;
this._actionDuration = actionDuration;
this._listeners = [];
this.addListener(this);
this.start();
this._currentTime = 0;
this._prevPos = null;
this._pos = beginValue;
this._startTime = RealWorldTimer.getElapsedTime();
this._valueChange = valueChange;
}
Motion.prototype.getElapsedTime = function () {
return RealWorldTimer.getElapsedTime();
};
Motion.prototype.start = function () {
this.rewind();
this.addListener(this);
};
Motion.prototype.stop = function () {
this.removeListener(this);
};
Motion.prototype.resume = function () {
this.addListener(this);
};
Motion.prototype.rewind = function (t) {
if (t === void 0) { t = this._currentTime; }
this._currentTime = !t ? 1 : t;
this.update();
};
Motion.prototype.addListener = function (listener) {
this._listeners.push(listener);
};
Motion.prototype.nextFrame = function () {
this.setTime(this._currentTime + 1);
};
Motion.prototype.prevFrame = function () {
this.setTime(this._currentTime - 1);
};
Motion.prototype.setTime = function (t) {
if (t > this._actionDuration) {
if (this._isLooping) {
this.rewind(t - this._actionDuration);
}
else {
this.stop();
}
}
else if (t < 0) {
this.rewind();
}
else {
this._currentTime = t;
}
this.update();
};
Motion.prototype.getTime = function () {
return this._currentTime;
};
Motion.prototype.setBegin = function (b) {
this._beginValue = b;
};
Motion.prototype.getBegin = function () {
return this._beginValue;
};
Motion.prototype.setActionDuration = function (d) {
this._actionDuration = d === null || d <= 0 ? 10000000 : d;
};
Motion.prototype.getActionDuration = function () {
return this._actionDuration;
};
Motion.prototype.setIsLooping = function (b) {
this._isLooping = b;
};
Motion.prototype.getIsLooping = function () {
return this._isLooping;
};
Motion.prototype.setObj = function (obj) {
this.obj = obj;
};
Motion.prototype.getObj = function () {
return this.obj;
};
Motion.prototype.setProp = function (prop) {
this.propertyToChange = prop;
};
Motion.prototype.getProp = function () {
return this.propertyToChange;
};
Motion.prototype.expandParams = function (params) {
if (params === void 0) { params = null; }
var lastT, nextT, beginValue, valueChange, actionDuration;
if (params) {
if (params.beginValue)
beginValue = params.beginValue;
if (params.valueChange)
valueChange = params.valueChange;
}
lastT = this._currentTime;
if (!params) {
lastT = this._currentTime;
nextT = this._currentTime + 1;
beginValue = this._beginValue;
valueChange = this._valueChange;
actionDuration = this._actionDuration;
}
else {
if (params.hasOwnProperty("t")) {
nextT = params.t;
}
if (!beginValue) {
beginValue = this._beginValue;
}
if (!valueChange) {
valueChange = this._valueChange;
}
if (!actionDuration) {
actionDuration = this._actionDuration;
}
}
return { lastT: lastT, nextT: nextT, beginValue: beginValue, valueChange: valueChange, actionDuration: actionDuration };
};
// Override this method
Motion.prototype.update = function (params) {
if (params === void 0) { params = null; }
if (params) {
this._currentTime = params.t;
}
else {
this._currentTime += 1;
}
return this.obj;
};
Motion.prototype.removeListener = function (listener) {
this._listeners = this._listeners.filter(function (l) { return l !== listener; });
};
Motion.prototype.toString = function () {
return "Motion[obj=".concat(JSON.stringify(this.obj), ", prop=").concat(JSON.stringify(this.propertyToChange), ", _beginValue=").concat(this._beginValue, ", _duration=").concat(this._actionDuration, "]"); // , useSeconds=${this.useSeconds}]`;
};
return Motion;
}());
export { Motion };
export default Motion;