|
| 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 linear_interpolation(pos): |
| 80 | + """ |
| 81 | + Easing function for animations: Linear Interpolation. |
| 82 | + """ |
| 83 | + return pos |
| 84 | + |
| 85 | + |
| 86 | +# Modeled after the parabola y = x^2 |
| 87 | +def quadratic_easein(pos): |
| 88 | + """ |
| 89 | + Easing function for animations: Quadratic Ease In |
| 90 | + """ |
| 91 | + return pos * pos |
| 92 | + |
| 93 | + |
| 94 | +# Modeled after the parabola y = -x^2 + 2x |
| 95 | +def quadratic_easeout(pos): |
| 96 | + """ |
| 97 | + Easing function for animations: Quadratic Ease Out. |
| 98 | + """ |
| 99 | + return -(pos * (pos - 2)) |
| 100 | + |
| 101 | + |
| 102 | +# Modeled after the piecewise quadratic |
| 103 | +# y = (1/2)((2x)^2) ; [0, 0.5) |
| 104 | +# y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1] |
| 105 | +def quadratic_easeinout(pos): |
| 106 | + """ |
| 107 | + Easing function for animations: Quadratic Ease In & Out |
| 108 | + """ |
| 109 | + if pos < 0.5: |
| 110 | + return 2 * pos * pos |
| 111 | + return (-2 * pos * pos) + (4 * pos) - 1 |
| 112 | + |
| 113 | + |
| 114 | +# Modeled after the cubic y = x^3 |
| 115 | +def cubic_easein(pos): |
| 116 | + """ |
| 117 | + Easing function for animations: Cubic Ease In |
| 118 | + """ |
| 119 | + return pos * pos * pos |
| 120 | + |
| 121 | + |
| 122 | +# Modeled after the cubic y = (x - 1)^3 + 1 |
| 123 | +def cubic_easeout(pos): |
| 124 | + """ |
| 125 | + Easing function for animations: Cubic Ease Out |
| 126 | + """ |
| 127 | + fos = pos - 1 |
| 128 | + return fos * fos * fos + 1 |
| 129 | + |
| 130 | + |
| 131 | +# Modeled after the piecewise cubic |
| 132 | +# y = (1/2)((2x)^3) ; [0, 0.5) |
| 133 | +# y = (1/2)((2x-2)^3 + 2) ; [0.5, 1] |
| 134 | +def cubic_easeinout(pos): |
| 135 | + """ |
| 136 | + Easing function for animations: Cubic Ease In & Out |
| 137 | + """ |
| 138 | + if pos < 0.5: |
| 139 | + return 4 * pos * pos * pos |
| 140 | + fos = (2 * pos) - 2 |
| 141 | + return 0.5 * fos * fos * fos + 1 |
| 142 | + |
| 143 | + |
| 144 | +# Modeled after the quartic x^4 |
| 145 | +def quartic_easein(pos): |
| 146 | + """ |
| 147 | + Easing function for animations: Quartic Ease In |
| 148 | + """ |
| 149 | + return pos * pos * pos * pos |
| 150 | + |
| 151 | + |
| 152 | +# Modeled after the quartic y = 1 - (x - 1)^4 |
| 153 | +def quartic_easeout(pos): |
| 154 | + """ |
| 155 | + Easing function for animations: Quartic Ease Out |
| 156 | + """ |
| 157 | + fos = pos - 1 |
| 158 | + return fos * fos * fos * (1 - pos) + 1 |
| 159 | + |
| 160 | + |
| 161 | +# Modeled after the piecewise quartic |
| 162 | +# y = (1/2)((2x)^4) ; [0, 0.5) |
| 163 | +# y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1] |
| 164 | +def quartic_easeinout(pos): |
| 165 | + """ |
| 166 | + Easing function for animations: Quartic Ease In & Out |
| 167 | + """ |
| 168 | + if pos < 0.5: |
| 169 | + return 8 * pos * pos * pos * pos |
| 170 | + fos = pos - 1 |
| 171 | + return -8 * fos * fos * fos * fos + 1 |
| 172 | + |
| 173 | + |
| 174 | +# Modeled after the quintic y = x^5 |
| 175 | +def quintic_easein(pos): |
| 176 | + """ |
| 177 | + Easing function for animations: Quintic Ease In |
| 178 | + """ |
| 179 | + return pos * pos * pos * pos * pos |
| 180 | + |
| 181 | + |
| 182 | +# Modeled after the quintic y = (x - 1)^5 + 1 |
| 183 | +def quintic_easeout(pos): |
| 184 | + """ |
| 185 | + Easing function for animations: Quintic Ease Out |
| 186 | + """ |
| 187 | + fos = pos - 1 |
| 188 | + return fos * fos * fos * fos * fos + 1 |
| 189 | + |
| 190 | + |
| 191 | +# Modeled after the piecewise quintic |
| 192 | +# y = (1/2)((2x)^5) ; [0, 0.5) |
| 193 | +# y = (1/2)((2x-2)^5 + 2) ; [0.5, 1] |
| 194 | +def quintic_easeinout(pos): |
| 195 | + """ |
| 196 | + Easing function for animations: Quintic Ease In & Out |
| 197 | + """ |
| 198 | + if pos < 0.5: |
| 199 | + return 16 * pos * pos * pos * pos * pos |
| 200 | + fos = (2 * pos) - 2 |
| 201 | + return 0.5 * fos * fos * fos * fos * fos + 1 |
| 202 | + |
| 203 | + |
| 204 | +# Modeled after quarter-cycle of sine wave |
| 205 | +def sine_easein(pos): |
| 206 | + """ |
| 207 | + Easing function for animations: Sine Ease In |
| 208 | + """ |
| 209 | + return math.sin((pos - 1) * math.pi / 2) + 1 |
| 210 | + |
| 211 | + |
| 212 | +# Modeled after quarter-cycle of sine wave (different phase) |
| 213 | +def sine_easeout(pos): |
| 214 | + """ |
| 215 | + Easing function for animations: Sine Ease Out |
| 216 | + """ |
| 217 | + return math.sin(pos * math.pi / 2) |
| 218 | + |
| 219 | + |
| 220 | +# Modeled after half sine wave |
| 221 | +def sine_easeinout(pos): |
| 222 | + """ |
| 223 | + Easing function for animations: Sine Ease In & Out |
| 224 | + """ |
| 225 | + return 0.5 * (1 - math.cos(pos * math.pi)) |
| 226 | + |
| 227 | + |
| 228 | +# Modeled after shifted quadrant IV of unit circle |
| 229 | +def circular_easein(pos): |
| 230 | + """ |
| 231 | + Easing function for animations: Circular Ease In |
| 232 | + """ |
| 233 | + return 1 - math.sqrt(1 - (pos * pos)) |
| 234 | + |
| 235 | + |
| 236 | +# Modeled after shifted quadrant II of unit circle |
| 237 | +def circular_easeout(pos): |
| 238 | + """ |
| 239 | + Easing function for animations: Circular Ease Out |
| 240 | + """ |
| 241 | + return math.sqrt((2 - pos) * pos) |
| 242 | + |
| 243 | + |
| 244 | +# Modeled after the piecewise circular function |
| 245 | +# y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5) |
| 246 | +# y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1] |
| 247 | +def circular_easeinout(pos): |
| 248 | + """ |
| 249 | + Easing function for animations: Circular Ease In & Out |
| 250 | + """ |
| 251 | + if pos < 0.5: |
| 252 | + return 0.5 * (1 - math.sqrt(1 - 4 * (pos * pos))) |
| 253 | + return 0.5 * (math.sqrt(-((2 * pos) - 3) * ((2 * pos) - 1)) + 1) |
| 254 | + |
| 255 | + |
| 256 | +# Modeled after the exponential function y = 2^(10(x - 1)) |
| 257 | +def exponential_easein(pos): |
| 258 | + """ |
| 259 | + Easing function for animations: Exponential Ease In |
| 260 | + """ |
| 261 | + if pos == 0: |
| 262 | + return pos |
| 263 | + return math.pow(2, 10 * (pos - 1)) |
| 264 | + |
| 265 | + |
| 266 | +# Modeled after the exponential function y = -2^(-10x) + 1 |
| 267 | +def exponential_easeout(pos): |
| 268 | + """ |
| 269 | + Easing function for animations: Exponential Ease Out |
| 270 | + """ |
| 271 | + if pos == 1: |
| 272 | + return pos |
| 273 | + return 1 - math.pow(2, -10 * pos) |
| 274 | + |
| 275 | + |
| 276 | +# Modeled after the piecewise exponential |
| 277 | +# y = (1/2)2^(10(2x - 1)) ; [0,0.5) |
| 278 | +# y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1] |
| 279 | +def exponential_easeinout(pos): |
| 280 | + """ |
| 281 | + Easing function for animations: Exponential Ease In & Out |
| 282 | + """ |
| 283 | + if pos in (0.0, 1.0): |
| 284 | + return pos |
| 285 | + if pos < 0.5: |
| 286 | + return 0.5 * math.pow(2, (20 * pos) - 10) |
| 287 | + return (-0.5 * math.pow(2, (-20 * pos) + 10)) + 1 |
| 288 | + |
| 289 | + |
| 290 | +# Modeled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x - 1)) |
| 291 | +def elastic_easein(pos): |
| 292 | + """ |
| 293 | + Easing function for animations: Elastic Ease In |
| 294 | + """ |
| 295 | + return math.sin(13 * pos * math.pi / 2) * math.pow(2, 10 * (pos - 1)) |
| 296 | + |
| 297 | + |
| 298 | +# Modeled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2, -10x) + 1 |
| 299 | +def elastic_easeout(pos): |
| 300 | + """ |
| 301 | + Easing function for animations: Elastic Ease Out |
| 302 | + """ |
| 303 | + return math.sin(-13 * math.pi / 2 * (pos + 1)) * math.pow(2, -10 * pos) + 1 |
| 304 | + |
| 305 | + |
| 306 | +# Modeled after the piecewise exponentially-damped sine wave: |
| 307 | +# y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5) |
| 308 | +# y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1] |
| 309 | +def elastic_easeinout(pos): |
| 310 | + """ |
| 311 | + Easing function for animations: Elastic Ease In & Out |
| 312 | + """ |
| 313 | + if pos < 0.5: |
| 314 | + return 0.5 * math.sin(13 * math.pi * pos) * math.pow(2, 10 * ((2 * pos) - 1)) |
| 315 | + return 0.5 * ( |
| 316 | + math.sin(-13 * math.pi / 2 * ((2 * pos - 1) + 1)) * pow(2, -10 * (2 * pos - 1)) |
| 317 | + + 2 |
| 318 | + ) |
| 319 | + |
| 320 | + |
| 321 | +# Modeled after the overshooting cubic y = x^3-x*sin(x*pi) |
| 322 | +def back_easein(pos): |
| 323 | + """ |
| 324 | + Easing function for animations: Back Ease In |
| 325 | + """ |
| 326 | + return pos * pos * pos - pos * math.sin(pos * math.pi) |
| 327 | + |
| 328 | + |
| 329 | +# Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi)) |
| 330 | +def back_easeout(pos): |
| 331 | + """ |
| 332 | + Easing function for animations: Back Ease Out |
| 333 | + """ |
| 334 | + fos = 1 - pos |
| 335 | + return 1 - (fos * fos * fos - fos * math.sin(fos * math.pi)) |
| 336 | + |
| 337 | + |
| 338 | +# Modeled after the piecewise overshooting cubic function: |
| 339 | +# y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5) |
| 340 | +# y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1] |
| 341 | +def back_easeinout(pos): |
| 342 | + """ |
| 343 | + Easing function for animations: Back Ease In & Out |
| 344 | + """ |
| 345 | + if pos < 0.5: |
| 346 | + fos = 2 * pos |
| 347 | + return 0.5 * (fos * fos * fos - fos * math.sin(fos * math.pi)) |
| 348 | + fos = 1 - (2 * pos - 1) |
| 349 | + return 0.5 * (1 - (fos * fos * fos - fos * math.sin(fos * math.pi))) + 0.5 |
| 350 | + |
| 351 | + |
| 352 | +def bounce_easein(pos): |
| 353 | + """ |
| 354 | + Easing function for animations: Bounce Ease In |
| 355 | + """ |
| 356 | + return 1 - bounce_easeout(1 - pos) |
| 357 | + |
| 358 | + |
| 359 | +def bounce_easeout(pos): |
| 360 | + """ |
| 361 | + Easing function for animations: Bounce Ease Out |
| 362 | + """ |
| 363 | + if pos < 4 / 11.0: |
| 364 | + return (121 * pos * pos) / 16.0 |
| 365 | + if pos < 8 / 11.0: |
| 366 | + return (363 / 40.0 * pos * pos) - (99 / 10.0 * pos) + (17 / 5.0) |
| 367 | + if pos < 9 / 10.0: |
| 368 | + return (4356 / 361.0 * pos * pos) - (35442 / 1805.0 * pos) + 16061 / 1805.0 |
| 369 | + return (54 / 5.0 * pos * pos) - (513 / 25.0 * pos) + 268 / 25.0 |
| 370 | + |
| 371 | + |
| 372 | +def bounce_easeinout(pos): |
| 373 | + """ |
| 374 | + Easing function for animations: Bounce Ease In & Out |
| 375 | + """ |
| 376 | + if pos < 0.5: |
| 377 | + return 0.5 * bounce_easein(pos * 2) |
| 378 | + return 0.5 * bounce_easeout(pos * 2 - 1) + 0.5 |
0 commit comments