vasilakisfil
vasilakisfil

Reputation: 2329

Change background color over time with Javascript

How can I emulate, for instance, the color of a police car's beacon (lightbar) that goes, over time, from blue to white and again to blue.

I suppose i could generate somehow too many setTimeout functions that change the color but I would like to know if there is a more elegant way (or ideally a library that does that).

Thanks!

Upvotes: 0

Views: 429

Answers (1)

Bergi
Bergi

Reputation: 664538

i could generate somehow too many setTimeout functions that change the color but I would like to know if there is a more elegant way

Yes, there is. A function that computes the color for the next step depending on the current time, paints it, and schedules itself with a timeout again.

or ideally a library that does that

Well, basically every DOM/animation library does that.

Upvotes: 2

Related Questions