Created
October 21, 2012 23:00
-
-
Save Flopster/3928846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Animation class | |
-- | |
-- Create with Animator(start, stop, time, easing, callback) | |
-- "start" is the start value | |
-- "end" is the end value | |
-- "time" is the time taken, in seconds, to complete the animation | |
-- "easing" is the easing of the animation (how it slows down/speeds up) | |
-- use EASENONE, EASEIN, EASEOUT, or EASEBOTH | |
-- "callback" is the function that is called once the animation is | |
-- complete | |
-- | |
-- Get the current value of the animation with | |
-- Animator:value() | |
-- This will call the callback function if the function is complete | |
-- (it only calls it once) | |
-- | |
-- You can also get the value at any time t between 0 and 1 using | |
-- Animator:valueAt(t, [easing]) | |
EASENONE = 1 | |
EASEIN = 2 | |
EASEOUT = 3 | |
EASEBOTH = 4 | |
Animator = class() | |
function Animator:init(start, stop, time, easing, callback) | |
self.start = start | |
self.stop = stop | |
self.stopTime = time | |
self.startTime = ElapsedTime | |
self.easing = easing or EASENONE | |
self.complete = false | |
self.callback = callback or function() end | |
end | |
function Animator:valueAt(t, easing) | |
easing = easing or self.easing | |
if self.easing == EASENONE then | |
value = t * (self.stop - self.start) + self.start | |
elseif self.easing == EASEOUT then | |
value = math.sin(math.pi/2 * t)* | |
(self.stop-self.start)+self.start | |
elseif self.easing == EASEIN then | |
value = (-math.cos(math.pi/2 * t)+1)* | |
(self.stop-self.start)+self.start | |
elseif self.easing == EASEBOTH then | |
value = (-math.cos(math.pi * t)+1)/2* | |
(self.stop-self.start)+self.start | |
end | |
return value | |
end | |
function Animator:value() | |
time = (ElapsedTime - self.startTime)/self.stopTime | |
time = math.min(time, 1) | |
if time == 1 then | |
if not self.complete then | |
self.complete = true | |
self.callback() | |
end | |
return self.stop | |
else | |
return self:valueAt(time) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment