Jump to content

Need Help With Resize With Aspect Ratio W/Code


JeZxLee
 Share

Recommended Posts

Hi,

Sorry, I need some help...

How can I modify the following code to resize game's canvas on browser resize while maintaining aspect ratio?

(original game canvas dimensions are 512x512)

		if (window.innerWidth != CurrentBrowserWidth || window.innerHeight != CurrentBrowserHeight)
		{
			var oldBrowserWidth = CurrentBrowserWidth;
			var oldBrowserHeight = CurrentBrowserHeight;
		
			CurrentBrowserWidth = window.innerWidth;
			CurrentBrowserHeight = window.innerHeight;
		
			renderer.autoResize = true;

			renderer.view.style.position = "absolute";
			renderer.view.style.display = "block";
			renderer.autoResize = true;
			renderer.resize(window.innerWidth, window.innerHeight);	
			
			if (oldBrowserWidth > CurrentBrowserWidth)
			{
				widthScale = ( widthScale * (CurrentBrowserWidth / oldBrowserWidth) );
			}
			else if (oldBrowserWidth < CurrentBrowserWidth)
			{
				widthScale = ( widthScale * (CurrentBrowserWidth / oldBrowserWidth) );
			}
			
			if (oldBrowserHeight > CurrentBrowserHeight)
			{
				heightScale = ( heightScale * (CurrentBrowserHeight / oldBrowserHeight) );
			}
			else if (oldBrowserHeight < CurrentBrowserHeight)
			{
				heightScale = ( heightScale * (CurrentBrowserHeight / oldBrowserHeight) );
			}
			
			stage.scale.set( widthScale, heightScale );
		}

The code above runs only on browser resize and is located in the main loop.

Any help would be appreciated, thank you!

JeZxLee

Link to comment
Share on other sites

Ok, I figured it out by adding this code to above:

if (Browser == "Other")
{
	if (widthScale < heightScale)
	{
		CurrentBrowserHeight = CurrentBrowserHeight * (widthScale / heightScale);
		heightScale = widthScale;
	}
	else if (widthScale > heightScale)
	{
		CurrentBrowserWidth = CurrentBrowserWidth * (heightScale / widthScale);
		widthScale = heightScale;
	}
}

Thanks!

JeZxLee

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