pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/easing/elastic.go (about) 1 package easing 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "math" 12 ) 13 14 // ////////////////////////////////////////////////////////////////////////////////// // 15 16 // ElasticIn accelerating from zero velocity 17 // https://easings.net/#easeInElastic 18 func ElasticIn(t, b, c, d float64) float64 { 19 if t > d { 20 return c 21 } 22 23 s := math.SqrtPi 24 p := d * 0.3 25 a := c 26 27 if t == 0 { 28 return b 29 } 30 31 t /= d 32 33 if t == 1 { 34 return b + c 35 } 36 37 if a < math.Abs(c) { 38 s = p / 4 39 } else { 40 s = p / DoublePi * math.Asin(c/a) 41 } 42 43 t-- 44 45 return -(a * math.Pow(2, 10*t) * math.Sin((t*d-s)*DoublePi/p)) + b 46 } 47 48 // ElasticOut decelerating to zero velocity 49 // https://easings.net/#easeOutElastic 50 func ElasticOut(t, b, c, d float64) float64 { 51 if t > d { 52 return c 53 } 54 55 s := math.SqrtPi 56 p := d * 0.3 57 a := c 58 59 if t == 0 { 60 return b 61 } 62 63 t /= d 64 65 if t == 1 { 66 return b + c 67 } 68 69 if a < math.Abs(c) { 70 s = p / 4 71 } else { 72 s = p / DoublePi * math.Asin(c/a) 73 } 74 75 return a*math.Pow(2, -10*t)*math.Sin((t*d-s)*DoublePi/p) + c + b 76 } 77 78 // ElasticInOut acceleration until halfway, then deceleration 79 // https://easings.net/#easeInOutElastic 80 func ElasticInOut(t, b, c, d float64) float64 { 81 if t > d { 82 return c 83 } 84 85 s := math.SqrtPi 86 p := d * (0.3 * 1.5) 87 a := c 88 89 if t == 0 { 90 return b 91 } 92 93 t /= d / 2 94 95 if t == 2 { 96 return b + c 97 } 98 99 if a < math.Abs(c) { 100 s = p / 4 101 } else { 102 s = p / DoublePi * math.Asin(c/a) 103 } 104 105 t-- 106 107 if t < 0 { 108 return -0.5*(a*math.Pow(2, 10*t)*math.Sin((t*d-s)*DoublePi/p)) + b 109 } 110 111 return a*math.Pow(2, -10*t)*math.Sin((t*d-s)*DoublePi/p)*0.5 + c + b 112 }