Skip to content

Instantly share code, notes, and snippets.

@alexcmgit
Created April 11, 2020 18:30
Show Gist options
  • Save alexcmgit/6e4c3f5a12d1622801059c1d400b6f00 to your computer and use it in GitHub Desktop.
Save alexcmgit/6e4c3f5a12d1622801059c1d400b6f00 to your computer and use it in GitHub Desktop.
function interpolate(xInterval, yInterval) {
let [x0, x1] = xInterval;
let [y0, y1] = yInterval;
return function (xA) {
if (xA > x1) xA = x1;
else if (xA < x0) xA = x0;
const yA = y0 + (y1 - y0) * ((xA - x0) / (x1 - x0));
return yA;
};
}
// ======
// USAGE
// ======
// Demo usage
const calc = interpolate([0, 5], [500, 1000]);
console.log(calc(2.5)); // Will show 750
// Apply in a Drawer Navigator
const touchXToTranslateX = interpolate([0, window.innerWidth], [0, 100]);
onTouchEvent(e => {
drawer.style.transform = `translateX(${touchXToTranslateX(e.x)}%)`;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment