Last active
December 16, 2015 19:19
-
-
Save quidmonkey/5484715 to your computer and use it in GitHub Desktop.
Fill Browser with a Small Canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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