Skip to content

Instantly share code, notes, and snippets.

@th3d0g
Created May 21, 2014 21:07
Show Gist options
  • Save th3d0g/fe409ee3791493eb581e to your computer and use it in GitHub Desktop.
Save th3d0g/fe409ee3791493eb581e to your computer and use it in GitHub Desktop.
Canvas fill browser window by setting viewport scale.
var screenScale = 1;
window.devicePixelRatio = window.devicePixelRatio || Math.round(screen.deviceXDPI*10 / screen.logicalXDPI)/10;
// Calc safe zone for pixels.
if( window.devicePixelRatio > 1 ) {
var lScreenWidth = Math.max( screen.width, screen.height ) * window.devicePixelRatio;
var lScreenHeight = Math.min( screen.width, screen.height ) * window.devicePixelRatio;
var SAFE_ZONE_WIDTH = 2048;
var SAFE_ZONE_HEIGHT = 1365;
if (!isWebGLSupported()) {
SAFE_ZONE_WIDTH/=2;
SAFE_ZONE_HEIGHT/=2;
}
var lRatio = Math.max(1,Math.ceil(Math.min(lScreenWidth / SAFE_ZONE_WIDTH, lScreenHeight / SAFE_ZONE_HEIGHT)));
screenScale = Math.round(100*lRatio / window.devicePixelRatio) / 100;
}
var head = document.getElementsByTagName('head')[0];
var meta = document.createElement('meta');
meta.setAttribute('name', 'viewport');
meta.setAttribute('content', 'initial-scale=' + scale + ', user-scalable=no, minimal-ui');
head.appendChild( meta );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment