Skip to content

Commit 47354f6

Browse files
committed
Add easing functions and custom animation support to animation plugin
1 parent 71d23ee commit 47354f6

1 file changed

Lines changed: 189 additions & 2 deletions

File tree

src/plugins/animation.lua

Lines changed: 189 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---@configDefault false
22

33
local registeredAnimations = {}
4-
local easings = {
4+
local easings = {}
5+
easings = {
56
linear = function(progress)
67
return progress
78
end,
@@ -19,6 +20,171 @@ local easings = {
1920
return 2 * progress * progress
2021
end
2122
return 1 - (-2 * progress + 2)^2 / 2
23+
end,
24+
25+
easeInCubic = function(progress)
26+
return progress * progress * progress
27+
end,
28+
29+
easeOutCubic = function(progress)
30+
return 1 - (1 - progress)^3
31+
end,
32+
33+
easeInOutCubic = function(progress)
34+
if progress < 0.5 then
35+
return 4 * progress * progress * progress
36+
end
37+
return 1 - (-2 * progress + 2)^3 / 2
38+
end,
39+
40+
easeInQuart = function(progress)
41+
return progress * progress * progress * progress
42+
end,
43+
44+
easeOutQuart = function(progress)
45+
return 1 - (1 - progress)^4
46+
end,
47+
48+
easeInOutQuart = function(progress)
49+
if progress < 0.5 then
50+
return 8 * progress * progress * progress * progress
51+
end
52+
return 1 - (-2 * progress + 2)^4 / 2
53+
end,
54+
55+
easeInQuint = function(progress)
56+
return progress * progress * progress * progress * progress
57+
end,
58+
59+
easeOutQuint = function(progress)
60+
return 1 - (1 - progress)^5
61+
end,
62+
63+
easeInOutQuint = function(progress)
64+
if progress < 0.5 then
65+
return 16 * progress * progress * progress * progress * progress
66+
end
67+
return 1 - (-2 * progress + 2)^5 / 2
68+
end,
69+
70+
easeInSine = function(progress)
71+
return 1 - math.cos(progress * math.pi / 2)
72+
end,
73+
74+
easeOutSine = function(progress)
75+
return math.sin(progress * math.pi / 2)
76+
end,
77+
78+
easeInOutSine = function(progress)
79+
return -(math.cos(math.pi * progress) - 1) / 2
80+
end,
81+
82+
easeInExpo = function(progress)
83+
if progress == 0 then return 0 end
84+
return 2^(10 * progress - 10)
85+
end,
86+
87+
easeOutExpo = function(progress)
88+
if progress == 1 then return 1 end
89+
return 1 - 2^(-10 * progress)
90+
end,
91+
92+
easeInOutExpo = function(progress)
93+
if progress == 0 then return 0 end
94+
if progress == 1 then return 1 end
95+
if progress < 0.5 then
96+
return 2^(20 * progress - 10) / 2
97+
end
98+
return (2 - 2^(-20 * progress + 10)) / 2
99+
end,
100+
101+
easeInCirc = function(progress)
102+
return 1 - math.sqrt(1 - progress * progress)
103+
end,
104+
105+
easeOutCirc = function(progress)
106+
return math.sqrt(1 - (progress - 1) * (progress - 1))
107+
end,
108+
109+
easeInOutCirc = function(progress)
110+
if progress < 0.5 then
111+
return (1 - math.sqrt(1 - (2 * progress)^2)) / 2
112+
end
113+
return (math.sqrt(1 - (-2 * progress + 2)^2) + 1) / 2
114+
end,
115+
116+
easeInBack = function(progress)
117+
local c1 = 1.70158
118+
local c3 = c1 + 1
119+
return c3 * progress * progress * progress - c1 * progress * progress
120+
end,
121+
122+
easeOutBack = function(progress)
123+
local c1 = 1.70158
124+
local c3 = c1 + 1
125+
return 1 + c3 * (progress - 1)^3 + c1 * (progress - 1)^2
126+
end,
127+
128+
easeInOutBack = function(progress)
129+
local c1 = 1.70158
130+
local c2 = c1 * 1.525
131+
if progress < 0.5 then
132+
return ((2 * progress)^2 * ((c2 + 1) * 2 * progress - c2)) / 2
133+
end
134+
return ((2 * progress - 2)^2 * ((c2 + 1) * (progress * 2 - 2) + c2) + 2) / 2
135+
end,
136+
137+
easeInElastic = function(progress)
138+
local c4 = (2 * math.pi) / 3
139+
if progress == 0 then return 0 end
140+
if progress == 1 then return 1 end
141+
return -(2^(10 * progress - 10)) * math.sin((progress * 10 - 10.75) * c4)
142+
end,
143+
144+
easeOutElastic = function(progress)
145+
local c4 = (2 * math.pi) / 3
146+
if progress == 0 then return 0 end
147+
if progress == 1 then return 1 end
148+
return 2^(-10 * progress) * math.sin((progress * 10 - 0.75) * c4) + 1
149+
end,
150+
151+
easeInOutElastic = function(progress)
152+
local c5 = (2 * math.pi) / 4.5
153+
if progress == 0 then return 0 end
154+
if progress == 1 then return 1 end
155+
if progress < 0.5 then
156+
return -(2^(20 * progress - 10) * math.sin((20 * progress - 11.125) * c5)) / 2
157+
end
158+
return (2^(-20 * progress + 10) * math.sin((20 * progress - 11.125) * c5)) / 2 + 1
159+
end,
160+
161+
easeInBounce = function(progress)
162+
return 1 - easings.easeOutBounce(1 - progress)
163+
end,
164+
165+
easeOutBounce = function(progress)
166+
local n1 = 7.5625
167+
local d1 = 2.75
168+
169+
if progress < 1 / d1 then
170+
return n1 * progress * progress
171+
elseif progress < 2 / d1 then
172+
progress = progress - 1.5 / d1
173+
return n1 * progress * progress + 0.75
174+
elseif progress < 2.5 / d1 then
175+
progress = progress - 2.25 / d1
176+
return n1 * progress * progress + 0.9375
177+
else
178+
progress = progress - 2.625 / d1
179+
return n1 * progress * progress + 0.984375
180+
end
181+
end,
182+
183+
easeInOutBounce = function(progress)
184+
if progress < 0.5 then
185+
return (1 - easings.easeOutBounce(1 - 2 * progress)) / 2
186+
end
187+
return (1 + easings.easeOutBounce(2 * progress - 1)) / 2
22188
end
23189
}
24190

@@ -66,7 +232,7 @@ function AnimationInstance:start()
66232
if self.handlers.start then
67233
self.handlers.start(self)
68234
end
69-
return self
235+
return self
70236
end
71237

72238
--- Updates the animation
@@ -522,6 +688,27 @@ Animation.registerAnimation("marquee", {
522688
end
523689
})
524690

691+
Animation.registerAnimation("custom", {
692+
start = function(anim)
693+
anim.callback = anim.args[1]
694+
if type(anim.callback) ~= "function" then
695+
error("custom animation requires a function as first argument")
696+
end
697+
end,
698+
699+
update = function(anim, progress)
700+
local elapsed = os.epoch("local") / 1000 - anim.startTime
701+
anim.callback(anim.element, progress, elapsed)
702+
return progress >= 1
703+
end,
704+
705+
complete = function(anim)
706+
if anim.callback then
707+
anim.callback(anim.element, 1, anim.duration)
708+
end
709+
end
710+
})
711+
525712
--- Adds additional methods for VisualElement when adding animation plugin
526713
--- @class VisualElement
527714
local VisualElement = {hooks={}}

0 commit comments

Comments
 (0)