Interpolate object to i-th step [0...n] between o1,o2 for every property in o1 - Node.js Object

Node.js examples for Object:Property

Description

Interpolate object to i-th step [0...n] between o1,o2 for every property in o1

Demo Code


function tweenStep(o1,o2,i,n) {
  var ret = {};//w w  w .jav a2 s. co m
  for (name in o1) {
    if ( !o1.hasOwnProperty( name ) ) continue;
    var v1 = o1[ name ], v2 = o2[ name ];
    
    if ( typeof v2 == "undefined" ) v2 = v1;
    var f = i / n;
    
    //  all numbers are simple linear interpolation from o1...o2
    var val = v1 + f*( v2 - v1 );
    if ( v1 == v2 )
      val = v1;
      
    // for colors interpolate RGB channels separately
    if ( name.match( /color/i ) ) {
      val = map_rgb_color( v1, v2, i, n );
    }
    
    //  constrain z-index to integer values
    if ( name == "zIndex" )
      val = Math.floor( val );
      
    ret[ name ] = val;
   }
  return ret;
}
//  parse # rr gg bb
function rgb(s) {
//alert("rgb " + s);
  var sharp = s.substr(0,1);
  var r = s.substr(1,2);
  var g = s.substr(3,2);
  var b = s.substr(5,2);
  var ret = [ parseInt(r,16), parseInt(g,16), parseInt(b,16) ];
  //alert( s + "->"+ ret[0] + ret[1] + ret[2] );
  return ret;
}

Related Tutorials