Skip to content

Instantly share code, notes, and snippets.

@antimatter15
Created March 29, 2012 11:56
Show Gist options
  • Save antimatter15/2236530 to your computer and use it in GitHub Desktop.
Save antimatter15/2236530 to your computer and use it in GitHub Desktop.
Smoothing any amount of input arbitrarily
function Interpolator(degree){
var inter = [];
function interpolate(a,b) {
return a + (b-a) * 0.04;
}
var fn = {
set: function(n){
inter[0] = n;
},
diff: function(){
return Math.abs(inter[0] - inter[degree])
},
inter: function(){
return inter
},
reset: function(){
for(var i = 0; i < degree + 1; i++){
inter[i] = 0;
}
},
get: function(){
for(var i = 0; i < degree; i++){
inter[i+1] = interpolate(inter[i+1], inter[i])
}
return inter[degree]
}
};
fn.reset();
return fn;
}
var polar_p1 = new Interpolator(2);
var polar_p2 = new Interpolator(2)
function updatePolar(p1, p2){
if(polar_p1.diff() < 1e-5 && polar_p2.diff() < 1e-5){
requestAnimFrame(renderPolar);
}
polar_p1.set(p1);
polar_p2.set(p2);
}
function renderPolar(){
var p1 = polar_p1.get();
var p2 = polar_p2.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment