Skip to content

Instantly share code, notes, and snippets.

@typemytype
Created October 15, 2020 19:43
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 typemytype/e39e68f53ad145993b96a85a9827504b to your computer and use it in GitHub Desktop.
Save typemytype/e39e68f53ad145993b96a85a9827504b to your computer and use it in GitHub Desktop.
from math import sqrt
class AnimationFrame(object):
def __init__(self, index, factor):
self._index = index
self._factor = factor
def index(self):
return self._index
def factor(self):
return self._factor
def linearFactor(self, b=0, c=1, d=1):
t = self._factor
return c * t / d + b
def easeInQuadFactor(self, b=0, c=1, d=1):
t = self._factor
t /= d
return c * t * t + b
def easeOutQuadFactor(self, b=0, c=1, d=1):
t = self._factor
t /= d
return -c * t * (t - 2) + b
def easeInOutQuadFactor(self, b=0, c=1, d=1):
t = self._factor
t /= d / 2
if t < 1:
return c / 2 * t * t + b
t -= 1
return -c / 2 * (t * (t - 2) - 1) + b
def easeInCubicFactor(self, b=0, c=1, d=1):
t = self._factor
t /= d
return c * t * t * t + b
def easeOutCubicFactor(self, b=0, c=1, d=1):
t = self._factor
t /= d
t -= 1
return c * (t * t * t + 1) + b
def easeInOutCubicFactor(self, b=0, c=1, d=1):
t = self._factor
t /= d / 2
if t < 1:
return c / 2 * t * t * t + b
t -= 2
return c / 2 * (t * t * t + 2) + b
def easeInOutCircFactor(self, b=0, c=1, d=1):
t = self._factor
t /= d / 2
if t < 1:
return -c / 2 * (sqrt(1 - t * t) - 1) + b
t -= 2
return c / 2 * (sqrt(1 - t * t) + 1) + b
def animation(frames=10, size=(100, 100), backgroundColor=(1, 1, 1), loop=True):
framesIndexes = range(frames)
if loop:
framesIndexes = list(framesIndexes) + list(reversed(framesIndexes[1:-1]))
def wrapper(func):
width, height = size
for frameIndex in framesIndexes:
newPage(width, height)
with savedState():
fill(*backgroundColor)
rect(0, 0, width, height)
factor = frameIndex / (frames - 1)
func(AnimationFrame(frameIndex, factor))
return wrapper
##############
@animation(frames=10, size=(50, 50), backgroundColor=(1, 0, 1))
def render(frame):
fill(1, 0, 0)
rect(0, 0, width() * frame.easeInOutCircFactor(), height())
fontSize(50)
fill(frame.easeInQuadFactor(), 1, 0)
text(str(frame.index()), (10, 10))
saveImage("test.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment