Skip to content

Instantly share code, notes, and snippets.

@quidmonkey
Last active December 16, 2015 19:19
Show Gist options
  • Save quidmonkey/5484715 to your computer and use it in GitHub Desktop.
Save quidmonkey/5484715 to your computer and use it in GitHub Desktop.
Fill Browser with a Small Canvas
var preserveAspectRatio = true;
function fillBrowser (canvas) {
if (preserveAspectRatio) {
var dWidth = window.innerWidth / canvas.width;
var dHeight = window.innerHeight / canvas.height;
var newWidth;
var newHeight;
if (dWidth < dHeight) {
newWidth = dWidth * canvas.width;
newHeight = dWidth * canvas.height;
} else {
newWidth = dHeight * canvas.width;
newHeight = dHeight * canvas.height;
}
canvas.style.width = ~~newWidth + 'px';
canvas.style.height = ~~newHeight + 'px';
} else {
canvas.style.width = window.innerWidth + 'px';
canvas.style.height = window.innerHeight + 'px';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment