|
| 1 | +# SPDX-FileCopyrightText: 2021 Kevin Matocha |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +`easing` |
| 7 | +================================================================================ |
| 8 | +
|
| 9 | +Various easing functions in support of the Widget library. |
| 10 | +
|
| 11 | +* Author(s): Kevin Matocha |
| 12 | +
|
| 13 | +Implementation Notes |
| 14 | +-------------------- |
| 15 | +
|
| 16 | +**Hardware:** |
| 17 | +
|
| 18 | +**Software and Dependencies:** |
| 19 | +
|
| 20 | +* Adafruit CircuitPython firmware for the supported boards: |
| 21 | + https://github.com/adafruit/circuitpython/releases |
| 22 | +
|
| 23 | +""" |
| 24 | + |
| 25 | +###### |
| 26 | +# |
| 27 | +# Adapted from: https://github.com/warrenm/AHEasing |
| 28 | +# |
| 29 | +# View animated examples here: https://easings.net |
| 30 | +# |
| 31 | +##### |
| 32 | +# // |
| 33 | +# // easing.c |
| 34 | +# // |
| 35 | +# // Copyright (c) 2011, Auerhaus Development, LLC |
| 36 | +# // |
| 37 | +# // This program is free software. It comes without any warranty, to |
| 38 | +# // the extent permitted by applicable law. You can redistribute it |
| 39 | +# // and/or modify it under the terms of the Do What The Fuck You Want |
| 40 | +# // To Public License, Version 2, as published by Sam Hocevar. See |
| 41 | +# // http://sam.zoy.org/wtfpl/COPYING for more details. |
| 42 | +# // |
| 43 | +## |
| 44 | +## |
| 45 | +# The MIT License (MIT) |
| 46 | +# |
| 47 | +# Copyright (c) 2021 Kevin Matocha (kmatch98, [email protected]) |
| 48 | +# |
| 49 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 50 | +# of this software and associated documentation files (the "Software"), to deal |
| 51 | +# in the Software without restriction, including without limitation the rights |
| 52 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 53 | +# copies of the Software, and to permit persons to whom the Software is |
| 54 | +# furnished to do so, subject to the following conditions: |
| 55 | +# |
| 56 | +# The above copyright notice and this permission notice shall be included in |
| 57 | +# all copies or substantial portions of the Software. |
| 58 | +# |
| 59 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 60 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 61 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 62 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 63 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 64 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 65 | +# THE SOFTWARE. |
| 66 | +# |
| 67 | +# |
| 68 | +# Easing functions for animation motion |
| 69 | +# |
| 70 | +# Input value (p) should be between 0.0 (start point) and 1.0 (ending point). |
| 71 | +# Output values begin at 0.0 and end at 1.0 but have a specific transfer displacment function |
| 72 | +# to give the desired motion response. |
| 73 | +# |
| 74 | +# Note: Some functions return values < 0.0 or > 1.0 due "springiness". |
| 75 | + |
| 76 | +import math |
| 77 | + |
| 78 | +# Modeled after the line y = x |
| 79 | +def LinearInterpolation(p): |
| 80 | + return p |
| 81 | + |
| 82 | + |
| 83 | +# Modeled after the parabola y = x^2 |
| 84 | +def QuadraticEaseIn(p): |
| 85 | + return p * p |
| 86 | + |
| 87 | + |
| 88 | +# Modeled after the parabola y = -x^2 + 2x |
| 89 | +def QuadraticEaseOut(p): |
| 90 | + return -(p * (p - 2)) |
| 91 | + |
| 92 | + |
| 93 | +# Modeled after the piecewise quadratic |
| 94 | +# y = (1/2)((2x)^2) ; [0, 0.5) |
| 95 | +# y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1] |
| 96 | +def QuadraticEaseInOut(p): |
| 97 | + if p < 0.5: |
| 98 | + return 2 * p * p |
| 99 | + return (-2 * p * p) + (4 * p) - 1 |
| 100 | + |
| 101 | + |
| 102 | +# Modeled after the cubic y = x^3 |
| 103 | +def CubicEaseIn(p): |
| 104 | + return p * p * p |
| 105 | + |
| 106 | + |
| 107 | +# Modeled after the cubic y = (x - 1)^3 + 1 |
| 108 | +def CubicEaseOut(p): |
| 109 | + f = p - 1 |
| 110 | + return f * f * f + 1 |
| 111 | + |
| 112 | + |
| 113 | +# Modeled after the piecewise cubic |
| 114 | +# y = (1/2)((2x)^3) ; [0, 0.5) |
| 115 | +# y = (1/2)((2x-2)^3 + 2) ; [0.5, 1] |
| 116 | +def CubicEaseInOut(p): |
| 117 | + if p < 0.5: |
| 118 | + return 4 * p * p * p |
| 119 | + f = (2 * p) - 2 |
| 120 | + return 0.5 * f * f * f + 1 |
| 121 | + |
| 122 | + |
| 123 | +# Modeled after the quartic x^4 |
| 124 | +def QuarticEaseIn(p): |
| 125 | + return p * p * p * p |
| 126 | + |
| 127 | + |
| 128 | +# Modeled after the quartic y = 1 - (x - 1)^4 |
| 129 | +def QuarticEaseOut(p): |
| 130 | + f = p - 1 |
| 131 | + return f * f * f * (1 - p) + 1 |
| 132 | + |
| 133 | + |
| 134 | +# Modeled after the piecewise quartic |
| 135 | +# y = (1/2)((2x)^4) ; [0, 0.5) |
| 136 | +# y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1] |
| 137 | +def QuarticEaseInOut(p): |
| 138 | + if p < 0.5: |
| 139 | + return 8 * p * p * p * p |
| 140 | + f = p - 1 |
| 141 | + return -8 * f * f * f * f + 1 |
| 142 | + |
| 143 | + |
| 144 | +# Modeled after the quintic y = x^5 |
| 145 | +def QuinticEaseIn(p): |
| 146 | + return p * p * p * p * p |
| 147 | + |
| 148 | + |
| 149 | +# Modeled after the quintic y = (x - 1)^5 + 1 |
| 150 | +def QuinticEaseOut(p): |
| 151 | + f = p - 1 |
| 152 | + return f * f * f * f * f + 1 |
| 153 | + |
| 154 | + |
| 155 | +# Modeled after the piecewise quintic |
| 156 | +# y = (1/2)((2x)^5) ; [0, 0.5) |
| 157 | +# y = (1/2)((2x-2)^5 + 2) ; [0.5, 1] |
| 158 | +def QuinticEaseInOut(p): |
| 159 | + if p < 0.5: |
| 160 | + return 16 * p * p * p * p * p |
| 161 | + f = (2 * p) - 2 |
| 162 | + return 0.5 * f * f * f * f * f + 1 |
| 163 | + |
| 164 | + |
| 165 | +# Modeled after quarter-cycle of sine wave |
| 166 | +def SineEaseIn(p): |
| 167 | + return math.sin((p - 1) * math.pi / 2) + 1 |
| 168 | + |
| 169 | + |
| 170 | +# Modeled after quarter-cycle of sine wave (different phase) |
| 171 | +def SineEaseOut(p): |
| 172 | + return math.sin(p * math.pi / 2) |
| 173 | + |
| 174 | + |
| 175 | +# Modeled after half sine wave |
| 176 | +def SineEaseInOut(p): |
| 177 | + return 0.5 * (1 - math.cos(p * math.pi)) |
| 178 | + |
| 179 | + |
| 180 | +# Modeled after shifted quadrant IV of unit circle |
| 181 | +def CircularEaseIn(p): |
| 182 | + return 1 - math.sqrt(1 - (p * p)) |
| 183 | + |
| 184 | + |
| 185 | +# Modeled after shifted quadrant II of unit circle |
| 186 | +def CircularEaseOut(p): |
| 187 | + return math.sqrt((2 - p) * p) |
| 188 | + |
| 189 | + |
| 190 | +# Modeled after the piecewise circular function |
| 191 | +# y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5) |
| 192 | +# y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1] |
| 193 | +def CircularEaseInOut(p): |
| 194 | + if p < 0.5: |
| 195 | + return 0.5 * (1 - math.sqrt(1 - 4 * (p * p))) |
| 196 | + return 0.5 * (math.sqrt(-((2 * p) - 3) * ((2 * p) - 1)) + 1) |
| 197 | + |
| 198 | + |
| 199 | +# Modeled after the exponential function y = 2^(10(x - 1)) |
| 200 | +def ExponentialEaseIn(p): |
| 201 | + if p == 0: |
| 202 | + return p |
| 203 | + return math.pow(2, 10 * (p - 1)) |
| 204 | + |
| 205 | + |
| 206 | +# Modeled after the exponential function y = -2^(-10x) + 1 |
| 207 | +def ExponentialEaseOut(p): |
| 208 | + if p == 1: |
| 209 | + return p |
| 210 | + return 1 - math.pow(2, -10 * p) |
| 211 | + |
| 212 | + |
| 213 | +# Modeled after the piecewise exponential |
| 214 | +# y = (1/2)2^(10(2x - 1)) ; [0,0.5) |
| 215 | +# y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1] |
| 216 | +def ExponentialEaseInOut(p): |
| 217 | + if (p == 0.0) or (p == 1.0): |
| 218 | + return p |
| 219 | + if p < 0.5: |
| 220 | + return 0.5 * math.pow(2, (20 * p) - 10) |
| 221 | + return (-0.5 * math.pow(2, (-20 * p) + 10)) + 1 |
| 222 | + |
| 223 | + |
| 224 | +# Modeled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x - 1)) |
| 225 | +def ElasticEaseIn(p): |
| 226 | + return math.sin(13 * p * math.pi / 2) * math.pow(2, 10 * (p - 1)) |
| 227 | + |
| 228 | + |
| 229 | +# Modeled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2, -10x) + 1 |
| 230 | +def ElasticEaseOut(p): |
| 231 | + return math.sin(-13 * math.pi / 2 * (p + 1)) * math.pow(2, -10 * p) + 1 |
| 232 | + |
| 233 | + |
| 234 | +# Modeled after the piecewise exponentially-damped sine wave: |
| 235 | +# y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5) |
| 236 | +# y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1] |
| 237 | +def ElasticEaseInOut(p): |
| 238 | + if p < 0.5: |
| 239 | + return 0.5 * math.sin(13 * math.pi * p) * math.pow(2, 10 * ((2 * p) - 1)) |
| 240 | + return 0.5 * ( |
| 241 | + math.sin(-13 * math.pi / 2 * ((2 * p - 1) + 1)) * pow(2, -10 * (2 * p - 1)) + 2 |
| 242 | + ) |
| 243 | + |
| 244 | + |
| 245 | +# Modeled after the overshooting cubic y = x^3-x*sin(x*pi) |
| 246 | +def BackEaseIn(p): |
| 247 | + return p * p * p - p * math.sin(p * math.pi) |
| 248 | + |
| 249 | + |
| 250 | +# Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi)) |
| 251 | +def BackEaseOut(p): |
| 252 | + f = 1 - p |
| 253 | + return 1 - (f * f * f - f * math.sin(f * math.pi)) |
| 254 | + |
| 255 | + |
| 256 | +# Modeled after the piecewise overshooting cubic function: |
| 257 | +# y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5) |
| 258 | +# y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1] |
| 259 | +def BackEaseInOut(p): |
| 260 | + if p < 0.5: |
| 261 | + f = 2 * p |
| 262 | + return 0.5 * (f * f * f - f * math.sin(f * math.pi)) |
| 263 | + f = 1 - (2 * p - 1) |
| 264 | + return 0.5 * (1 - (f * f * f - f * math.sin(f * math.pi))) + 0.5 |
| 265 | + |
| 266 | + |
| 267 | +def BounceEaseIn(p): |
| 268 | + return 1 - BounceEaseOut(1 - p) |
| 269 | + |
| 270 | + |
| 271 | +def BounceEaseOut(p): |
| 272 | + if p < 4 / 11.0: |
| 273 | + return (121 * p * p) / 16.0 |
| 274 | + if p < 8 / 11.0: |
| 275 | + return (363 / 40.0 * p * p) - (99 / 10.0 * p) + (17 / 5.0) |
| 276 | + if p < 9 / 10.0: |
| 277 | + return (4356 / 361.0 * p * p) - (35442 / 1805.0 * p) + 16061 / 1805.0 |
| 278 | + return (54 / 5.0 * p * p) - (513 / 25.0 * p) + 268 / 25.0 |
| 279 | + |
| 280 | + |
| 281 | +def BounceEaseInOut(p): |
| 282 | + if p < 0.5: |
| 283 | + return 0.5 * BounceEaseIn(p * 2) |
| 284 | + return 0.5 * BounceEaseOut(p * 2 - 1) + 0.5 |
0 commit comments