calculates widths/heights according to common ratios.
what's an aspect ratio *really*?
well, i'm actually not quite sure.
…
But fitting things using math,
just seems to be nice
and nobody knows why.
🌴
npm i fit-aspect-ratio
const aspect = //calculate a missing width/height// {width:300, height:480} // (2.35:1)// {width:940, height:400} //fit to the closest aspect ratiolet res = // { closest:// { names: [ '16:9', ... ],// description: 'HD video',// decimal: 1.77777 },// width: 1280,// height: 720// } // force a specific aspect ratio (assumes landscape) //essentially '4:3'// {width:480, height:640}
you can see the list of aspect-ratios we use here
when fitting to an aspect ratio, height
is adjusted, not width. (..scrolling down is easier?) This can be configured in future versions.
Demo
ok, ELI5 aspect-ratio
ratios are (typically) given as width:height
suppose our computer screen is 4:3
, our resolution could be 640x480
.
To calculate:
// get a decimal representationlet decimal= 4/3 // 1.3333333 // this can get us our width..width = 480 * decimal// 640 // to get height, flip numerator/denominatorlet inverse = 1/decimal // or '3/4'height = 640 * inverse// 480
this took me a good while...
See also:
- aspect-crop - cropping images based on this library
MIT