|
| 1 | +# easings.py |
| 2 | + |
| 3 | +# Useful easing functions for values animation |
| 4 | +# |
| 5 | +# How to use: |
| 6 | +# The four inputs t, b, c, d are defined as follows: |
| 7 | +# t = current time (in any unit measure, but same unit as duration) |
| 8 | +# b = starting value to interpolate |
| 9 | +# c = the total change in value of b that needs to occur |
| 10 | +# d = total time it should take to complete (duration) |
| 11 | +# |
| 12 | +# Example: |
| 13 | +# |
| 14 | +# current_time: int = 0; |
| 15 | +# duration: int = 100; |
| 16 | +# start_position_x: float = 0.0 |
| 17 | +# final_position_x: float = 30.0 |
| 18 | +# current_position_x: float = start_position_x |
| 19 | +# |
| 20 | +# while currentPositionX < finalPositionX: |
| 21 | +# current_positionX = ease_sine_in(current_time, start_position_x, final_position_x - start_position_x, duration) |
| 22 | +# current_time += 1 |
| 23 | +# |
| 24 | +# A port of Robert Penner's easing equations to C (http://robertpenner.com/easing/) |
| 25 | + |
| 26 | +# Robert Penner License |
| 27 | +# --------------------------------------------------------------------------------- |
| 28 | +# Open source under the BSD License. |
| 29 | +# |
| 30 | +# Copyright (c) 2001 Robert Penner. All rights reserved. |
| 31 | +# |
| 32 | +# Redistribution and use in source and binary forms, with or without modification, |
| 33 | +# are permitted provided that the following conditions are met: |
| 34 | +# |
| 35 | +# - Redistributions of source code must retain the above copyright notice, |
| 36 | +# this list of conditions and the following disclaimer. |
| 37 | +# - Redistributions in binary form must reproduce the above copyright notice, |
| 38 | +# this list of conditions and the following disclaimer in the documentation |
| 39 | +# and/or other materials provided with the distribution. |
| 40 | +# - Neither the name of the author nor the names of contributors may be used |
| 41 | +# to endorse or promote products derived from this software without specific |
| 42 | +# prior written permission. |
| 43 | +# |
| 44 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 45 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 46 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 47 | +# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 48 | +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 49 | +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 50 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 51 | +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 52 | +# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 53 | +# OF THE POSSIBILITY OF SUCH DAMAGE. |
| 54 | +# --------------------------------------------------------------------------------- |
| 55 | + |
| 56 | +from math import ( |
| 57 | + sin, |
| 58 | + cos, |
| 59 | + sqrt, |
| 60 | + asin, |
| 61 | + pi |
| 62 | +) |
| 63 | + |
| 64 | + |
| 65 | +__all__ = [ |
| 66 | + 'ease_linear_none', |
| 67 | + 'ease_linear_in', |
| 68 | + 'ease_linear_out', |
| 69 | + 'ease_linear_in_out', |
| 70 | + 'ease_sine_in', |
| 71 | + 'ease_sine_out', |
| 72 | + 'ease_sine_in_out', |
| 73 | + 'ease_circ_in', |
| 74 | + 'ease_circ_out', |
| 75 | + 'ease_circ_in_out', |
| 76 | + 'ease_cubic_in', |
| 77 | + 'ease_cubic_out', |
| 78 | + 'ease_cubic_in_out', |
| 79 | + 'ease_quad_in', |
| 80 | + 'ease_quad_out', |
| 81 | + 'ease_quad_in_out', |
| 82 | + 'ease_expo_in', |
| 83 | + 'ease_expo_out', |
| 84 | + 'ease_expo_in_out', |
| 85 | + 'ease_back_in', |
| 86 | + 'ease_back_out', |
| 87 | + 'ease_back_in_out', |
| 88 | + 'ease_bounce_in', |
| 89 | + 'ease_bounce_out', |
| 90 | + 'ease_bounce_in_out', |
| 91 | + 'ease_elastic_in', |
| 92 | + 'ease_elastic_out', |
| 93 | + 'ease_elastic_in_out', |
| 94 | +] |
| 95 | + |
| 96 | + |
| 97 | +# Linear Easing functions |
| 98 | +def ease_linear_none(t: float, b: float, c: float, d: float) -> float: |
| 99 | + return c * t / d + b |
| 100 | + |
| 101 | + |
| 102 | +def ease_linear_in(t: float, b: float, c: float, d: float) -> float: |
| 103 | + return c * t / d + b |
| 104 | + |
| 105 | + |
| 106 | +def ease_linear_out(t: float, b: float, c: float, d: float) -> float: |
| 107 | + return c * t / d + b |
| 108 | + |
| 109 | + |
| 110 | +def ease_linear_in_out(t: float, b: float, c: float, d: float) -> float: |
| 111 | + return c * t / d + b |
| 112 | + |
| 113 | + |
| 114 | +# Sine Easing functions |
| 115 | +def ease_sine_in(t: float, b: float, c: float, d: float) -> float: |
| 116 | + return -c * cos(t / d * (pi / 2)) + c + b |
| 117 | + |
| 118 | + |
| 119 | +def ease_sine_out(t: float, b: float, c: float, d: float) -> float: |
| 120 | + return c * sin(t / d * (pi / 2)) + b |
| 121 | + |
| 122 | + |
| 123 | +def ease_sine_in_out(t: float, b: float, c: float, d: float) -> float: |
| 124 | + return -c / 2 * (cos(pi * t / d) - 1) + b |
| 125 | + |
| 126 | + |
| 127 | +# Circular Easing functions |
| 128 | +def ease_circ_in(t: float, b: float, c: float, d: float) -> float: |
| 129 | + t /= d |
| 130 | + return -c * (sqrt(1 - t ** 2) - 1) + b |
| 131 | + |
| 132 | + |
| 133 | +def ease_circ_out(t: float, b: float, c: float, d: float) -> float: |
| 134 | + t = t / d - 1 |
| 135 | + return c * sqrt(1 - t ** 2) + b |
| 136 | + |
| 137 | + |
| 138 | +def ease_circ_in_out(t: float, b: float, c: float, d: float) -> float: |
| 139 | + t = t / d * 2 |
| 140 | + if t < 1: |
| 141 | + return -c / 2 * (sqrt(1 - t * t) - 1) + b |
| 142 | + else: |
| 143 | + t = t - 2 |
| 144 | + return c / 2 * (sqrt(1 - t * t) + 1) + b |
| 145 | + |
| 146 | + |
| 147 | +# Cubic Easing functions |
| 148 | +def ease_cubic_in(t: float, b: float, c: float, d: float) -> float: |
| 149 | + t = t / d |
| 150 | + return c * (t ** 3) + b |
| 151 | + |
| 152 | + |
| 153 | +def ease_cubic_out(t: float, b: float, c: float, d: float) -> float: |
| 154 | + t = t / d - 1 |
| 155 | + return c * ((t ** 3) + 1) + b |
| 156 | + |
| 157 | + |
| 158 | +def ease_cubic_in_out(t: float, b: float, c: float, d: float) -> float: |
| 159 | + t = t / d * 2 |
| 160 | + if t < 1: |
| 161 | + return c / 2 * t * t * t + b |
| 162 | + else: |
| 163 | + t = t - 2 |
| 164 | + return c / 2 * (t * t * t + 2) + b |
| 165 | + |
| 166 | + |
| 167 | +# Quadratic Easing functions |
| 168 | +def ease_quad_in(t: float, b: float, c: float, d: float) -> float: |
| 169 | + t = t / d |
| 170 | + return c * (t ** 2) + b |
| 171 | + |
| 172 | + |
| 173 | +def ease_quad_out(t: float, b: float, c: float, d: float) -> float: |
| 174 | + t = t / d |
| 175 | + return -c * t * (t - 2) + b |
| 176 | + |
| 177 | + |
| 178 | +def ease_quad_in_out(t: float, b: float, c: float, d: float) -> float: |
| 179 | + t = t / d * 2 |
| 180 | + if t < 1: |
| 181 | + return c / 2 * (t ** 2) + b |
| 182 | + else: |
| 183 | + return -c / 2 * ((t - 1) * (t - 3) - 1) + b |
| 184 | + |
| 185 | + |
| 186 | +# Exponential |
| 187 | +def ease_expo_in(t: float, b: float, c: float, d: float) -> float: |
| 188 | + if t == 0.: |
| 189 | + return b |
| 190 | + else: |
| 191 | + return c * (2 ** (10 * (t / d - 1))) + b - c * 0.001 |
| 192 | + |
| 193 | + |
| 194 | +def ease_expo_out(t: float, b: float, c: float, d: float) -> float: |
| 195 | + if t == d: |
| 196 | + return b + c |
| 197 | + else: |
| 198 | + return c * 1.001 * (-(2 ** (-10) * t / d) + 1) + b |
| 199 | + |
| 200 | + |
| 201 | +def ease_expo_in_out(t: float, b: float, c: float, d: float) -> float: |
| 202 | + if t == 0: |
| 203 | + return b |
| 204 | + if t == d: |
| 205 | + return b + c |
| 206 | + t = t / d * 2 |
| 207 | + if t < 1: |
| 208 | + return c / 2 * (2 ** (10 * (t - 1))) + b - c * 0.0005 |
| 209 | + else: |
| 210 | + t = t - 1 |
| 211 | + return c / 2 * 1.0005 * (-(2 ** (-10 * t)) + 2) + b |
| 212 | + |
| 213 | + |
| 214 | +# Back Easing functions |
| 215 | +def ease_back_in(t: float, b: float, c: float, d: float) -> float: |
| 216 | + s: float = 1.70158 |
| 217 | + t = t / d |
| 218 | + return c * t * t *((s + 1) * t - s) + b |
| 219 | + |
| 220 | + |
| 221 | +def ease_back_out(t: float, b: float, c: float, d: float) -> float: |
| 222 | + s: float = 1.70158 |
| 223 | + t = t / d - 1 |
| 224 | + return c * (t * t * ((s + 1) * t + s) + 1) + b |
| 225 | + |
| 226 | + |
| 227 | +def ease_back_in_out(t: float, b: float, c: float, d: float) -> float: |
| 228 | + s: float = 1.70158 |
| 229 | + s = s *1.525 |
| 230 | + t = t / d * 2 |
| 231 | + if t < 1: |
| 232 | + return c / 2 * (t * t * ((s + 1) * t - s)) + b |
| 233 | + else: |
| 234 | + t = t - 2 |
| 235 | + return c / 2 * (t * t * ((s + 1) * t + s) + 2) + b |
| 236 | + |
| 237 | + |
| 238 | +# Ease Bounce functions |
| 239 | +def ease_bounce_in(t: float, b: float, c: float, d: float) -> float: |
| 240 | + return c - ease_bounce_out(d - t, 0, c, d) + b |
| 241 | + |
| 242 | + |
| 243 | +def ease_bounce_out(t: float, b: float, c: float, d: float) -> float: |
| 244 | + t = t / d |
| 245 | + m = 7.5625 |
| 246 | + n = 2.75 |
| 247 | + if t < 1 / n: |
| 248 | + return c * (m * t * t) + b |
| 249 | + elif t < 2 / n: |
| 250 | + t = t - (1.5 / 2.75) |
| 251 | + return c * (m * t * t + 0.75) + b |
| 252 | + elif t < 2.5 / n: |
| 253 | + t = t - (2.25 / n) |
| 254 | + return c * (m * t * t + 0.9375) + b |
| 255 | + else: |
| 256 | + t = t - (2.625 / n) |
| 257 | + return c * (m * t * t + 0.984375) + b |
| 258 | + |
| 259 | + |
| 260 | +def ease_bounce_in_out(t: float, b: float, c: float, d: float) -> float: |
| 261 | + if t < d / 2: |
| 262 | + return ease_bounce_in(t * 2, 0, c, d) * 0.5 + b |
| 263 | + else: |
| 264 | + return ease_bounce_out(t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b |
| 265 | + |
| 266 | + |
| 267 | +# Ease Elastic functions |
| 268 | +def ease_elastic_in(t: float, b: float, c: float, d: float) -> float: |
| 269 | + p: float = d * 0.3 |
| 270 | + a: float = c |
| 271 | + s: float = p / (2 * pi) * asin(c / a) |
| 272 | + if t == 0.: |
| 273 | + return b |
| 274 | + t = t / d |
| 275 | + if t == 1: |
| 276 | + return b + c |
| 277 | + if a < abs(c): |
| 278 | + a = c |
| 279 | + s = p / 4 |
| 280 | + |
| 281 | + t = t - 1 |
| 282 | + return -(a * (2 ** (10 * t)) * sin((t * d - s) * (2 * pi) / p)) * b |
| 283 | + |
| 284 | + |
| 285 | +def ease_elastic_out(t: float, b: float, c: float, d: float) -> float: |
| 286 | + p: float = d * 0.3 |
| 287 | + a: float = c |
| 288 | + s: float = p / (2 * pi) * asin(c / a) |
| 289 | + if t == 0.: |
| 290 | + return b |
| 291 | + t = t / d |
| 292 | + if t == 1: |
| 293 | + return b + c |
| 294 | + if a < abs(c): |
| 295 | + a = c |
| 296 | + s = p / 4 |
| 297 | + |
| 298 | + return a * (2 ** (-10 * t)) * sin((t * d - s) * (2 * pi ) / p) + c + b |
| 299 | + |
| 300 | + |
| 301 | +def ease_elastic_in_out(t: float, b: float, c: float, d: float) -> float: |
| 302 | + if t == 0: |
| 303 | + return b |
| 304 | + t = t / d * 2 |
| 305 | + if t == 2: |
| 306 | + return b + c |
| 307 | + |
| 308 | + p: float = d * (0.3 * 1.5) |
| 309 | + a: float = 0.0 |
| 310 | + s: float = p / (2 * pi) * asin(c / a) |
| 311 | + |
| 312 | + if a < abs(c): |
| 313 | + a = c |
| 314 | + s = p / 4 |
| 315 | + |
| 316 | + if t < 1: |
| 317 | + t = t - 1 |
| 318 | + return -0.5 * (a * (2 ** (10 * t)) * sin((t * d - s) * (2 * pi) / p)) + b |
| 319 | + else: |
| 320 | + t = t - 1 |
| 321 | + return a * (2 ** (-10 * t)) * sin((t * d - s) * (2 * pi) / p) * 0.5 + c + b |
0 commit comments