Skip to content

Instantly share code, notes, and snippets.

@mutoo
Created March 10, 2021 12:23
Show Gist options
  • Save mutoo/0c4c388749fedd22f16375c07f8cae40 to your computer and use it in GitHub Desktop.
Save mutoo/0c4c388749fedd22f16375c07f8cae40 to your computer and use it in GitHub Desktop.
{
const ratioConfig = (x, y) => ({
x,
y,
ratio: x / y
})
const commonRatio = [ratioConfig(1, 1), ratioConfig(4, 3), ratioConfig(16, 9), ratioConfig(16, 10)];
const getRatioConfig = (x, y) => {
const ratio = x / y;
const rank = [...commonRatio].map((r) => ({
...r,
delta: Math.abs(r.ratio - ratio),
})).sort((r1, r2) => r1.delta - r2.delta);
return ratioConfig(rank[0].x, rank[0].y);
}
const ret = getRatioConfig(982, 737);
console.log(ret.x, ret.y); // 4, 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment