smoke-distance
Version:
tweening engine for Typescript
164 lines (162 loc) • 3.88 kB
JavaScript
// src/easing.ts
var easeInBy = (power) => {
return (t) => Math.pow(t, power);
};
var easeOutBy = (power) => {
return (t) => 1 - Math.abs(Math.pow(t - 1, power));
};
var easeInOutBy = (power) => {
return (t) => t < 0.5 ? easeInBy(power)(t * 2) / 2 : easeOutBy(power)(t * 2 - 1) / 2 + 0.5;
};
var linear = (t) => t;
var quadIn = easeInBy(2);
var quadOut = easeOutBy(2);
var quadInOut = easeInOutBy(2);
var cubicIn = easeInBy(3);
var cubicOut = easeOutBy(3);
var cubicInOut = easeInOutBy(3);
var quartIn = easeInBy(4);
var quartOut = easeOutBy(4);
var quartInOut = easeInOutBy(4);
var quintIn = easeInBy(5);
var quintOut = easeOutBy(5);
var quintInOut = easeInOutBy(5);
var sineIn = (t) => 1 + Math.sin(Math.PI / 2 * t - Math.PI / 2);
var sineOut = (t) => Math.sin(Math.PI / 2 * t);
var sineInOut = (t) => (1 + Math.sin(Math.PI * t - Math.PI / 2)) / 2;
var bounceOut = (t) => {
const s = 7.5625;
const p = 2.75;
if (t < 1 / p)
return s * t * t;
if (t < 2 / p) {
t -= 1.5 / p;
return s * t * t + 0.75;
}
if (t < 2.5 / p) {
t -= 2.25 / p;
return s * t * t + 0.9375;
}
t -= 2.625 / p;
return s * t * t + 0.984375;
};
var bounceIn = (t) => 1 - bounceOut(1 - t);
var bounceInOut = (t) => t < 0.5 ? bounceIn(t * 2) * 0.5 : bounceOut(t * 2 - 1) * 0.5 + 0.5;
var easing = {
bounceIn,
bounceInOut,
bounceOut,
sineInOut,
sineOut,
sineIn,
quintInOut,
quintOut,
quintIn,
quartInOut,
quartOut,
quartIn,
cubicInOut,
cubicIn,
cubicOut,
quadInOut,
quadOut,
quadIn,
linear
};
// src/distance.ts
var SmokeDistance = class {
_from = {};
_to = {};
_duration = 500;
_delay = 0;
_easing = "linear";
_onStart = void 0;
_onUpdate = void 0;
_onFinish = void 0;
_startTime = 0;
_started = false;
_finished = false;
_timer = null;
_time = null;
_elapsed = null;
_keys = {};
constructor(options) {
const {
from,
to,
duration,
delay,
easing: easing2,
onStart,
onUpdate,
onFinish
} = options;
for (const key in from) {
if (to[key] === void 0)
to[key] = from[key];
}
for (const key in to) {
if (from[key] === void 0)
from[key] = to[key];
}
this._from = from;
this._to = to;
this._duration = duration || 500;
this._delay = delay || 0;
this._easing = easing2 || "linear";
this._onStart = onStart;
this._onUpdate = onUpdate || function() {
};
this._onFinish = onFinish;
this._startTime = Date.now() + this._delay;
this._started = false;
this._finished = false;
this._timer = null;
this._time = null;
this._keys = {};
}
update() {
this._time = Date.now();
if (this._time < this._startTime)
return;
if (this._finished)
return;
if (this._elapsed === this._duration) {
if (!this._finished) {
this._finished = true;
this._onFinish && this._onFinish(this._keys);
}
return;
}
this._elapsed = this._time - this._startTime;
this._elapsed = this._elapsed > this._duration ? this._duration : this._elapsed;
for (const key in this._to)
this._keys[key] = this._from[key] + (this._to[key] - this._from[key]) * easing[this._easing](this._elapsed / this._duration);
if (!this._started) {
this._onStart && this._onStart(this._keys);
this._started = true;
}
this._onUpdate && this._onUpdate(this._keys);
}
start() {
this._startTime = Date.now() + this._delay;
const tick = () => {
this.update();
this._timer = requestAnimationFrame(tick);
if (this._finished) {
cancelAnimationFrame(this._timer);
this._timer = null;
}
};
tick();
}
stop() {
if (this._timer !== null) {
cancelAnimationFrame(this._timer);
this._timer = null;
}
}
};
export {
SmokeDistance
};