57 lines
1.3 KiB
JavaScript
57 lines
1.3 KiB
JavaScript
function finalWidthHeight(
|
|
selectionWidth,
|
|
selectionHeight,
|
|
minWidth,
|
|
minHeight
|
|
) {
|
|
// const minWidth = 512
|
|
// const minHeight = 512
|
|
|
|
// const selectionWidth = 256
|
|
// const selectionHeight = 1000
|
|
|
|
let finalWidth = 0
|
|
let finalHeight = 0
|
|
|
|
if (selectionWidth <= selectionHeight) {
|
|
//do operation on the smaller dimension
|
|
const scaleRatio = selectionWidth / minWidth
|
|
|
|
finalWidth = minWidth
|
|
finalHeight = selectionHeight / scaleRatio
|
|
} else {
|
|
const scaleRatio = selectionHeight / minHeight
|
|
|
|
finalHeight = minHeight
|
|
finalWidth = selectionWidth / scaleRatio
|
|
}
|
|
return [finalWidth, finalHeight]
|
|
}
|
|
|
|
async function selectionToFinalWidthHeight() {
|
|
const { getSelectionInfoExe } = require('./psapi')
|
|
try {
|
|
const selectionInfo = await getSelectionInfoExe()
|
|
const [finalWidth, finalHeight] = finalWidthHeight(
|
|
selectionInfo.width,
|
|
selectionInfo.height,
|
|
512,
|
|
512
|
|
)
|
|
|
|
return [
|
|
parseInt(finalWidth),
|
|
parseInt(finalHeight),
|
|
selectionInfo.width,
|
|
selectionInfo.height,
|
|
]
|
|
} catch (e) {
|
|
console.warn('you need a rectangular selection', e)
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
finalWidthHeight,
|
|
selectionToFinalWidthHeight,
|
|
}
|