UNPKG

@litecanvas/utils

Version:

Utilities to help build litecanvas games

257 lines (253 loc) 6.58 kB
(() => { var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; // src/_global.js globalThis.utils = globalThis.utils || {}; globalThis.utils.global = (overrides = true) => { for (const key in globalThis.utils) { if ("global" === key) continue; if (overrides || globalThis[key] === void 0) { globalThis[key] = globalThis.utils[key]; } } }; // src/tween/index.js var index_exports = {}; __export(index_exports, { BACK_IN: () => BACK_IN, BACK_IN_OUT: () => BACK_IN_OUT, BACK_OUT: () => BACK_OUT, BOUNCE_IN: () => BOUNCE_IN, BOUNCE_IN_OUT: () => BOUNCE_IN_OUT, BOUNCE_OUT: () => BOUNCE_OUT, EASE_IN: () => EASE_IN, EASE_IN_OUT: () => EASE_IN_OUT, EASE_OUT: () => EASE_OUT, ELASTIC_IN: () => ELASTIC_IN, ELASTIC_IN_OUT: () => ELASTIC_IN_OUT, ELASTIC_OUT: () => ELASTIC_OUT, LINEAR: () => LINEAR, tween: () => tween }); var HALF_PI = Math.PI / 2; var tween = (object, prop, toValue, duration = 1, easing = LINEAR) => { return new TweenController(object, prop, toValue, duration, easing); }; var LINEAR = (n) => n; var EASE_IN = (n) => n * n; var EASE_OUT = (n) => -n * (n - 2); var EASE_IN_OUT = (n) => { if (n < 0.5) { return 2 * n * n; } return -2 * n * n + 4 * n - 1; }; var BACK_IN = (n) => n * n * n - n * Math.sin(n * Math.PI); var BACK_OUT = (n) => { let a = 1 - n; return 1 - (a * a * a - a * Math.sin(a * Math.PI)); }; var BACK_IN_OUT = (n) => { if (n < 0.5) { let a2 = 2 * n; return 0.5 * (a2 * a2 * a2 - a2 * Math.sin(a2 * Math.PI)); } let a = 1 - (2 * n - 1); return 0.5 * (1 - (a * a * a - a * Math.sin(n * Math.PI))) + 0.5; }; var ELASTIC_IN = (n) => { return Math.sin(13 * HALF_PI * n) * Math.pow(2, 10 * (n - 1)); }; var ELASTIC_OUT = (n) => { return Math.sin(-13 * HALF_PI * (n + 1)) * Math.pow(2, -10 * n) + 1; }; var ELASTIC_IN_OUT = (n) => { if (n < 0.5) { let a2 = Math.sin(13 * HALF_PI * (2 * n)); let b2 = Math.pow(2, 10 * (2 * n - 1)); return 0.5 * a2 * b2; } let a = Math.sin(-13 * HALF_PI * (2 * n - 1 + 1)); let b = Math.pow(2, -10 * (2 * n - 1)); return 0.5 * (a * b + 2); }; var BOUNCE_IN = (n) => 1 - BOUNCE_OUT(1 - n); var BOUNCE_OUT = (n) => { if (n < 4 / 11) { return 121 * n * n / 16; } else if (n < 8 / 11) { return 363 / 40 * n * n - 99 / 10 * n + 17 / 5; } else if (n < 9 / 10) { return 4356 / 361 * n * n - 35442 / 1805 * n + 16061 / 1805; } return 54 / 5 * n * n - 513 / 25 * n + 268 / 25; }; var BOUNCE_IN_OUT = (n) => { if (n < 0.5) { return 0.5 * BOUNCE_IN(n * 2); } return 0.5 * BOUNCE_OUT(n * 2 - 1) + 0.5; }; var TweenController = class { /** @type {boolean} */ running = false; /** @type {*} */ _o; /** @type {string} */ _p; /** @type {number|number} */ _x; /** @type {number} */ _d; /** @type {number} */ _w; /** @type {(x: number) => number} */ _e; /** @type {boolean} */ _rel; /** @type {Function[]} */ _cb = []; /** @type {number} */ _t = 0; /** @type {Function} */ _u = 0; /** @type {TweenController} */ _ch = this; /** @type {TweenController} */ _cu = this; /** @type {LitecanvasInstance} */ _lc; /** * @param {*} object * @param {string} prop * @param {number|number} toValue * @param {number} duration * @param {(x: number) => number} easing */ constructor(object, prop, toValue, duration, easing) { this._o = object; this._p = prop; this._x = toValue; this._d = duration; this._e = easing; this._w = 0; } /** * @param {LitecanvasInstance} [engine] * @returns {this} */ start(engine) { if (this.running) { return this; } this._cu.stop(false); this._ch = this._cu = this; this.running = true; const fromValue = this._o[this._p] || 0; const toValue = this._rel ? fromValue + this._x : this._x; this._lc = this._lc || engine || globalThis; this._u = this._lc.listen("update", (dt) => { if (this._t <= this._w) { this._t += dt; return; } const t = this._t - this._w; this._o[this._p] = this._lc.lerp(fromValue, toValue, this._e(t / this._d)); this._t += dt; if (t >= this._d) { this._o[this._p] = toValue; this.stop(); } }); return this; } /** * @param {boolean} completed if `false` don't call the `onEnd()` registered callbacks. * @returns {this} */ stop(completed = true) { if (!this._u) return this; this.running = false; this._u(); this._t = 0; if (completed) { for (const callback of this._cb) { callback(this._o); } } return this; } /** * @param {LitecanvasInstance} [engine] * @returns {this} */ restart(engine = null, completed = false) { return this.stop(completed).restart(engine); } /** * @param {Function} callback * @returns {this} */ onEnd(callback) { this._cb.push(callback); return this; } /** * @param {TweenController} another * @returns {this} */ chain(another) { this._ch.onEnd(() => { this._cu = another.start(this._lc); }); this._ch = another; return this; } /** * @param {boolean} [flag=true] * @returns {this} */ reset() { this._cb.length = 0; return this.stop(); } /** * @param {boolean} [flag=true] * @returns {this} */ relative(flag = true) { this._rel = flag; return this; } /** * @param {number} value * @returns {this} */ delay(value) { this._w = value; return this; } /** * Returns the current tween of the chain. * * @returns {this} */ get current() { return this._cu; } /** * @returns {number} the current progress (0..1) */ get progress() { if (this.running && this._t > this._w) { return (this._t - this._w) / this._d; } return 0; } }; // src/tween/_web.js globalThis.utils = Object.assign(globalThis.utils || {}, index_exports); })();