-
-
Save fhefh2015/b485cfc06011951cbfd44026d1c0e0f4 to your computer and use it in GitHub Desktop.
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
{ | |
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