Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Color Animation

I want to animate (transition) from 1 color to another in raw javascript.

I dont want to use any framework (jquery, mootools) or css3. plain raw javascript.

I have been really having trouble to do this, can someone help me out ? :)

like image 272
user1437328 Avatar asked Jul 02 '12 11:07

user1437328


People also ask

What is a JavaScript animation?

The Animation Code JavaScript animations are done by programming gradual changes in an element's style. The changes are called by a timer. When the timer interval is small, the animation looks continuous.

Is it possible to animate colors in CSS3?

You can also animate colors using CSS3 transitions but it's only supported by modern browsers.

How long does it take to animate a picture in JavaScript?

But in the next chapter we’ll do some JavaScript animations to cover more complex cases. The picture grows on click from 40x24px to 400x240px (10 times larger). The animation takes 3 seconds. At the end output: “Done!”. During the animation process, there may be more clicks on the plane.

What is the Best jQuery plugin for color animation?

Bitstorm has the best jquery color animation plugin I've seen. It's an improvement to the jquery color project. It also supports rgba. Show activity on this post. You can use jQuery UI to add this functionality. You can grab just what you need, so if you want to animate color, all you have to include is the following code.


1 Answers

maybe something like this:

lerp = function(a, b, u) {
    return (1 - u) * a + u * b;
};

fade = function(element, property, start, end, duration) {
    var interval = 10;
    var steps = duration / interval;
    var step_u = 1.0 / steps;
    var u = 0.0;
    var theInterval = setInterval(function() {
        if (u >= 1.0) {
            clearInterval(theInterval);
        }
        var r = Math.round(lerp(start.r, end.r, u));
        var g = Math.round(lerp(start.g, end.g, u));
        var b = Math.round(lerp(start.b, end.b, u));
        var colorname = 'rgb(' + r + ',' + g + ',' + b + ')';
        el.style.setProperty(property, colorname);
        u += step_u;
    }, interval);
};

You can play around an try it out as a jsfiddle or check out the full working example below. You might want to improve this by using HSL/HSV colors, which gives you a prettier transition, but i'll leave that up to you.

<html>
<head>
  <title>Fade</title>
  <style type="text/css">
    #box {
      width: 100px;
      height: 100px;
      background-color: rgb(255,0,0);
    }
  </style>
</head>
<body>
  <div id="box"></div>

  <script type="text/javascript" charset="utf-8">
    // linear interpolation between two values a and b
    // u controls amount of a/b and is in range [0.0,1.0]
    lerp = function(a,b,u) {
        return (1-u) * a + u * b;
    };

    fade = function(element, property, start, end, duration) {
      var interval = 10;
      var steps = duration/interval;
      var step_u = 1.0/steps;
      var u = 0.0;
      var theInterval = setInterval(function(){
        if (u >= 1.0){ clearInterval(theInterval) }
        var r = parseInt(lerp(start.r, end.r, u));
        var g = parseInt(lerp(start.g, end.g, u));
        var b = parseInt(lerp(start.b, end.b, u));
        var colorname = 'rgb('+r+','+g+','+b+')';
        el.style.setProperty(property, colorname);
        u += step_u;
      }, interval);
    };

    // in action
    el = document.getElementById('box'); // your element
    property = 'background-color';       // fading property
    startColor = {r:255, g:  0, b:  0};  // red
    endColor   = {r:  0, g:128, b:128};  // dark turquoise
    fade(el,'background-color',startColor,endColor,1000);

    // fade back after 2 secs
    setTimeout(function(){
      fade(el,'background-color',endColor,startColor,1000);
    },2000);
  </script>
</body>
</html>
like image 182
Patrick Oscity Avatar answered Sep 30 '22 22:09

Patrick Oscity