box-of-eases/easelib.lua

133 lines
3.7 KiB
Lua
Raw Normal View History

2021-09-17 19:24:26 +00:00
local self = {}
local sqrt = math.sqrt
local sin = math.sin
local asin = math.asin
local cos = math.cos
local pow = math.pow
local exp = math.exp
local pi = math.pi
local abs = math.abs
self = setmetatable(self, {
__index = function(s, i)
for _,v in ipairs(self) do
if v[1] == i then
return v[2]
end
end
end
})
table.insert(self, {'linear', function(t) return t end})
table.insert(self, {'instant', function() return 1 end})
table.insert(self, {'bounce', function(t) return 4 * t * (1 - t) end})
table.insert(self, {'tri', function(t) return 1 - abs(2 * t - 1) end})
table.insert(self, {'bell', function(t) return self.inOutQuint(self.tri(t)) end})
table.insert(self, {'pop', function(t) return 3.5 * (1 - t) * (1 - t) * sqrt(t) end})
table.insert(self, {'tap', function(t) return 3.5 * t * t * sqrt(1 - t) end})
table.insert(self, {'pulse', function(t) return t < .5 and self.tap(t * 2) or -self.pop(t * 2 - 1) end})
table.insert(self, {'spike', function(t) return exp(-10 * abs(2 * t - 1)) end})
table.insert(self, {'inverse', function(t) return t * t * (1 - t) * (1 - t) / (0.5 - t) end})
table.insert(self, {'inSine', function(x)
2021-09-17 22:38:04 +00:00
return 1 - cos(x * (pi * 0.5))
2021-09-17 19:24:26 +00:00
end})
table.insert(self, {'outSine', function(x)
2021-09-17 22:38:04 +00:00
return sin(x * (pi * 0.5))
2021-09-17 19:24:26 +00:00
end})
table.insert(self, {'inOutSine', function(x)
2021-09-17 22:38:04 +00:00
return 0.5 - 0.5 * cos(x * pi)
2021-09-17 19:24:26 +00:00
end})
table.insert(self, {'inQuad', function(t) return t * t end})
table.insert(self, {'outQuad', function(t) return -t * (t - 2) end})
table.insert(self, {'inOutQuad', function(t)
2021-09-17 22:38:04 +00:00
t = t * 2
if t < 1 then
return 0.5 * t ^ 2
else
return 1 - 0.5 * (2 - t) ^ 2
end
2021-09-17 19:24:26 +00:00
end})
table.insert(self, {'inCubic', function(t) return t * t * t end})
table.insert(self, {'outCubic', function(t) return 1 - (1 - t) ^ 3 end})
table.insert(self, {'inOutCubic', function(t)
2021-09-17 22:38:04 +00:00
t = t * 2
if t < 1 then
return 0.5 * t ^ 3
else
return 1 - 0.5 * (2 - t) ^ 3
end
2021-09-17 19:24:26 +00:00
end})
table.insert(self, {'inQuart', function(t) return t * t * t * t end})
table.insert(self, {'outQuart', function(t) return 1 - (1 - t) ^ 4 end})
table.insert(self, {'inOutQuart', function(t)
2021-09-17 22:38:04 +00:00
t = t * 2
if t < 1 then
return 0.5 * t ^ 4
else
return 1 - 0.5 * (2 - t) ^ 4
end
2021-09-17 19:24:26 +00:00
end})
table.insert(self, {'inQuint', function(t) return t ^ 5 end})
table.insert(self, {'outQuint', function(t) return 1 - (1 - t) ^ 5 end})
table.insert(self, {'inOutQuint', function(t)
2021-09-17 22:38:04 +00:00
t = t * 2
if t < 1 then
return 0.5 * t ^ 5
else
return 1 - 0.5 * (2 - t) ^ 5
end
2021-09-17 19:24:26 +00:00
end})
table.insert(self, {'inExpo', function(t) return 1000 ^ (t - 1) - 0.001 end})
table.insert(self, {'outExpo', function(t) return 1.001 - 1000 ^ -t end})
table.insert(self, {'inOutExpo', function(t)
2021-09-17 22:38:04 +00:00
t = t * 2
if t < 1 then
return 0.5 * 1000 ^ (t - 1) - 0.0005
else
return 1.0005 - 0.5 * 1000 ^ (1 - t)
end
2021-09-17 19:24:26 +00:00
end})
table.insert(self, {'inCirc', function(t) return 1 - sqrt(1 - t * t) end})
table.insert(self, {'outCirc', function(t) return sqrt(-t * t + 2 * t) end})
table.insert(self, {'inOutCirc', function(t)
2021-09-17 22:38:04 +00:00
t = t * 2
if t < 1 then
return 0.5 - 0.5 * sqrt(1 - t * t)
else
t = t - 2
return 0.5 + 0.5 * sqrt(1 - t * t)
end
2021-09-17 19:24:26 +00:00
end})
table.insert(self, {'outBounce', function(t)
2021-09-17 22:38:04 +00:00
if t < 1 / 2.75 then
return 7.5625 * t * t
elseif t < 2 / 2.75 then
t = t - 1.5 / 2.75
return 7.5625 * t * t + 0.75
elseif t < 2.5 / 2.75 then
t = t - 2.25 / 2.75
return 7.5625 * t * t + 0.9375
else
t = t - 2.625 / 2.75
return 7.5625 * t * t + 0.984375
end
2021-09-17 19:24:26 +00:00
end})
table.insert(self, {'inBounce', function(t) return 1 - self.outBounce(1 - t) end})
table.insert(self, {'inOutBounce', function(t)
2021-09-17 22:38:04 +00:00
if t < 0.5 then
return self.inBounce(t * 2) * 0.5
else
return self.outBounce(t * 2 - 1) * 0.5 + 0.5
end
2021-09-17 19:24:26 +00:00
end})
return self