melonJS icon indicating copy to clipboard operation
melonJS copied to clipboard

support for screen autoresize

Open ghost opened this issue 15 years ago • 54 comments

Implement graphics auto-resize in melonJS, and use the browser resize event, to create (full)screen size independent games.

ghost avatar Aug 27 '11 11:08 ghost

http://jsfiddle.net/zS7Bu/1/

This is how AutoResize = false, AutoZoom = true is supposed to work. Notice the image is _always_ the correct aspect ratio, especially when the height of the container would cause the bottom of the image to get chopped off. That "chopping off" means the aspect ratio is changing. And that is not what you want.

parasyte avatar Sep 24 '12 08:09 parasyte

as I was also highthing it here : https://groups.google.com/forum/?fromgroups=#!topic/melonjs/DNI1BWr0m7M

me.sys.scale will need to be modified, to “become” a vector containing the x and y scale ratio, that can be used later to correctly compute mouse coordinates. As from my understanding, currently a use case with AutoResize = true, AutoZoom = true; (does not maintain aspect ratio) won’t work with mouse event (coordinates will be wrong)

ghost avatar Sep 24 '12 09:09 ghost

I agree 100%. Scaling without maintaining aspect ratio will really do some strange things. The backbuffer (when double buffering is enabled) will also need some special handling for this case.

parasyte avatar Sep 24 '12 09:09 parasyte

If I may a couple of other remarks :

  • Instead of adding a resizeViewport function to me.game, I would rather suggest to add a resize function to the viewport object. (note also that if you resize the viewport, you probably also need to update the deadzone values)
  • I’m personally not a big fan of the two additional parameters added to the init function. What about the possibility to specify “auto” for the existing scale parameter, to at least get rid of the autoresize one ? then I have no idea so far concerning the second one :)
  • Why adding a new updateCanvasSize function instead of updating the existing updateDisplaySize one (I’m not sure someone even used it) ? these two function are somehow redundant, with the previous probably even broken

@andyveliz, by the way, that's a great work you did here, thank you for working on this :)

ghost avatar Sep 24 '12 09:09 ghost

Great ideas! well... the autoresize was indeed tricky, but the mouse coordinates are working as far as I tested, correctly.

I think the auto parameter in scale it's a lot easier, I think I use the resizeViewport function in me.game, because the drawManager parameter it's not public, and i needed to update the fullscreen_rect variable. Any ideas about this?

About the updateCanvasSize I created it because it was just a hack, I was a little afraid of breaking some other functionality, but if I have your blessing to mess with it, it will be a lot more elegant.

@parasyte The backbuffer was really tricky, but with my hack it works :D

andyveliz avatar Sep 24 '12 12:09 andyveliz

@andyveliz Hi ! So i started integrating your code to progress on this topic, and while starting to make some modification, I had a question.

I can see that you actually resize the viewport, I guess that you made this for a good reason, but I don't understand why ? :)

The feature here being basically to scale (maintaining or nor aspect ratio) automatically the display to fit with the browser window size, why do we need this? In my point of view, the offscreen canvas and the corresponding viewport should never changed, as only the displayed canvas is scaled/resized.

thanks for the feedback !

ghost avatar Oct 11 '12 10:10 ghost

@parasyte @andyveliz

At this stage (integration + clean-up), I think we need some beta tester here, any volunteer ? :):):)

ghost avatar Oct 12 '12 12:10 ghost

I was looking at graphs for work and ran into this, another example of zooming with the mousewheel and dragging the viewport with mousedown. http://sigmajs.org/examples/gexf_example.html

Same functionality in an HTML5 game: https://chrome.google.com/webstore/detail/fieldrunners/lkpikhjbfbffdblahfidklcohlaeabak?utm_source=chrome-ntp-icon

I just want to make sure this sort of functionality will be possible after autoZoom and autoResize.

Another feature that I mentioned in #113 that would be greatly appreciated is a method to assign map layers and objects to at least two different buffers, and to be able to zoom them independently before they are outputted on top of each other on the canvas. For example, I would assign the map layers, object entities, sprites, and animation sheets to the back buffer and gui and hud objects to the front buffer. Zooming with the mousewheel would only effect the back buffer.

An example of this sort of functionality can be seen here: https://chrome.google.com/webstore/detail/fieldrunners/lkpikhjbfbffdblahfidklcohlaeabak?utm_source=chrome-ntp-icon

greghouston avatar Oct 15 '12 20:10 greghouston

@parasyte @greghouston @andyveliz

Hi Guys, I would need some advice here before going further, as at this point we have basic automatic scaling stuff working .

What we have here so far :

  • the me.video.init takes the following parameter function(wrapperid, game_width, game_height, doublebuffering, scale, autoresize)
  • double-buffering is now enabled by default if canvas resizing/scaling is requested
  • if scale is equal to 'auto', the canvas will be scaled based on the canvas parent container width and height
  • if autoresize is equal to false (or not present) the canvas will be scaled maintaining the aspect ratio, else it takes the parent container width & height.
  • viewport is kept unchanged for now (to be re-added later)

So a few questions :

  • I would suggest to rename here the autoresize parameter to maintainAspectRatio, as it makes more sense this way, what do you think ?
  • how should we manage viewport resizing ? the engine is also publishing a message on the me.event.WINDOW_ONRESIZE channel, maybe there a cleaner way to implement it through this, instead of adding again a new parameter to the me.video.init function ?

ghost avatar Oct 16 '12 14:10 ghost

Renaming autoresize to maintainAspectRatio makes sense to me.

I wonder if there are enough parameters now that it would make sense to make it something like:

function(wrapperid, game_width, game_height, { doublebuffering: true, scale: true, maintainAspectRatio: true }

Just a thought.

I don't know if it is already built-in or not, but a nice feature would be adding event listeners for window.resize and orientation change so that the game could resize without requiring a refresh of the page. This could even be yet another option.

This game has great incremental resizing: http://browserquest.mozilla.org/

If you resize the browser small enough it will request, "Please rotate your device to landscape mode". Since our resize is based on the wrapper element, this would be easy to implement with media queries if melonjs resized when there was a window.resize or orientation change.

I sort of think it might be helpful to have a second scale function called zoom. Zoom would not change the canvas size, but either the entire canvas scale or the scale of a buffer. This way if the player is zoomed into the game at 1.5 scale, and then changes orientation on their iPad, the zoom is applied after the canvas scale. So the canvas might have a scale of 2 and zoom be at 1.5.

me.sys.scale me.sys.zoom

This way also a zoom of 1.5 would have the same effect at all scales. A zoom of 1 being the default. In my game for instance, the mousewheel will effect zoom with a range clamp of 1 to 1.5. Zooming should not effect the canvas size. It almost worked with the now deprecated me.video.scale, but was rather buggy.

greghouston avatar Oct 17 '12 02:10 greghouston

You might also consider adding me.sys.orientation that would either return a value of portrait or landscape.

greghouston avatar Oct 17 '12 03:10 greghouston

Hi @greghouston

thank you for the feedback,

Concerning notification, the engine publish a message (using minPubSub) on a specific channel, see here : https://github.com/obiot/melonJS/blob/ticket-4/src/video/video.js#L225

but yes, another one for orientation change, and even fullscreen, would be good too (I might think about a generic channel), as the event type is then available in the passed event object.

Concerning the parameters, I will then renamed the auto-resize parameter to maintainAspectRatio. Also the scale can take a float value ( to specify a fix scaling value) or the auto value for automatic scaling.

Note finally, that for now, the viewport and "backbuffer" canvas size are unchanged, only the "frontbuffer" display canvas is automatically scaled ( i need to add this back, but I wanted first to clean-up the implementation for the basic scaling)

ghost avatar Oct 17 '12 03:10 ghost

Hey Guys! sorry for not being around for a bit. I was getting married, so i was a little busy... hehehe

@obiot The reason i have to change the viewport size was to change the fullscreen_rect var in the drawManager, the problem i had, was that after resizing the canvas the tiles weren't always drawn so i did a little trace, and found out that you were using that variable to see what tiles shoud be drawn, and that value didn't change when the canvas is resized.

I've also tried changing the size of the backbuffer but that only led to zooming of the map...

i guess maybe you found a better way to do the resizing :)

and I think you're right about changing the name of the variable, it's more descriptive.

i'll test out this new fixes...

andyveliz avatar Oct 17 '12 12:10 andyveliz

nooo way, you got married ? me too this weekend !!!!!! Congratulations man :):):)

ghost avatar Oct 17 '12 12:10 ghost

hahaha! congrats to you too!! what a coincidence! hahahahah

andyveliz avatar Oct 17 '12 12:10 andyveliz

@obiot I haven't had a chance to look at this, sadly. Have been super busy since last week, but things are starting to clear up in my schedule. (Also @andyveliz my best friend's sister got married last Friday, and I was part of their wedding! Congratulations to you!)

I'm ok with renaming the variable to "maintainAspectRatio", since it is a little bit more "obvious". Good to see you also took the time to reverse the logic on it when renamed. ;) I also kind of prefer Greg's suggestion of passing an optional settings object.

I'll get to some code review on this either today or tomorrow sometime. :) I encourage others to help us all with code reviews, too. They are incredibly useful!

parasyte avatar Oct 17 '12 16:10 parasyte

oh so you actually know each other, what a small world ! else for sure please take your time, I was certainly not pushing anything :)

ghost avatar Oct 18 '12 05:10 ghost

Guys,

I really would like to merge this into the main branch, as it's starting to be difficult to manage as a separate branch, as the main one includes some bug fixes that are also required here (like with the floating object stuff). And I believe this could be a nice first milestone.

what do you think ?

we can then let this one open, or close and create a new ticket for the missing stuff (basically it's the viewport resize)

ghost avatar Oct 24 '12 03:10 ghost

Let me know what you do. If you close this one I'll make another ticket for zoom and another one for resize on orientation change.

greghouston avatar Oct 24 '12 04:10 greghouston

I will merge it a bit later in the main branch, will close this one and create 2 new tickets.

Please have a look after, If I can ask, to ensure I did not miss anything :)

ghost avatar Oct 25 '12 02:10 ghost