Skip to content

Instantly share code, notes, and snippets.

@edwardhotchkiss
Created October 26, 2012 19:02
Show Gist options
  • Save edwardhotchkiss/3960746 to your computer and use it in GitHub Desktop.
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.
/**
* @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;
};
@jericopulvera
Copy link

width is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment