Created
May 18, 2015 08:00
-
-
Save marlun78/54bbf62e249f36c3aed1 to your computer and use it in GitHub Desktop.
Takes a screen dimension and return its ratio as width:height
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
/** | |
* toRatio.js | |
* Copyright (c) 2015 marlun78 | |
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83 | |
* | |
* Takes a screen dimension and return its ratio as width:height. | |
* | |
* @example | |
* toRatio(1280, 720); //=> 16:9 | |
* toRatio(800, 600); //=> 4:3 | |
* | |
* @param {number} width | |
* @param {number} height | |
* @returns {string} | |
*/ | |
function toRatio(width, height) { | |
if (width % 10 === 0 && height % 10 === 0) { | |
return toRatio(width / 10, height / 10); | |
} else if (width % 2 === 0 && height % 2 === 0) { | |
return toRatio(width / 2, height / 2); | |
} else { | |
return width + ':' + height; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment