Skip to content

Instantly share code, notes, and snippets.

@think2011
Last active November 2, 2016 12:06
Show Gist options
  • Save think2011/5ed955e2b7ec88d52f36eb9b7adf17c1 to your computer and use it in GitHub Desktop.
Save think2011/5ed955e2b7ec88d52f36eb9b7adf17c1 to your computer and use it in GitHub Desktop.
比例尺寸计算
/**
* 比例尺寸计算
*
* @param options
* @param options.w
* @param options.h
* @param [options.newW]
* @param [options.newH]
* @param [options.max]
* @returns {object}
*/
function zoomSize(options) {
var w = options.w
var h = options.h
var newW = options.newW
var newH = options.newH
var max = options.max
var result = {w:w,h:h}
if(newW && newH) {
return result
}
else if (max) {
return w > h
? zoomSize({w:w, h:h, newW: max})
: zoomSize({w:w, h:h, newH: max})
}
else if(newW) {
result.w =newW
result.h = h * (newW / w)
}
else if (newH) {
result.w = w * (newH / h)
result.h = newH
}
return {
w: Math.ceil(result.w),
h: Math.ceil(result.h)
}
}
console.log(zoomSize({
w:180,
h:400,
newW:660
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment