I change the text color with requestAnimationFrame(animate);
function:
requestAnimationFrame(animate);
function animate(time){
... // change text color here
if (offset_s < offset_e) {requestAnimationFrame(animate);}
}
offset_s
and offset_s
indicates start and end positions of the text for color change. In some cases the animation should last for 2 seconds, but in order cases - for 5 seconds, but offset_e - offset_s
could be the same in these two cases. What can I do to control the speed of animation based on given time in seconds/milliseconds?
I suggest to use setInterval function in JavaScript.
requestAnimationFrame
really needs some 'ugly' calculations. Don't believe me? Scroll up, you will see...So, to make
setInterval
function as handy as rAF(requestAnimationFrame) store the function inside of variable. Here is an example:given way, you can control your FPS and pick correct velocity for your objects.
From the tags of the question i can only see that you animate something drawn on canvas and thats why u cannot use css-animation or jquery-animation.
You have to control the length of the animation by calculating the time difference.
u can do it similar to this example
trigger your animation and call start_animate(2000) //duration in millisecond 1000=1 sec
You should separate concerns clearly.
Have a single requestAnimationFrame running, which computes the current animation time and calls every update and draw related functions.
Then your animations would be handled by a function (or class instance if you go OOP) that deals with the current animation time.
Just some direction for the code :
To start your 'engine', just call
launchAnimation
then you'll have a validanimationTime
anddt
to deal with.I'd make
animationHandler
an instance of anAnimationHandler
class, that allows to add/remove/update/draw animations.