From 99fe4c0556eaa22732b2d4a35fa3cc7f17808c8d Mon Sep 17 00:00:00 2001 From: PeaAshMeter Date: Tue, 4 Nov 2025 02:03:34 +0300 Subject: [PATCH] Add easing functions normalized to [0, 1] range --- lib/utils/easing.lua | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 lib/utils/easing.lua diff --git a/lib/utils/easing.lua b/lib/utils/easing.lua new file mode 100644 index 0000000..5d3092a --- /dev/null +++ b/lib/utils/easing.lua @@ -0,0 +1,52 @@ +---- Функции смягчения, нормализованные к [0, 1] + +---@alias ease fun(x: number): number + +local easing = {} + +--- @type ease +function easing.easeInSine(x) + return 1 - math.cos((x * math.pi) / 2); +end + +--- @type ease +function easing.easeOutSine(x) + return math.sin((x * math.pi) / 2); +end + +--- @type ease +function easing.easeInOutSine(x) + return -(math.cos(x * math.pi) - 1) / 2; +end + +--- @type ease +function easing.easeInQuad(x) + return x * x +end + +--- @type ease +function easing.easeOutQuad(x) + return 1 - (1 - x) * (1 - x) +end + +--- @type ease +function easing.easeInOutQuad(x) + return x < 0.5 and 2 * x * x or 1 - math.pow(-2 * x + 2, 2) / 2; +end + +--- @type ease +function easing.easeInCubic(x) + return x * x * x +end + +--- @type ease +function easing.easeOutCubic(x) + return 1 - math.pow(1 - x, 3) +end + +--- @type ease +function easing.easeInOutCubic(x) + return x < 0.5 and 4 * x * x * x or 1 - math.pow(-2 * x + 2, 3) / 2; +end + +return easing