Jump to content

How to scale and center Pixi.js stage in the maximized canvas element?


Alexander Farber
 Share

Recommended Posts

Hello and good evening!

I am trying to develop a board game and would like to:

  • Use HTML elements like buttons etc.
  • Give Pixi.js max possible space and fit + center-align the game board inside the canvas element

Ivan had suggested to scale the stage and the children will be scaled automatically.

I have tried it in the attached file (and here is a jsfiddle), but just cannot figure out the math for scaling. Also, my dragging code breaks.

Regards
Alex

 

Screenshot 2020-10-20 at 20.20.08.png

test-pixi-stage-scale.html

Link to comment
Share on other sites

Try this :

var ratio;

resize()
{
   ratio = Math.min(window.innerWidth / your_default_screen_width, window.innerHeight /  
 your_default_screen_height);
}

_applyRatio(displayObj: PIXI.DisplayObject, ratio: number)
{
     if (ratio == 1) return;
     var obj: any = displayObj;
     obj.position.x = obj.position.x * ratio;
     obj.position.y = obj.position.y * ratio;
     obj.scale.x = obj.scale.x * ratio;
     obj.scale.y = obj.scale.y * ratio;
}

_applyRatio(bunny, 1 / ratio);

 

Edited by ozgun
Link to comment
Share on other sites

Thanks for you suggestion ozgun, but I didn't want to go through all the children of app.stage and apply some transforms to all of them.

At the end I have solved my problem with the attached file.

I have also posted my answer at Stackoverflow: https://stackoverflow.com/a/64528244/165071

Screenshot 2020-10-25 at 21.01.26.png

test-pixi-stage-scale-bunny.html

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