Skip to content

Instantly share code, notes, and snippets.

@a327ex
Created May 6, 2019 20:40
Show Gist options
  • Save a327ex/2b0a1c7f4108f86b384a96e7355368c6 to your computer and use it in GitHub Desktop.
Save a327ex/2b0a1c7f4108f86b384a96e7355368c6 to your computer and use it in GitHub Desktop.
timers = {}
function after(delay, action, repeatable, after, tag)
local tag = tag or uid() -- uid returns a unique id every time it's called, "or" will resolve the expression to the result of uid() if tag is nil (the same as if not tag then tag = uid() end)
timers[tag] = {type = 'after', current_time = 0, delay = delay, action = action, repeatable = repeatable, after = after, tag = tag}
end
function tween(delay, target, source, method, after, tag)
local tag = tag or uid()
local initial_values = {}
for k, _ in pairs(source) do initial_values[k] = target[k] end
timers[tag] = {type = 'tween', current_time = 0, delay = delay, target = target, initial_values = initial_values, source = source, method = method, after = after, tag = tag}
end
function update_timers(dt)
for tag, timer in pairs(timers) do
timer.current_time = timer.current_time + dt
if timer.type == 'after' then
if timer.current_time > timer.delay then
if timer.action then timer.action() end
if timer.repeatable then
if type(timer.repeatable) == 'number' then
timer.repeatable = timer.repeatable - 1
if timer.repeatable > 0 then timer.current_time = 0
else
if timer.after then timer.after() end
timers[tag] = nil
end
else timer.current_time = 0 end
end
end
elseif timer.type == 'tween' then
local t = _G[timer.method](timer.current_time/timer.delay)
for k, v in pairs(timer.source) do timer.target[k] = lerp(t, timer.initial_values[k], v) end
if timer.current_time > timer.delay then
if timer.after then timer.after() end
timers[tag] = nil
end
end
end
end
PI = math.pi
PI2 = math.pi/2
LN2 = math.log(2)
LN210 = 10*math.log(2)
function linear(t)
return t
end
function sine_in(t)
if t == 0 then return 0
elseif t == 1 then return 1
else return 1 - math.cos(t*PI2) end
end
function sine_out(t)
if t == 0 then return 0
elseif t == 1 then return 1
else return math.sin(t*PI2) end
end
function sine_in_out(t)
if t == 0 then return 0
elseif t == 1 then return 1
else return -0.5*(math.cos(t*PI) - 1) end
end
function sine_out_in(t)
if t == 0 then return 0
elseif t == 1 then return 1
elseif t < 0.5 then return 0.5*math.sin((t*2)*PI2)
else return -0.5*math.cos((t*2-1)*PI2) + 1 end
end
function quad_in(t)
return t*t
end
function quad_out(t)
return -t*(t-2)
end
function quad_in_out(t)
if t < 0.5 then return 2*t*t
else
t = t - 1
return -2*t*t + 1
end
end
function quad_out_in(t)
if t < 0.5 then
t = t*2
return -0.5*t*(t-2)
else
t = t*2 - 1
return 0.5*t*t + 0.5
end
end
function cubic_in(t)
return t*t*t;
end
function cubic_out(t)
t = t - 1
return t*t*t + 1
end
function cubic_in_out(t)
t = t*2
if t < 1 then return 0.5*t*t*t
else
t = t - 2
return 0.5*(t*t*t + 2)
end
end
function cubic_out_in(t)
t = t*2 - 1
return 0.5*(t*t*t + 1)
end
function quart_in(t)
return t*t*t*t
end
function quart_out(t)
t = t - 1
t = t*t
return 1 - t*t
end
function quart_in_out(t)
t = t*2
if t < 1 then return 0.5*t*t*t*t
else
t = t - 2
t = t*t
return -0.5*(t*t - 2)
end
end
function quart_out_in(t)
if t < 0.5 then
t = t*2 - 1
t = t*t
return -0.5*t*t + 0.5
else
t = t*2 - 1
t = t*t
return 0.5*t*t + 0.5
end
end
function quint_in(t)
return t*t*t*t*t
end
function quint_out(t)
t = t - 1
return t*t*t*t*t + 1
end
function quint_in_out(t)
t = t*2
if t < 1 then
return 0.5*t*t*t*t*t
else
t = t - 2
return 0.5*t*t*t*t*t + 1
end
end
function quint_out_in(t)
t = t*2 - 1
return 0.5*(t*t*t*t*t + 1)
end
function expo_in(t)
if t == 0 then return 0
else return math.exp(LN210*(t - 1)) end
end
function expo_out(t)
if t == 1 then return 1
else return 1 - math.exp(-LN210*t) end
end
function expo_in_out(t)
if t == 0 then return 0
elseif t == 1 then return 1 end
t = t*2
if t < 1 then return 0.5*math.exp(LN210*(t - 1))
else return 0.5*(2 - math.exp(-LN210*(t - 1))) end
end
function expo_out_in(t)
if t < 0.5 then return 0.5*(1 - math.exp(-20*LN2*t))
elseif t == 0.5 then return 0.5
else return 0.5*(math.exp(20*LN2*(t - 1)) + 1) end
end
function circ_in(t)
if t < -1 or t > 1 then return 0
else return 1 - math.sqrt(1 - t*t) end
end
function circ_out(t)
if t < 0 or t > 2 then return 0
else return math.sqrt(t*(2 - t)) end
end
function circ_in_out(t)
if t < -0.5 or t > 1.5 then return 0.5
else
t = t*2
if t < 1 then return -0.5*(math.sqrt(1 - t*t) - 1)
else
t = t - 2
return 0.5*(math.sqrt(1 - t*t) + 1)
end
end
end
function circ_out_in(t)
if t < 0 then return 0
elseif t > 1 then return 1
elseif t < 0.5 then
t = t*2 - 1
return 0.5*math.sqrt(1 - t*t)
else
t = t*2 - 1
return -0.5*((math.sqrt(1 - t*t) - 1) - 1)
end
end
function bounce_in(t)
t = 1 - t
if t < 1/2.75 then return 1 - (7.5625*t*t)
elseif t < 2/2.75 then
t = t - 1.5/2.75
return 1 - (7.5625*t*t + 0.75)
elseif t < 2.5/2.75 then
t = t - 2.25/2.75
return 1 - (7.5625*t*t + 0.9375)
else
t = t - 2.625/2.75
return 1 - (7.5625*t*t + 0.984375)
end
end
function bounce_out(t)
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
end
function bounce_in_out(t)
if t < 0.5 then
t = 1 - t*2
if t < 1/2.75 then return (1 - (7.5625*t*t))*0.5
elseif t < 2/2.75 then
t = t - 1.5/2.75
return (1 - (7.5625*t*t + 0.75))*0.5
elseif t < 2.5/2.75 then
t = t - 2.25/2.75
return (1 - (7.5625*t*t + 0.9375))*0.5
else
t = t - 2.625/2.75
return (1 - (7.5625*t*t + 0.984375))*0.5
end
else
t = t*2 - 1
if t < 1/2.75 then return (7.5625*t*t)*0.5 + 0.5
elseif t < 2/2.75 then
t = t - 1.5/2.75
return (7.5625*t*t + 0.75)*0.5 + 0.5
elseif t < 2.5/2.75 then
t = t - 2.25/2.75
return (7.5625*t*t + 0.9375)*0.5 + 0.5
else
t = t - 2.625/2.75
return (7.5625*t*t + 0.984375)*0.5 + 0.5
end
end
end
function bounce_out_in(t)
if t < 0.5 then
t = t*2
if t < 1/2.75 then return (7.5625*t*t)*0.5
elseif t < 2/2.75 then
t = t - 1.5/2.75
return (7.5625*t*t + 0.75)*0.5
elseif t < 2.5/2.75 then
t = t - 2.25/2.75
return (7.5625*t*t + 0.9375)*0.5
else
t = t - 2.625/2.75
return (7.5625*t*t + 0.984375)*0.5
end
else
t = 1 - (t*2 - 1)
if t < 1/2.75 then return 0.5 - (7.5625*t*t)*0.5 + 0.5
elseif t < 2/2.75 then
t = t - 1.5/2.75
return 0.5 - (7.5625*t*t + 0.75)*0.5 + 0.5
elseif t < 2.5/2.75 then
t = t - 2.25/2.75
return 0.5 - (7.5625*t*t + 0.9375)*0.5 + 0.5
else
t = t - 2.625/2.75
return 0.5 - (7.5625*t*t + 0.984375)*0.5 + 0.5
end
end
end
overshoot = 1.70158
function back_in(t)
if t == 0 then return 0
elseif t == 1 then return 1
else return t*t*((overshoot + 1)*t - overshoot) end
end
function back_out(t)
if t == 0 then return 0
elseif t == 1 then return 1
else
t = t - 1
return t*t*((overshoot + 1)*t + overshoot) + 1
end
end
function back_in_out(t)
if t == 0 then return 0
elseif t == 1 then return 1
else
t = t*2
if t < 1 then return 0.5*(t*t*(((overshoot*1.525) + 1)*t - overshoot*1.525))
else
t = t - 2
return 0.5*(t*t*(((overshoot*1.525) + 1)*t + overshoot*1.525) + 2)
end
end
end
function back_out_in(t)
if t == 0 then return 0
elseif t == 1 then return 1
elseif t < 0.5 then
t = t*2 - 1
return 0.5*(t*t*((overshoot + 1)*t + overshoot) + 1)
else
t = t*2 - 1
return 0.5*t*t*((overshoot + 1)*t - overshoot) + 0.5
end
end
amplitude = 1
period = 0.0003
function elastic_in(t)
if t == 0 then return 0
elseif t == 1 then return 1
else
t = t - 1
return -(amplitude*math.exp(LN210*t)*math.sin((t*0.001 - period/4)*(2*PI)/period))
end
end
function elastic_out(t)
if t == 0 then return 0
elseif t == 1 then return 1
else return math.exp(-LN210*t)*math.sin((t*0.001 - period/4)*(2*PI)/period) + 1 end
end
function elastic_in_out(t)
if t == 0 then return 0
elseif t == 1 then return 1
else
t = t*2
if t < 1 then
t = t - 1
return -0.5*(amplitude*math.exp(LN210*t)*math.sin((t*0.001 - period/4)*(2*PI)/period))
else
t = t - 1
return amplitude*math.exp(-LN210*t)*math.sin((t*0.001 - period/4)*(2*PI)/period)*0.5 + 1
end
end
end
function elastic_out_in(t)
if t < 0.5 then
t = t*2
if t == 0 then return 0
else return (amplitude/2)*math.exp(-LN210*t)*math.sin((t*0.001 - period/4)*(2*PI)/period) + 0.5 end
else
if t == 0.5 then return 0.5
elseif t == 1 then return 1
else
t = t*2 - 1
t = t - 1
return -((amplitude/2)*math.exp(LN210*t)*math.sin((t*0.001 - period/4)*(2*PI)/period)) + 0.5
end
end
end
function lerp(value, from, to)
return from*(1 - value) + to*value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment