Home > Enterprise >  How can I solve for current opacity in this animation?
How can I solve for current opacity in this animation?

Time:09-17

I'm trying to do a simple fade in/out animation in Lua.

I feel like these variables should be enough to solve for the alpha/opacity I want to set the box at every frame, but I'm having a lot of trouble with the fade out, since alpha = targetAlpha * animationPos always returns 0 while multiplying by the target alpha of 0.

All of these variables are decimal values between 0-1, representing alpha or %time completed.

  1. targetAlpha - The alpha value at the end of animation.
  2. initialAlpha - The alpha the box started at when the animation initialized.
  3. animationPos - The current position (%time completed) of the animation
  4. currentAlpha - Current alpha of the box.

Maybe I'm just super fried today, but I've been trying what feels like a billion combinations of these vars to find the equation that works, and to no luck.

Any help is appreciated!

CodePudding user response:

What you want is a linear interpolation, which takes two values a and b, and an interpolation value f between 0 and 1.

function lerp(a, b, f)
    return a * (1 - f)   b * f
end

And now you can just interpolate between initial and target alpha using your current animation progress:

alpha = lerp(initialAlpha, targetAlpha, animationPos)
  • Related