I am trying to animate some text on the screen in two ways:
- Move its Y position from 1 to -1 [already finished]
- Shift its RGB color value from white(255,255,255) to green(149,184,6) [need help with]
I am using the Linear Interpolation function from npm lerp to calculate a value between each RGB color channel.
const r = Math.floor(lerp(255, 149, alpha));
const g = Math.floor(lerp(255, 184, alpha));
const b = Math.floor(lerp(255, 6, alpha));
The lerp function alpha
parameter requires a percentage decimal value from 0 to 1, but I would like to use the texts position from -1 to 1 as the dynamic alpha value while the texts position changes.
To visualize this animation, please watch this video
Using react-three-fiber, I am changing the text's color value using this line:
ref.current.color = `rgb(${r}, ${g}, ${b})`;
I need help writing a function that takes in an input value between 1 and -1, and then converts it to an output value between 0 and 1 for the alpha parameter in the lerp function.
function translate(positionY){
//translates
}
const positionY = -0.5;
console.log(translate(positionY)) //.75
This function will be used to get the alpha for the lerp function:
const r = Math.floor(lerp(255, 149, translate(positionY)));
const g = Math.floor(lerp(255, 184, translate(positionY)));
const b = Math.floor(lerp(255, 6, translate(positionY)));