intrinsic-scale
Version:
Replicate background-size: cover/contain (scale+crop) for canvas/CSS/Node/… on any type of media.
24 lines (20 loc) • 568 B
JavaScript
export default function resizeToFit(mode, source, target) {
if (mode !== 'cover' && mode !== 'contain') {
throw new TypeError('Invalid mode');
}
const sourceRatio = source.width / source.height;
const targetRatio = target.width / target.height;
let {width} = target;
let {height} = target;
if (mode === 'contain' ? (sourceRatio > targetRatio) : (sourceRatio < targetRatio)) {
height = width / sourceRatio;
} else {
width = height * sourceRatio;
}
return {
width,
height,
x: (target.width - width) / 2,
y: (target.height - height) / 2,
};
}