Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
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