Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] positioning nested groups #747

Closed
1 of 3 tasks
JohnPaulHarold opened this issue Dec 9, 2024 · 5 comments
Closed
1 of 3 tasks

[Question] positioning nested groups #747

JohnPaulHarold opened this issue Dec 9, 2024 · 5 comments

Comments

@JohnPaulHarold
Copy link

JohnPaulHarold commented Dec 9, 2024

Describe your question

I'm having issues trying to align nested groups. It's quite possible I am doing this wrong, so it's a question for now, rather than viewing this as a bug.

Your code (either pasted here, or a link to a hosted example)

a simple CodePen is here, https://codepen.io/johnpaulharold/pen/EaYyjOa

Screenshots
a grab of what the codepen shows

Screenshot 2024-12-09 at 14 44 39

Environment (please select one):

  • Code executes in browser (e.g: using script tag to load library)
  • Packaged software (e.g: ES6 imports, react, angular, vue.js)
  • Running headless (usually Node.js)

If applicable:

Desktop (please complete the following information):

  • OS: OSX
  • Browser Chrome stable
  • Version 131

Additional context

My expectation with the code is that the group of 3 red buttons should be aligned to that the vertical guideline appears to cut straight through the middle of the menu, which should be the "B" button. Instead, it appears as though only the first button in the group is considered. Am I doing something wrong here?

@jonobr1
Copy link
Owner

jonobr1 commented Jan 6, 2025

Thanks for posting with a great example. This is a bug. Working on it 👍

@jonobr1
Copy link
Owner

jonobr1 commented Jan 6, 2025

This patch fixes the underlying issue: #755

But, your question is still valid. How do nested elements layout? Hopefully the below helps clarify your question:

By default Two.js elements render in their own transformation matrix at the center. So the 0, 0 (also known as the origin) of a Two.Rectangle is not the upper-left corner, but the center. As a result, your code's origin is the center of the first rectangle. In order to move the origin of the Menu group is to offset the positions of the buttons by the total width of all the buttons. You can do that with a simple method: menu.center()

@jonobr1 jonobr1 closed this as completed Jan 6, 2025
@JohnPaulHarold
Copy link
Author

@jonobr1 Thank you for looking into this. I've update the pen but am still seeing issues. I might be doing something wrong still.

I added menu.center() to the menu grouping. This does change the layout, but now the alignment is towards the last element in the group. Like this,

Screenshot 2025-01-11 at 12 41 21

The change I made looks like

menu.position.x = world.width / 2;
menu.position.y = world.height / 2;
+ menu.center();

@jonobr1
Copy link
Owner

jonobr1 commented Jan 11, 2025

I haven't published the changes yet to NPM. You'll need to use the GitHub dev branch until I do.

@JohnPaulHarold
Copy link
Author

I haven't published the changes yet to NPM. You'll need to use the GitHub dev branch until I do.

urggh, how silly of me. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants