support for screen autoresize
Implement graphics auto-resize in melonJS, and use the browser resize event, to create (full)screen size independent games.
This would be a great feature!
I'm working on this! :D
i'm thinking i could rewrite the me.video.init to have 2 more parameters, autoresize, and autozoom. So you face with four different scenarios:
- AutoResize: false, AutoZoom: false => What happens now in melonJS, you have always the same canvas size, even if you resize the window.
- AutoResize: true, AutoZoom: false => The size of the canvas always it the size of the window, but you don't do zoom, so you end up increasing or decreasing the amount of map that is shown on the screen (maybe useful in some RPGs?)
- AutoResize: false, AutoZoom: true => Does Zoom Keeping the Aspect Ratio of the game
- AutoResize: true, AutoZoom: true => Does Zoom on the game, but it allows some deformities of the aspect ratio. ?? it need more work...
just tell me what you think...
@andyveliz I set this ticket to milestone 0.9.5 as part of my wish list for the next version.
A few suggestions:
AutoZoom should also scale the pixel size by the same ratio that it fills the browser, of course, maintaining the same aspect ratio like you said.
AutoResize = true, AutoZoom = true: The canvas should first be AutoZoomed, like above, and then the canvas resized to fill any empty space in the browser.
Here are some images to help visualize. The white space in the browser is NOT part of the canvas. The black space in the browser IS part of the canvas (rendering the melonJS background color, if any).
AutoResize = false, AutoZoom = false:

AutoResize = true, AutoZoom = false:

AutoResize = false, AutoZoom = true:

AutoResize = true, AutoZoom = true:

Great... thanks for the images... that really clarified a lot...
i'll try to submit some code for you to see in the next few weeks... i hope i can help..
Parasyte's images look good and might negate the following suggestion...
It would be great to be able to specify which dimensions are resized.
-
- Scale to the view port size whilst retaining aspect ratio
-
- Resize height of canvas
-
- Resize width of canvas
Example: http://www.freeimagehosting.net/6a8aj
Does anyone have a fork with resizing implemented?
This feature should have a max-width option so that you can be sure the game is never resized larger than it was designed to be. Otherwise the graphics will start to look bad on larger screens when the resolution is greater than the graphics were made at.
For example, for my game, when it is viewed on a screen with a width larger than 1024px the game is going to be centered and have a box-shadow and there will be additional content on the page, e.g., about info, comments, footer, etc. Using media queries I will remove that extra content when the browser width is 1024px or below so that the game can truly expand to fit the screen when viewed on tablets and phones.
It would also be nice to have a feature to force the game to appear in either portrait or landscape mode when viewed on tablets or phones. One tower defense game I play, Tower Madness is always in portrait mode, and another one, Fieldrunners, is always in landscape mode. Designing a game to look right in both modes, including maps and menus, would be very difficult on smaller screens.
There are orientation media queries.
You can rotate content with CSS3.
Hey thanks for all the good ideas!
currently i'm working on this issue, i have just a few non-working test ... :( but i'll commit some code to my fork of melonjs as soon as i can... i'm only working on resizing on the fly the canvas, with the option to maintain the aspect ratio, basically the images that Jason share with us... i think that this could solve the "resizing when changing orientation" issue... i'll be looking to add the other great features you guys wrote about.
Hi guys, i've commited some code to my fork, in the canvas-resize branch, it's a hack making the autoresize and autozoom functions work, check them out... any feedback is appreciated, I hope this can be useful to the main repository.
https://github.com/andyveliz/melonJS
@greghouston A max[Height|Width] feature is fine. But there are also some other implications to consider when zooming a canvas to larger or smaller than it was designed for.
When zooming the canvas smaller than its native size, you actually _lose quality_ just as much as when the canvas is enlarged. The quality loss is not as obvious when the image is scaled with interpolation, but it comes at a performance cost. In this case, it is more effective to use two (or more) copies of every image in the game, and choose the closest fit when displaying. This is a technique used in 3D rendering for many years, called MipMapping; when a texture is scaled toward 0.5 times its original size, a smaller version of the texture will be used, increasing performance and visual quality.
Likewise on the other end of the spectrum, when enlarging the canvas, you may not want the image scaled with interpolation enabled, to give the game a distinct "pixelated" look. This can be done with CSS using the image-rendering property on the canvas. That affects the entire canvas, but if you also want it to affect sprites that are scaled or rotated within the canvas, you also need to use context.mozImageSmoothingEnabled = false and context.webkitImageSmoothingEnabled = false when drawing the sprites.
So maybe that's a third boolean to pass, whether you want image smoothing enabled (default) or disabled?
@andyveliz I looked through the commit in your canvas-resize branch. The most important thing it has to offer is the math, which I haven't studied yet. It looks pretty good at first glance. And some of the new methods are also very helpful. The one piece of criticism I have is how your hack reuses variables, and also uses the me.sys namespace unnecessarily.
One idea I want to bring up is the possibility of allowing the canvas to properly reflow with other elements in the HTML document, instead of forcing it to the size of the browser viewport. Imagine the body margins have not been set to 0 (the default is always something larger than 0) then you will stretch the browser viewport by making the canvas the same width/height without taking into account the body margin. Imagine if the canvas also had its own margin... Or if the canvas is inside a div element that sits next to a side-navigation menu. Now we hit all kind of complex situations where the canvas cannot be made the same size as the browser viewport.
The ideal solution, I think, is calculating the size of the canvas's parent element content box, and performing one of three operations when autosize OR autozoom are set, where p is the parent element's content box:
- If autosize is true:
canvas.width = p.widthandcanvas.height = p.height - Else if autozoom is true AND p.width >= p.height:
canvas.height = p.height - Else if autozoom is true AND p.height > p.width:
canvas.width = p.width
Some special care must be taken when p == document.body, because the body may have a height that is smaller than the browser viewport height. Easy thing to do for solving that may be unconditionally setting document.body.style.minHeight = window.innerHeight or similar at the top of the window resize event handler.
I know I haven't covered every edge case, but just adding some more details to take into consideration.
@parasyte
You're right!
I haven't tough of that... i'll re write the code to take in cosideration the wrapper variable. The resize or zoom should be contained within the object you choose in your html, not always using the full screen.
I took the math from CommandoJS, I think it's allright.
I was thinking about the "max-width" option that @greghouston suggested, it could be a problem if you have a big widescreen, in some games you don't want the player to have the advantage of seeing more map. How do you guys feel about a max-aspect-ratio variable. You can have both, autozoom and autoresize, as long as you don't go too far in your aspect ratio.
I think in the case you don't want to show too much of the map, you just want AutoResize = false?
I think it may be better to consider this as an exception, so normal players can have a better gaming experience in all the different screen sizes, but at the same time being prepared for bigger screens and more clever players.
I'll write some code, and try to keep it simple.
If both AutoResize and AutoZoom are true the game is going to appear very similar from one screen to another varying only as much as the aspect ratio does, e.g., aspect ratio on iPhone is 1.5:1, the iPad is 1.333:1. My monitor is 1.6:1.
What would be nice though is a maintain aspect ratio option so that it acts like embedded flash does. If you set the width of a flash object to 100%, it will scale to fit the width of the parent container and the height of the flash object will have the correct aspect ratio. This is really important in responsive web designs.
To mimic Flash: AutoResize: true, AutoZoom: true, MaintainAspectRatio: true
Please provide some images that show how a game will render with this third boolean. You'll now have 2**3 = 8 different modes of resize, instead of just four.
Keep in mind that AutoResize = true will always change the aspect ratio.
Here is a perfect example. Resize the result container.
http://jsfiddle.net/greghouston/zS7Bu/
I just see a broken image.
Failed to load resource: the server responded with a status of 403 (Forbidden) http://www.gamesuncovered.com/uploads/2009/01/10/fieldrunners1-122933.png
But regardless, it looks like what you want is AutoResize = false, AutoZoom = true
Find any image on the web and put its URL in there.
MaintainAspectRatio would make it so that AutoResize resized to full width and then the height was generated via the original aspect ratio ... just like when an image in HTML is set to width: 100%, height: auto. In the case of images the aspect ratio is built in. In our case the aspect ratio is defined by the original viewport size.
I guess you could make MaintainAspectRatio always fit in the screen in which case sometimes it would use 100% height and sometimes 100% width or that could be an option.
Quote: // But regardless, it looks like what you want is AutoResize = false, AutoZoom = true
Not exactly. An image always resizes to full width. In the AutoResize = false, AutoZoom = true example above it has resized to full height.
Actually, in the use case I am thinking, AutoResize = false, AutoZoom = true: may actually work so you may be right. I was thinking of how it would display in a column on a webpage. Since those columns don't have heights defined it would have to use the width.