support for screen autoresize
Implement graphics auto-resize in melonJS, and use the browser resize event, to create (full)screen size independent games.
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!
@ciangames thank you for the feedback !
@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' !
@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 } });
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....
@obiot
Regarding orientation, that makes sense. No need for a ticket for that one.
That just leaves zoom.
@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 : 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)
@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)
@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.
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.
I'm ok to spend more time on this, but do you really believe it's that critical ?
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)
@obiot: Nope, not at all critical! Just an observation. ;)