davestephans

Canvas size did not change after setUserScale

Recommended Posts

Hi,

I am facing one problem with game resizing, here i resized the whole canvas width and height based on the device browser width and height.

maximum width and hieght is 1920x1080.

The problem is when i called the game.scale.setUserScale(scaleFactor,scaleFactor), after that the canvas width and height did not change.

Here the canvas width and height will be change when mobile device get rotation like portrait to landscape and landscape to portrait.

What will be the problem, could you guys any one help me. My code is,

 

var windowWidth = window.innerWidth;

var windowHeight = window.innerHeight;

var GAME_MAX_WIDTH = 1920;

var GAME_MAX_HEIGHT=1080;   

var scaleFactor=1;

    
    if((GAME_MAX_WIDTH/windowWidth) > (GAME_MAX_HEIGHT/windowHeight))
    {
        scaleFactor = (windowWidth / GAME_MAX_WIDTH);        
    }
    else 
    {
        scaleFactor = (windowHeight / GAME_MAX_HEIGHT);
    }    
    
    var positionX  = windowWidth/2 - (GAME_MAX_WIDTH*scaleFactor)/2;
    var positionY  = windowHeight/2 - (GAME_MAX_HEIGHT*scaleFactor)/2;
    
    var view = document.getElementById('game_div_id'); 
            
    view.style.top = positionY+"px";
    view.style.left = positionX+"px";
        
    mainGame.scale.scaleMode = Phaser.ScaleManager.USER_SCALE;
    mainGame.scale.setUserScale(scaleFactor,scaleFactor);

 

Thanks,

Share this post


Link to post
Share on other sites

Hi @davestephans,

Just a quick question, what is the exact resolutions you are testing on? It's working on resolutions lower than the max one you specified, right?

Because, going over this "if":

if((GAME_MAX_WIDTH/windowWidth) > (GAME_MAX_HEIGHT/windowHeight))
{
  scaleFactor = (windowWidth / GAME_MAX_WIDTH);        
}
else 
{
  scaleFactor = (windowHeight / GAME_MAX_HEIGHT);
}    
    

If you are testing on resolution 1920x1080, then the "scaleFactor" will always be set to 1, and therefore you would be calling:

mainGame.scale.setUserScale(1, 1);

Which wouldn't actually resize the screen. Would that be the case?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.