Last active
November 2, 2016 12:06
-
-
Save think2011/5ed955e2b7ec88d52f36eb9b7adf17c1 to your computer and use it in GitHub Desktop.
比例尺寸计算
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 比例尺寸计算 | |
* | |
* @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