intrinsic-scale
Replicate background-size: cover/contain
for canvas/CSS/Node/… on any type of media.
Many libraries that take care of replicating background-size
/object-fit
’s cover
/contain
have this functionality embedded. You can use this function anywhere, from the browser to calculate sizing on a canvas, to node when doing some image file resizing.
This module doesn't concern itself with getting the sizes or applying the values. You provide numbers, you get numbers.
Install
npm install intrinsic-scale
import resizeToFit from 'intrinsic-scale';
Usage
const source = {width: 50, y: 50};
const target = {width: 100, height: 200};
const { width, height, x, y } = resizeToFit('contain', source, target);
console.log(width, height, x, y);
const source = {width: 50, y: 50};
const target = {width: 100, height: 200};
const { width, height, x, y } = resizeToFit('cover', source, target);
console.log(width, height, x, y);
Dependencies
No dependencies. It works in the browser and in node.
Used on