UNPKG

apeman-react-image

Version:
40 lines (32 loc) 776 B
/** * @function _scaledSize */ 'use strict' const numcal = require('numcal') function scaledSize (contentSize, frameSize, policy) { let cw = contentSize.width let ch = contentSize.height let fw = frameSize.width let fh = frameSize.height let wRate = numcal.min(1, fw / cw) let hRate = numcal.min(1, fh / ch) let sizeWithRate = (rate) => ({ width: contentSize.width * rate, height: contentSize.height * rate }) switch (policy) { case 'none': return sizeWithRate(1) case 'fit': return sizeWithRate( numcal.min(wRate, hRate) ) case 'fill': return sizeWithRate( numcal.max(wRate, hRate) ) default: throw new Error(`Unknown policy: ${policy}`) } } module.exports = scaledSize