Skip to content

Instantly share code, notes, and snippets.

@Aeon
Forked from istro/foobar
Last active December 11, 2015 22:08
Show Gist options
  • Save Aeon/4667014 to your computer and use it in GitHub Desktop.
Save Aeon/4667014 to your computer and use it in GitHub Desktop.
ads = [
{width: 1000, height:500},
{width: 200, height:200},
{width: 600, height:750},
{width: 600, height:600}
];
screen_ratio = window.document.width / window.document.height;
minimum_area = window.document.width * window.document.height * 0.8;
ads.sort(function(a, b) {
var a_ratio_to_screen = Math.round(Math.abs(a.width / a.height - screen_ratio) * 100)/100,
b_ratio_to_screen = Math.round(Math.abs(b.width / b.height - screen_ratio) * 100)/100,
a_area = a.width * a.height,
b_area = b.width * b.height;
// determine the best fit and size
// if they are both bigger than minimum area
if(a_area > minimum_area && b_area > minimum_area) {
// pick the one with best proportions
return a_ratio_to_screen < b_ratio_to_screen ? 1 : -1;
} else {
// pick the bigger ad first
return a_area < b_area ? 1 : -1;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment