diff --git a/.eslintrc b/.eslintrc index cb49f18..5dce277 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,18 +1,19 @@ { "root": true, "rules": { - "newline-before-return": 0, - "prefer-const": 0, - "no-fallthrough": 0, + "@typescript-eslint/indent": 0, "@typescript-eslint/no-duplicate-enum-values": 0, - "@typescript-eslint/no-inferrable-types": 0, - "@typescript-eslint/no-explicit-any": 0, + "@typescript-eslint/no-empty-function": 0, "@typescript-eslint/no-empty-interface": 0, + "@typescript-eslint/no-explicit-any": 0, + "@typescript-eslint/no-extra-semi": 0, + "@typescript-eslint/no-inferrable-types": 0, "@typescript-eslint/no-non-null-assertion": 0, - "@typescript-eslint/no-empty-function": 0, + "@typescript-eslint/no-this-alias": 0, "@typescript-eslint/unified-signatures": 0, - "@typescript-eslint/indent": 0, - "@typescript-eslint/no-extra-semi": 0, + "newline-before-return": 0, + "no-fallthrough": 0, + "prefer-const": 0, "@typescript-eslint/explicit-member-accessibility": 1, "no-trailing-spaces": "warn", "@typescript-eslint/ban-types": "warn", diff --git a/src/TimingMode.ts b/src/TimingMode.ts index b20a313..8d6ece6 100644 --- a/src/TimingMode.ts +++ b/src/TimingMode.ts @@ -1,13 +1,3 @@ -/** - * Timing mode function. - * - * @see {TimingMode} for https://easings.net imports - */ -export type TimingModeFn = (progress: number) => number; - -/** A number 0 -> 1 */ -type Progress = number; - const pow = Math.pow; const sqrt = Math.sqrt; const sin = Math.sin; @@ -19,7 +9,7 @@ const c3 = c1 + 1; const c4 = (2 * PI) / 3; const c5 = (2 * PI) / 4.5; -const _bounceOut = function (x: Progress): number { +const _bounceOut = function (x: number): number { const n1 = 7.5625; const d1 = 2.75; @@ -37,66 +27,75 @@ const _bounceOut = function (x: Progress): number { } }; +/** + * Any timing mode function. + * + * @example x => x // Linear, constant-time. + * + * @see {TimingMode} + */ +export type TimingModeFn = (x: number) => number; + /** * Timing mode functions - * https://easings.net/ * + * @see https://easings.net/ * @see https://raw.githubusercontent.com/ai/easings.net/master/src/easings/easingsFunctions.ts */ export const TimingMode = { - linear: (x: Progress): number => x, - easeInQuad: function (x: Progress): number { + linear: (x: number): number => x, + easeInQuad: function (x: number): number { return x * x; }, - easeOutQuad: function (x: Progress): number { + easeOutQuad: function (x: number): number { return 1 - (1 - x) * (1 - x); }, - easeInOutQuad: function (x: Progress): number { + easeInOutQuad: function (x: number): number { return x < 0.5 ? 2 * x * x : 1 - pow(-2 * x + 2, 2) / 2; }, - easeInCubic: function (x: Progress): number { + easeInCubic: function (x: number): number { return x * x * x; }, - easeOutCubic: function (x: Progress): number { + easeOutCubic: function (x: number): number { return 1 - pow(1 - x, 3); }, - easeInOutCubic: function (x: Progress): number { + easeInOutCubic: function (x: number): number { return x < 0.5 ? 4 * x * x * x : 1 - pow(-2 * x + 2, 3) / 2; }, - easeInQuart: function (x: Progress): number { + easeInQuart: function (x: number): number { return x * x * x * x; }, - easeOutQuart: function (x: Progress): number { + easeOutQuart: function (x: number): number { return 1 - pow(1 - x, 4); }, - easeInOutQuart: function (x: Progress): number { + easeInOutQuart: function (x: number): number { return x < 0.5 ? 8 * x * x * x * x : 1 - pow(-2 * x + 2, 4) / 2; }, - easeInQuint: function (x: Progress): number { + easeInQuint: function (x: number): number { return x * x * x * x * x; }, - easeOutQuint: function (x: Progress): number { + easeOutQuint: function (x: number): number { return 1 - pow(1 - x, 5); }, - easeInOutQuint: function (x: Progress): number { + easeInOutQuint: function (x: number): number { return x < 0.5 ? 16 * x * x * x * x * x : 1 - pow(-2 * x + 2, 5) / 2; }, - easeInSine: function (x: Progress): number { + easeInSine: function (x: number): number { return 1 - cos((x * PI) / 2); }, - easeOutSine: function (x: Progress): number { + easeOutSine: function (x: number): number { return sin((x * PI) / 2); }, - easeInOutSine: function (x: Progress): number { + easeInOutSine: function (x: number): number { return -(cos(PI * x) - 1) / 2; }, - easeInExpo: function (x: Progress): number { + easeInExpo: function (x: number): number { return x === 0 ? 0 : pow(2, 10 * x - 10); }, - easeOutExpo: function (x: Progress): number { + easeOutExpo: function (x: number): number { return x === 1 ? 1 : 1 - pow(2, -10 * x); }, - easeInOutExpo: function (x: Progress): number { + easeInOutExpo: function (x: number): number { return x === 0 ? 0 : x === 1 @@ -105,43 +104,43 @@ export const TimingMode = { ? pow(2, 20 * x - 10) / 2 : (2 - pow(2, -20 * x + 10)) / 2; }, - easeInCirc: function (x: Progress): number { + easeInCirc: function (x: number): number { return 1 - sqrt(1 - pow(x, 2)); }, - easeOutCirc: function (x: Progress): number { + easeOutCirc: function (x: number): number { return sqrt(1 - pow(x - 1, 2)); }, - easeInOutCirc: function (x: Progress): number { + easeInOutCirc: function (x: number): number { return x < 0.5 ? (1 - sqrt(1 - pow(2 * x, 2))) / 2 : (sqrt(1 - pow(-2 * x + 2, 2)) + 1) / 2; }, - easeInBack: function (x: Progress): number { + easeInBack: function (x: number): number { return c3 * x * x * x - c1 * x * x; }, - easeOutBack: function (x: Progress): number { + easeOutBack: function (x: number): number { return 1 + c3 * pow(x - 1, 3) + c1 * pow(x - 1, 2); }, - easeInOutBack: function (x: Progress): number { + easeInOutBack: function (x: number): number { return x < 0.5 ? (pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2 : (pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2; }, - easeInElastic: function (x: Progress): number { + easeInElastic: function (x: number): number { return x === 0 ? 0 : x === 1 ? 1 : -pow(2, 10 * x - 10) * sin((x * 10 - 10.75) * c4); }, - easeOutElastic: function (x: Progress): number { + easeOutElastic: function (x: number): number { return x === 0 ? 0 : x === 1 ? 1 : pow(2, -10 * x) * sin((x * 10 - 0.75) * c4) + 1; }, - easeInOutElastic: function (x: Progress): number { + easeInOutElastic: function (x: number): number { return x === 0 ? 0 : x === 1 @@ -150,11 +149,11 @@ export const TimingMode = { ? -(pow(2, 20 * x - 10) * sin((20 * x - 11.125) * c5)) / 2 : (pow(2, -20 * x + 10) * sin((20 * x - 11.125) * c5)) / 2 + 1; }, - easeInBounce: function (x: Progress): number { + easeInBounce: function (x: number): number { return 1 - _bounceOut(1 - x); }, easeOutBounce: _bounceOut, - easeInOutBounce: function (x: Progress): number { + easeInOutBounce: function (x: number): number { return x < 0.5 ? (1 - _bounceOut(1 - 2 * x)) / 2 : (1 + _bounceOut(2 * x - 1)) / 2; diff --git a/src/mixin.ts b/src/mixin.ts index aa89752..c1135ac 100644 --- a/src/mixin.ts +++ b/src/mixin.ts @@ -1,5 +1,5 @@ -import { ActionTicker } from "./lib/ActionTicker"; import { _ as Action } from "./Action"; +import { ActionTicker } from "./lib/ActionTicker"; import { getSpeed } from "./lib/utils/displayobject"; // @@ -15,12 +15,10 @@ export function registerGlobalMixin(displayObject: any): void { const prototype = displayObject.prototype; // - Properties: - prototype.speed = 1.0; prototype.isPaused = false; // - Methods: - prototype.run = function (_action: Action, completion?: () => void): void { const action = completion ? Action.sequence([_action, Action.run(completion)]) : _action; ActionTicker.runAction(undefined, this, action); @@ -34,7 +32,6 @@ export function registerGlobalMixin(displayObject: any): void { action: Action, timeoutBufferMs: number = 100 ): Promise { - // eslint-disable-next-line @typescript-eslint/no-this-alias const node = this; return new Promise(function (resolve, reject) { const timeLimitMs = timeoutBufferMs + (getSpeed(node) * action.duration * 1_000); diff --git a/src/types.d.ts b/src/types.d.ts index 1789999..8367db9 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,5 +1,3 @@ -// typealiases - declare global { /** Time measured in seconds. */ @@ -8,16 +6,16 @@ declare global { /** Targeted display node. */ type TargetNode = PIXI.DisplayObject; - /** Targeted display node that has a width/height. */ + /** Targeted display node with a width and height. */ type SizedTargetNode = TargetNode & SizeLike; - /** A vector (e.g. PIXI.Point, or any node). */ + /** Any vector-like object (e.g. PIXI.Point, or any node). */ interface VectorLike { x: number; y: number; } - /** Any object with a width and height. */ + /** Any object with a width and height (e.g. PIXI.Sprite). */ interface SizeLike { width: number; height: number;