Jump to content

Stage and DisplayObjects resize to fit the screen


caymanbruce
 Share

Recommended Posts

How can I resize DisplayObjects on my screen when I resize the window? This is a very common requirement but I can't find tutorial bout this. For example, when I press F11 to make the window fullscreen, the stage and all displayObjects in the stage should adjust its size to fit the screen. When I drag the browser edge to resize the window the objects should also resize to fit the screen. How to achieve this?

I have this code now, but it doesn't work very well because the ratio doesn't seem quite right.

window.addEventListener('resize', resize);

function resize(event) {
    const w = window.innerWidth;
    const h = window.innerHeight;
    let ratio = Math.min( w / GAME_WIDTH, h / GAME_HEIGHT);
    stage.scale.x = stage.scale.y = ratio;
    renderer.resize(Math.ceil(GAME_WIDTH * ratio), Math.ceil(GAME_HEIGHT * ratio));
};

Link to comment
Share on other sites

6 minutes ago, themoonrat said:

 

I guess I am the unlucky one. This code doesn't work as I expected when I write

window.addEventListener('resize', resize);

function resize(event) {
    scaleScene(renderer, stage);
};

When I switch to full screen mode my stage doesn't expand to full screen, it only adds edges to top and bottom.

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...