Skip to content

Instantly share code, notes, and snippets.

@Flopster
Created October 21, 2012 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Flopster/3928830 to your computer and use it in GitHub Desktop.
Save Flopster/3928830 to your computer and use it in GitHub Desktop.
--# Animator
-- 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
--# Main
-- Animation
-- Use this function to perform your initial setup
function setup()
tests = {Test1(), Test2()}
iparameter("testNumber", 1, #tests)
currentTest = tests[1]
currentTestNum = 1
end
-- This function gets called once every frame
function draw()
if currentTestNum ~= testNumber then
currentTestNum = testNumber
currentTest = tests[currentTestNum]
currentTest:reset()
end
currentTest:draw()
end
--# Test1
Test1 = class()
function Test1:init()
-- you can accept and set parameters here
self:reset()
end
function Test1:reset()
self.firstflipdone = function()
self.anim2 = Animator(-90, 0, 0.5, EASEOUT, self.secondflipdone)
self.flipstate = 1
end
self.secondflipdone = function()
self.flipstate = 2
end
self.anim1 = Animator(0, 90, 0.5, EASEIN, self.firstflipdone)
self.flipstate = 0
end
function Test1:draw()
-- Codea does not automatically call this method
-- This sets a dark background color
background(0, 0, 0, 255)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
pushMatrix()
pushStyle()
rectMode(CENTER)
if self.flipstate == 0 then
perspective()
translate(0, 0, -930)
fill(255, 0, 0, 255)
rotate(self.anim1:value(), 1, 0, 0)
rect(0, 0, 200, 200)
fill(0, 0, 0, 255)
translate(0, 0, 1)
text("Red!", 0,0)
elseif self.flipstate == 1 then
perspective()
translate(0, 0, -930)
fill(0, 131, 255, 255)
rotate(self.anim2:value(), 1, 0, 0)
rect(0, 0, 200, 200)
fill(0, 0, 0, 255)
translate(0, 0, 1)
text("Blue!", 0,0)
else
ortho()
translate(WIDTH/2, HEIGHT/2)
fill(0, 131, 255, 255)
rect(0, 0, 200, 200)
fill(0, 0, 0, 255)
text("Blue!", 0,0)
end
popStyle()
popMatrix()
end
--# Test2
Test2 = class()
function Test2:init()
-- you can accept and set parameters here
self:reset()
end
function Test2:reset()
self.dokey1 = function() self:key1() end
self.dokey2 = function() self:key2() end
self.dokey3 = function() self:key3() end
self.dokey1()
end
function Test2:draw()
-- Codea does not automatically call this method
background(0, 0, 0, 255)
ortho()
pushMatrix()
translate(200+self.translateAnim:value(), 200)
scale(self.scaleAnim:value(), self.scaleAnim:value())
fill(255, 255, 255, 255)
rectMode(CENTER)
rect(0, 0, 100, 100)
popMatrix()
end
function Test2:key1()
self.translateAnim = Animator(50, 100, 0.5, EASEIN, self.dokey2)
self.scaleAnim = Animator(1, 0.7, 0.5, EASEIN)
end
function Test2:key2()
self.translateAnim = Animator(100, 300, 1, EASENONE, self.dokey3)
self.scaleAnim = Animator(0.7, 2, 0.5, EASEOUT)
end
function Test2:key3()
self.translateAnim = Animator(300, 50, 1.5, EASEBOTH, self.dokey1)
self.scaleAnim = Animator(2, 1, 0.5, EASEIN)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment