Created
April 11, 2020 18:30
-
-
Save alexcmgit/6e4c3f5a12d1622801059c1d400b6f00 to your computer and use it in GitHub Desktop.
To interpolate two interval's https://pt.wikipedia.org/wiki/Interpolação_linear
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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