Thwack!! on a maximized 1024x768 browser window |
Since we're not completely concerned about the exact width or height, the first piece of information we need to set is the ratio of width to height. In Thwack!!, we have it set to 4/3 (the game area is 4 units wide to 3 units high). Once we have determined this, it's a matter of making adjustments whenever the document is resized or, in the case of mobile devices, the screen orientation is changed. We handle these events by setting:
window.addEventListener('resize', resizeGame, false); window.addEventListener('orientationchange', resizeGame, false);Now we create a "resizeGame" function to handle these two events. With Thwack!! it's a bit more complicated since we're using multiple canvases, but if the game is running on a single canvas, it might look something like this:
function resizeGame() { var gameBoard = document.getElementById('canvas'); var widthToHeight = 4 / 3; var newWidth = window.innerWidth; var newHeight = window.innerHeight; var newWidthToHeight = newWidth / newHeight; if (newWidthToHeight > widthToHeight) { // window width is too wide relative to desired game width gameBoard.style.height = newHeight + 'px'; newWidth = newHeight * widthToHeight; gameBoard.style.width = newWidth + 'px'; } else { // window height is too high relative to desired game height gameBoard.style.width = newWidth + 'px'; newHeight = newWidth / widthToHeight; gameBoard.style.height = newHeight + 'px'; } // center the canvas gameBoard.style.marginTop = (-newHeight / 2) + 'px'; gameBoard.style.marginLeft = (-newWidth / 2) + 'px'; };
Thwack!! on mobile Safari |
The last part re-centers our canvas in the middle of the screen. Many of the CSS properties we are concerned with are manipulated directly by the function above, but in order for those to work, we set up a few other CSS properties as follows:
#canvas { position: absolute; left: 50%; top: 50%; }This allows us to put the top left corner of the canvas in the center of the screen, and then our resizeGame() function gives the canvas a negative top and left margin half of the width and height of the game board so it is centered in the window.
Genius solution, I'll be sure to use it. In my game, the default is 854x480, but there's a fullscreen button. Right now it makes it fill the WHOLE thing but I'd love to have a way to preserve the aspect ratio, so thanks. :)
ReplyDeleteGlad it's useful! I posted a slightly more detailed version of this tutorial on html5rocks.com you may want to check out as well.
ReplyDeleteI saw and commented on that one too. :)
ReplyDeleteSo you did! Hmm, I need to pay more attention.
ReplyDelete