Skip to content

Instantly share code, notes, and snippets.

@weiland
Created June 6, 2014 14:45
Show Gist options
  • Save weiland/b4015d7325b49e18726a to your computer and use it in GitHub Desktop.
Save weiland/b4015d7325b49e18726a to your computer and use it in GitHub Desktop.
Calculate the new size of an element w*h with a new width or height.
(function CalculateNewSizes(){
var width, height, nWidth, nHeight, tmp;
tmp = prompt('Current dimensions 123x1234: ').split('x');
width = tmp[0];
height = tmp[1];
tmp = prompt('Set a new value for width. (prefix h for new Height)').replace(/ /g, '');
if(tmp.indexOf('h') !== -1) {
nHeight = tmp.replace(/h/, '');
nWidth = parseInt(width*nHeight/height);
} else {
nWidth = tmp;
nHeight = parseInt(height*nWidth/width);
}
alert('new width: ' + nWidth + "\n" + 'new height: ' + nHeight);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment