Created
October 26, 2012 19:02
-
-
Save edwardhotchkiss/3960746 to your computer and use it in GitHub Desktop.
Calculate new Width/Height of a Video or Image based on current width and height, then add padding to update to desired aspect ratio returning a new aspect ratio (say 16:9 or 4:3 etc) set of dimensions.
This file contains 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
/** | |
* @method calculatePaddedNewAspectRatio | |
* @param {Number} rW New Aspect Ratio Width | |
* @param {Number} rH New Aspect Ratio Height | |
* @param {Number} cW Current Width | |
* @param {Number} cH Current Height | |
* @return {Object} Aspect ratio correct .width and .height | |
**/ | |
function calculatePaddedNewAspectRatio(rW, rH, cW, cH) { | |
// aspect adjust WxH | |
var w2 = Math.round((cH / 9) * 16); | |
var h2 = Math.round((cW / 16) * 9); | |
// new width and height, which to pad? | |
var newWidth = (w2 < width) ? width : w2; | |
var newHeight = (h2 < height) ? height : h2; | |
// updated with and height, with correct aspect ratio w/ padding as needed. | |
var dimensions = { | |
width: newWidth, | |
height: newHeight | |
}; | |
return dimensions; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
width is not defined