Jump to content

Automatically Resize Entire Canvas To Browser Dimensions?


JeZxLee
 Share

Recommended Posts

Hi,

We were going to release an SDL2 game on the Windows(R) Store & maybe XBox Live,

But Microsoft(R) dragged their feet so we said so long to them and sold our company XBox One for $60.

We are now going to make a cross-platform Internet game using: "PixiJSv4" & "Audio5js".

Just started with the coding today...

How can we automatically resize the game's canvas to fill the browser's dimensions while maintaining aspect ratio?

Thanks!

JeZxLee

Link to comment
Share on other sites

Hi Again!

So I've got the following code in the main loop:

renderer.autoResize = true;

renderer.view.style.position = "absolute";
renderer.view.style.display = "block";
renderer.autoResize = true;
renderer.resize(window.innerWidth, window.innerHeight);	

But it only resizes the canvas and not the sprites?

Any help would be appreciated, thanks!

JeZxLee

Link to comment
Share on other sites

25 minutes ago, alex_h said:

You probably don't want to resize the canvas, you want to scale it up using css. You might then need to adjust the width or height a bit if the aspect ratios of the game and the window don't match.

Hi,

I was hoping there was something I could do in JavaScript?

I am not really knowledgeable of CSS.

I do have a 1 line CSS file which loads on app start.

Can you provide an example in CSS where browser resize would scale the canvas?

Also would this effect mouse/touch coordinates?

Thanks!

JeZxLee 

 

Link to comment
Share on other sites

If you scale both dimensions by the same factor then the ratio between them will be maintained. Otherwise it wont be. So you can't both scale your content to exactly fill the window and maintain the aspect ratio, because the former requires the scaling factors to sometimes be different and the latter requires them to always be the same.

And yes, Pix'si hit detection takes Pixi scaling into account but not CSS scaling, so CSS scaling will break Pixi's ability to know which DisplayObject you clicked on for instance. So if you use CSS scaling instead of Pixi scaling it will make some things harder..

 

Link to comment
Share on other sites

You can use renderer.resize(w, h) to resize the canvas and stage.scale.set(s) or stage.scale.set(xScale, yScale) to scale whatever you add to stage.

So you want to fill the window vertically and overflow horizontally?

I can post snippet or maybe CodePen demo but need to confirm what you're after first :)

Link to comment
Share on other sites

1 hour ago, JeZxLee said:

Hi,

Have no idea what game I am making.

I am just putting together a 2-D framework to make a game now.

JeZxLee

Cool sounds familiar. My point just that strategies for adapting to window dimensions are different for side-scrolling platformer than for overhead view with scrolling all directions than for non-scrolling game, etc.

Link to comment
Share on other sites

22 minutes ago, JeZxLee said:

Hi,

Still not perfect with browser resizing.

Please view below screenshot to see current issue:

http://16bitsoft.com/files/PixiAudio5JS/DevPhotos/PixiScreenResizeIssue-01.png

There are scrollbars in the browser?

Any idea how to get rid of those?

Thanks!

JeZxLee

Try this CSS. Sorry can't remember exaclty which part fixes the scollbar but I think alltogether that will fix it

body {
    padding: 0;
    margin: 0;
    overflow: hidden;
}

canvas {
    padding: 0;
    margin: 0;
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...