function preserveAspect() { var scaled = $(window); scaled.css({ 'height': '100%', 'width': '100%'}); scaled.css("box-sizing", "border-box"); var gameRatio = 16/9; var maxWidth = 1920; var w = scaled.outerWidth(); var h = scaled.outerHeight(); console.log("window is " + w + "x" + h + ", gameRatio is " + gameRatio + ", window is " + w/h); if (w > gameRatio * h) { //console.log("window wide so maximize height"); newHeight = h; newWidth = gameRatio*h; } else { //console.log("window narrow so maximize width"); newWidth = w; newHeight = w/gameRatio; } if (newWidth > maxWidth) { //console.log("too wide so decrease width"); newWidth = maxWidth; newHeight = maxWidth / gameRatio; } console.log( "setting game size to " + newWidth + "x" + newHeight ); scaled.width(newWidth); scaled.height(newHeight); scaled.css({margin: 0}); //console.log($(gameContainer)); //console.log(document.getElementById("#canvas")); var thiscanvas = document.getElementById("#canvas"); thiscanvas.width = newWidth; thiscanvas.height = newHeight; $(gameContainer).width(newWidth); $(gameContainer).height(newHeight); $(loader).width(newWidth); $(loader).height(newHeight); window.scrollTo(0, 0); } $(document).ready(function() { preserveAspect(); $(window).resize(preserveAspect); $(window).on("orientationchange", preserveAspect); });