Responsive canvas (adaptive, resizable)

545 views
Skip to first unread message

Ctibor Laky

unread,
Jan 2, 2014, 3:16:22 PM1/2/14
to craf...@googlegroups.com
How to achieve responsive canvas for a crafty game? I the way that viewport will adapt to new window size? Is it possible? Because when I was trying to resize canvas by js, everything messed up :/

canvas = Crafty.stage.elem.getElementsByTagName("canvas")[0]
canvas.width = window.innerWidth-100;
canvas.height = window.innerHeight-100;

Canvas resized correctly, but the game was distroyed...

Thanks for answers in advance ;)

figosi...@gmail.com

unread,
Jan 8, 2014, 11:22:47 AM1/8/14
to craf...@googlegroups.com
Have the same bl...dy problem.

Any help?

Sam Fereday

unread,
Jan 9, 2014, 10:00:21 AM1/9/14
to craf...@googlegroups.com
It's possible that on the resize all of the values have changed such as coordinates and whatnot perhaps? You may have to reload all elements to recalculate the positions of everything and then place it all down again. This is just a theory, so by all means correct me if I'm wrong.

Kevin Simper

unread,
Jan 10, 2014, 5:08:36 AM1/10/14
to craf...@googlegroups.com
You can bind a function to the window resize event, and then trigger the Crafty.viewport.reload() function.

Remeber to use some form of a debouncer, because dragging the window will fire the window.resize function maybe a 1000 times and it will be very heavy.

Use something like this:

var delay = (function(){
  var timer;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();
$(window).resize(function() {
    delay(function(){
      Crafty.viewport.reload();
      //...
    }, 500);
});

starwed

unread,
Jan 10, 2014, 4:39:22 PM1/10/14
to craf...@googlegroups.com
Crafty.viewport.reload should already trigger on resize, I think.

But it looks like if you set the viewport size explicitly,  reload won't change it.  That kind of makes sense, but we should probably support a responsive viewport other than fullscreen.

We should certainly trigger an event when the viewport resizes, and automatically deal with the canvas size when it happens.

Ctibor Laky

unread,
Jan 12, 2014, 10:41:36 AM1/12/14
to craf...@googlegroups.com
Viewport.reload doesnt do the thing, if anyone have working example please share it via jsfiddle or similar service, thanks ;)

Ctibor Laky

unread,
Jan 12, 2014, 10:43:21 AM1/12/14
to craf...@googlegroups.com
My attempt with viewport.reload():

window.onresize = function() { // responsible game borders
var stage = Crafty.stage.elem,
canvas = stage.getElementsByTagName("canvas")[0];
stage.style.width = Game.width() + "px";
stage.style.height = Game.height() + "px";
canvas.width = Game.width();
canvas.height = Game.height();
  Crafty.viewport.reload();
};

starwed

unread,
Jan 12, 2014, 2:20:51 PM1/12/14
to craf...@googlegroups.com
You'll probably also want to specifically resize the viewport as well.

I just landed a [change](https://github.com/craftyjs/Crafty/pull/691) that resizes the canvas and stage when you change the viewport dimensions, so this'll be easier in the next version of Crafty.  :)
Reply all
Reply to author
Forward
0 new messages