Advertisement
skozombie

Automatically scale entire browser window

Sep 2nd, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Automatically scale the entire viewport with some jquery stuff
  2. //
  3. // Need something like this in your CSS:
  4. //
  5. //  html {
  6. //      height: 1080px;
  7. //      width: 1920px;
  8. //      transform-origin: top left;
  9. //  }
  10.  
  11. function debounce(func, wait, immediate) {
  12.     var timeout;
  13.     return function () {
  14.         var context = this, args = arguments;
  15.         var later = function () {
  16.             timeout = null;
  17.             if (!immediate) func.apply(context, args);
  18.         };
  19.         var callNow = immediate && !timeout;
  20.         clearTimeout(timeout);
  21.         timeout = setTimeout(later, wait);
  22.         if (callNow) func.apply(context, args);
  23.     };
  24. };
  25.  
  26. function set_size() {
  27.     var tw = 1920;
  28.     var th = 1080;
  29.     var w = $(window).width();
  30.     var h = $(window).height();
  31.  
  32.     var scale = 1;
  33.  
  34.     // width out by more than +/- 2%
  35.     if (Math.abs(w / tw - 1) > 0.02) {
  36.         scale = Math.max(w / tw, 0.25); // avoid going to crazy crash territory so clamp at 25% scaling
  37.     }
  38.     if (console) console.log("Auto sizing for resolution: " + w + "x" + h + ", scaling:" + scale);
  39.  
  40.     $('html').css('transform', 'scale(' + scale + ')');
  41.     $('html').css('-ms-transform', 'scale(' + scale + ')');
  42. }
  43.  
  44. $(function () {
  45.     var debouncedResize = debounce(set_size, 500);
  46.     $(window).resize(debouncedResize);
  47.     set_size();
  48. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement