Created
June 22, 2016 02:00
-
-
Save anonymous/19538b15fad80d49fe4419c69fd8f0db to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/cohomuyeji
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function getOutputResolution(max_width, max_height, videoInfo) { | |
var tmp_max_width = 0 | |
var tmp_max_height = 0 | |
// ShrinkToFit: don't scale up videos | |
max_width = (max_width > videoInfo.width) ? videoInfo.width : max_width | |
max_height = (max_height > videoInfo.height) ? videoInfo.height : max_height | |
if (videoInfo.width < videoInfo.height) { // for portrait | |
tmp_max_width = Math.min(max_width, max_height) // getting the smallest number for width | |
tmp_max_height = Math.max(max_width, max_height) // getting the biggest number for height | |
} else { // for landscape | |
tmp_max_width = Math.max(max_width, max_height) | |
tmp_max_height = Math.min(max_width, max_height) | |
} | |
var scalingFactor = Math.min( | |
tmp_max_width / videoInfo.width, | |
tmp_max_height / videoInfo.height | |
); | |
//console.log(scalingFactor, scalingFactor * videoInfo.width, scalingFactor * videoInfo.width) | |
// fix for odd pixel error | |
var result_width = 2 * Math.round(Math.floor(scalingFactor * videoInfo.width) / 2) | |
var result_height = 2 * Math.round(Math.floor(scalingFactor * videoInfo.height) / 2) | |
// calculated the desired width and height of the video | |
return ({width: result_width, height: result_height}) | |
} | |
var video = {width: 568, height: 320}; | |
console.log( getOutputResolution(376, 376, video) ); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function getOutputResolution(max_width, max_height, videoInfo) { | |
var tmp_max_width = 0 | |
var tmp_max_height = 0 | |
// ShrinkToFit: don't scale up videos | |
max_width = (max_width > videoInfo.width) ? videoInfo.width : max_width | |
max_height = (max_height > videoInfo.height) ? videoInfo.height : max_height | |
if (videoInfo.width < videoInfo.height) { // for portrait | |
tmp_max_width = Math.min(max_width, max_height) // getting the smallest number for width | |
tmp_max_height = Math.max(max_width, max_height) // getting the biggest number for height | |
} else { // for landscape | |
tmp_max_width = Math.max(max_width, max_height) | |
tmp_max_height = Math.min(max_width, max_height) | |
} | |
var scalingFactor = Math.min( | |
tmp_max_width / videoInfo.width, | |
tmp_max_height / videoInfo.height | |
); | |
//console.log(scalingFactor, scalingFactor * videoInfo.width, scalingFactor * videoInfo.width) | |
// fix for odd pixel error | |
var result_width = 2 * Math.round(Math.floor(scalingFactor * videoInfo.width) / 2) | |
var result_height = 2 * Math.round(Math.floor(scalingFactor * videoInfo.height) / 2) | |
// calculated the desired width and height of the video | |
return ({width: result_width, height: result_height}) | |
} | |
var video = {width: 568, height: 320}; | |
console.log( getOutputResolution(376, 376, video) ); | |
</script></body> | |
</html> |
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
function getOutputResolution(max_width, max_height, videoInfo) { | |
var tmp_max_width = 0 | |
var tmp_max_height = 0 | |
// ShrinkToFit: don't scale up videos | |
max_width = (max_width > videoInfo.width) ? videoInfo.width : max_width | |
max_height = (max_height > videoInfo.height) ? videoInfo.height : max_height | |
if (videoInfo.width < videoInfo.height) { // for portrait | |
tmp_max_width = Math.min(max_width, max_height) // getting the smallest number for width | |
tmp_max_height = Math.max(max_width, max_height) // getting the biggest number for height | |
} else { // for landscape | |
tmp_max_width = Math.max(max_width, max_height) | |
tmp_max_height = Math.min(max_width, max_height) | |
} | |
var scalingFactor = Math.min( | |
tmp_max_width / videoInfo.width, | |
tmp_max_height / videoInfo.height | |
); | |
//console.log(scalingFactor, scalingFactor * videoInfo.width, scalingFactor * videoInfo.width) | |
// fix for odd pixel error | |
var result_width = 2 * Math.round(Math.floor(scalingFactor * videoInfo.width) / 2) | |
var result_height = 2 * Math.round(Math.floor(scalingFactor * videoInfo.height) / 2) | |
// calculated the desired width and height of the video | |
return ({width: result_width, height: result_height}) | |
} | |
var video = {width: 568, height: 320}; | |
console.log( getOutputResolution(376, 376, video) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment