Created
March 5, 2013 14:36
-
-
Save LukeChannings/5090708 to your computer and use it in GitHub Desktop.
computes the dimensions for an element to fully cover the viewport.
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
/** | |
* computes the dimensions for an element to fully cover the viewport. | |
* @param vw {Number} the width of the viewport | |
* @param vh {Number} the height of the viewort | |
* @param a {Number} the aspect ratio of the element being displayed. | |
*/ | |
function getScaledDimensions(vw, vh, a) { | |
// variables to contain the width and height results. | |
var w, h, ox, oy; | |
// compute two possible dimensions based on the viewport height and width. | |
var cw = vh * a, ch = vw * 1 / a; | |
// set the width and the height to whichever dimension encompasses the entire viewport. | |
if (cw<=vw) w = vw, h = ch, ox = 0, oy = (vh - ch) / 2; | |
if (ch<=vh) w = cw, h = vh, ox = (vw - cw) / 2, oy = 0; | |
// return the computed dimensions. | |
return {width: Math.round(w), height: Math.round(h), offsetX: ox, offsetY: oy}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment