Reputation: 187
Someone shared their code and I saw a bunch of functions that were stored in what seemed to me to be a dictionary and. So, I liked the idea and I borrowed them. The code that the person wrote it in was in JS, and I work with Python, so I translated the code into Python.
Here is what that person wrote in JS:
EasingFunctions = {
// no easing, no acceleration
linear: t => t,
// accelerating from zero velocity
easeInQuad: t => t * t,
// decelerating to zero velocity
easeOutQuad: t => t * (2 - t),
// acceleration until halfway, then deceleration
easeInOutQuad: t => t < .5 ? 2 * t * t : -1 + (4 - 2 * t) * t,
// accelerating from zero velocity
easeInCubic: t => t * t * t,
// decelerating to zero velocity
easeOutCubic: t => (--t) * t * t + 1,
// acceleration until halfway, then deceleration
easeInOutCubic: t => t < .5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
// accelerating from zero velocity
easeInQuart: t => t * t * t * t,
// decelerating to zero velocity
easeOutQuart: t => 1 - (--t) * t * t * t,
// acceleration until halfway, then deceleration
easeInOutQuart: t => t < .5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t,
// accelerating from zero velocity
easeInQuint: t => t * t * t * t * t,
// decelerating to zero velocity
easeOutQuint: t => 1 + (--t) * t * t * t * t,
// acceleration until halfway, then deceleration
easeInOutQuint: t => t < .5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t
}
And it works fine if you ran this code. However in the code I translated, it gave me an error saying that I miss a paranthesis, comma, or a colon. Here is the code:
EasingFunctions = {
# no easing, no acceleration
linear: lambda t : t,
# accelerating from zero velocity
easeInQuad: lambda t : t ** 2,
# decelerating to zero velocity
easeOutQuad: lambda t : t * (2-t),
# acceleration until halfway, then deceleration
easeInOutQuad: (lambda t : t = (2*(t**2)) if t < 0.5 else ((-1+(4-2*t)) * t)),
# accelerating from zero velocity
easeInCubic: lambda t : t * t * t,
# decelerating to zero velocity
easeOutCubic: lambda t : (t-1) * t * t + 1,
# acceleration until halfway, then deceleration
easeInOutCubic: lambda t : t = 4*t*t*t if t < 0.5 else (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
# accelerating from zero velocity
easeInQuart: lambda t : t ** 4,
# decelerating to zero velocity
easeOutQuart: lambda t : 1 - (t-1) * t * t * t,
# acceleration until halfway, then deceleration
easeInOutQuart: lambda t : t = 8 * t * t * t * t if t < 0.5 else 1 - 8 * (t) * t * t * t
# accelerating from zero velocity
easeInQuint: lambda t : t ** 5,
# decelerating to zero velocity
easeOutQuint: lambda t : 1 + (t-1) * t * t * t * t,
# acceleration until halfway, then deceleration
easeInOutQuint: lambda t : t = 16 * t * t * t * t * t if t < 0.5 else 1 + 16 * (t-1) * t * t * t * t
}
And what confused me is that the error was indicated to be the first key value that had an if
statement in it. I thought that this was allowed in Python, what is wrong with the code?
Upvotes: 0
Views: 3068
Reputation: 722
As you mentioned in the comments that you still can't figure out how to do it using string keys for dictionary, I'm posting this answer. Though, it was partially mentioned in the comments how to do this.
a = {
'linear': lambda t: t,
'easeInQuad': lambda t: t ** 2,
'easeOutQuad': lambda t: t * (2-t),
'easeOutQuint': lambda t: 1 + (t - 1) * t * t * t * t,
}
print(a['linear'](69))
print(a['easeInQuad'](69))
print(a['easeOutQuad'](69))
print(a['easeOutQuint'](69))
Result:
69
4761
-4623
1541364229
Again, as mentioned in comments, Python doesn't support --
operation. Hope this helps.
Upvotes: 1