Skip to content

Commit

Permalink
Fix animation strategies IN_EXP, OUT_EXP, and IN_OUT_EXP
Browse files Browse the repository at this point in the history
These would not give the correct value at the extremes because it's an exponential curve, it doesn't actually ever get to the value, but for practical reason having it be 0 at 0 and 1 at 1 is much preferred. 

IN_OUT_EXP is fixed by implementing it in terms of IN_EXP and OUT_EXP.

GitHub: #129
  • Loading branch information
RedEpicness committed Nov 28, 2023
1 parent f4bdee3 commit 3f43410
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,23 @@ enum class Animations : AnimationStrategy {
},
IN_EXP {
override fun getValue(percentComplete: Float): Float {
return 2f.pow(10 * (percentComplete - 1))
return if (percentComplete <= 0f) 0f else 2f.pow(10 * (percentComplete - 1))
}
},
OUT_EXP {
override fun getValue(percentComplete: Float): Float {
return -(2f.pow(-10 * percentComplete)) + 1
return if (percentComplete >= 1f) 1f else -(2f.pow(-10 * percentComplete)) + 1
}
},
IN_OUT_EXP {
override fun getValue(percentComplete: Float): Float {
var t = percentComplete * 2
if (t < 1) return 0.5f * 2f.pow(10 * (t - 1))
t--
return 0.5f * (-(2f.pow(-10 * t)) + 2)
// We don't need the extra checks, as those are now handled by the functions we call
// multiply percentComplete by 2 to normalize range before calling getValue
// then divide by two to shrink it to 0.0-0.5
return if (percentComplete < 0.5) IN_EXP.getValue(percentComplete * 2) / 2
// multiply percentComplete by 2 and subtract one to normalize range before calling getValue
// then divide by two to shrink it and add 0.5 to move it upwards, as a continuation of the first half
else OUT_EXP.getValue((percentComplete * 2) - 1) / 2 + 0.5f
}
},
IN_CIRCULAR {
Expand Down Expand Up @@ -201,4 +204,4 @@ enum class Animations : AnimationStrategy {
return OUT_BOUNCE.getValue(percentComplete * 2 - 1) * 0.5f + 0.5f
}
}
}
}

0 comments on commit 3f43410

Please sign in to comment.