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

Hi Olivier

I performed some tests with the Screen Auto Resize (for auto resize the canvas, with "auto" scale) and with the hints below (for resize the game DIV):

http://www.html5rocks.com/en/tutorials/casestudies/gopherwoord-studios-resizing-html5-games/

And has worked like a charm!

Thanks!

aaschmitz avatar Oct 25 '12 02:10 aaschmitz

@ciangames thank you for the feedback !

ghost avatar Oct 25 '12 03:10 ghost

@greghouston

for the orientation change, what I can do is to create a specific channel you can listen to, like how i did for the resize event or even also trigger the resize event on orientation change (so that you just to listen to the resize), but however I don't really see the benefit of the me.sys.orientation since it's already available then using the window.orientation property.

something like this should work : if Math.abs(window.orientation) === 90 then 'portrait' !

ghost avatar Oct 25 '12 10:10 ghost

@greghouston

an even better example using the last commit :

// register to the window resize channel me.event.subscribe(me.event.WINDOW_ONRESIZE, function() { if (Math.abs(window.orientation) === 90) { // portrait : do something } else { // paysage : do something else } });

ghost avatar Oct 25 '12 10:10 ghost

bonus question to all :

does anyone has an idea on how to remove/fix this "flickering" effect when the window is resizing (as a reminder, when a canvas is resized; it's content is cleared) ? I tried to defer the function call, but it does not change anything....

ghost avatar Oct 25 '12 10:10 ghost

@obiot

Regarding orientation, that makes sense. No need for a ticket for that one.

That just leaves zoom.

greghouston avatar Oct 25 '12 17:10 greghouston

@obiot: Repainting on resize event like that will cause some performance decrease while resizing. (I know, it's not very common, but maybe it's noticeable on older hardware.)

If that's a concern, you could instead do a "smart" defer, that waits some short period (like 35ms -- perfect for 30fps) and resets itself if the "smart defer" is run again before the 35ms lapse. Then the user resizes by quickly dragging the window handle; the canvas resize will take effect only after the resize handle slows down.

Function.prototype.timeout = function (time /*, args */) {
    var fn = this, args = Array.prototype.slice.call(arguments, 1);
    if (this.delayId) {
        window.clearTimeout(this.delayId);
    }
    this.delayId = window.setTimeout(function() {
        return fn.apply(fn, args);
    }, time || 0);
    return this.delayId;
};

Use it:

me.video.updateDisplaySize.timeout(35, scale, scale);

parasyte avatar Oct 25 '12 18:10 parasyte

@parasyte : yes, I thought about it as well, but is it really a concern ? I tried it on all my machines yesterday and did not notice any lag or issue, so..... and after all it's just re-sizing, people should not spend time resizing the window continuously ?

let's keep anyway this piece of code here, in case of major complains on the feature. (what would need to be modified in my code is however to clear the pending deferred function, in case another is deferred meanwhile)

ghost avatar Oct 26 '12 03:10 ghost

@parasyte : found this blog entry (http://drupalmotion.com/article/debounce-and-throttle-visual-explanation) while googling our suggestion, this could be actually applied to a lots of things in melonJS (mousemove as a start, as in the bottom of the blog entry example)

ghost avatar Oct 26 '12 05:10 ghost

@parasyte : else i think that if I could just store the timer id returned by the defer fuction, and clear it before deferring again the next resize function call, it should be enough (for a window resizing). Keep in mind that deferred function are only executed when the stack is empty, which is once every frame.

ghost avatar Oct 26 '12 06:10 ghost

Well, it's not the clearTimeout that will fix the problem with performance, but the actual time passed to setTimeout. I agree it will prevent multiple defers from executing per frame. But for performance you want the delay to be longer than a single frame.

parasyte avatar Oct 26 '12 08:10 parasyte

I'm ok to spend more time on this, but do you really believe it's that critical ?

ghost avatar Oct 26 '12 09:10 ghost

So I did that for now (easy & cheap solution). I will give it more attention tonight once back at home, and will try this with my old mbp and several browsers (to see If there is a perfromance issue we should worry about)

ghost avatar Oct 26 '12 10:10 ghost

@obiot: Nope, not at all critical! Just an observation. ;)

parasyte avatar Oct 26 '12 15:10 parasyte