Maximizing the Game
Screen real estate on mobile devices is valuable especially for mobile games. The last thing you want to do is waste some of that real estate by not having maximized the game.
Setting the Viewport
The first step is to tell the browser that you don’t want to let users zoom in and out of the page. This is done by setting a viewport <meta> tag in the HTML. The viewport tag began its life as an iOS-only feature but has since spread to Android as well. Add the following to the <head> of your HTML document.
<meta name="viewport" content="width=device-width, user-scalable=0,
minimum-scale=1.0, maximum-scale=1.0"/>
This tag tells the browser to set the width of the page to the actual device’s pixel width and not to let the user zoom in and out. Chapter 6, “Being a Good Mobile Citizen,” discusses this tag in depth.
If you reload the page, you notice the game is zoomed in a bit but still doesn’t correctly fit on the page.
Resizing the Canvas
To fix the size issue and set the game up on the page so it fits as well as possible, there are a few extra steps that need to be taken. This is more difficult than it may seem due to various mobile peculiarities. Chapter 6 covers this topic in depth, but following is the basic pseudo-code:
Check if browser has support for touch events Exit early if screen is larger than a max size or no touch support Check if the user is in landscape mode, if so, ask them to rotate the browser Resize container to be larger than the page to allow ...
Get Professional HTML5 Mobile Game Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.