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

Cannot update right-aligned text without expanding width of parent container #5979

Closed
RobertAKARobin opened this issue Aug 1, 2019 · 4 comments
Labels
🤔 Question User question, similar to Help Wanted or Needs Help. These can be addressed whenever someone has tim

Comments

@RobertAKARobin
Copy link

RobertAKARobin commented Aug 1, 2019

Yes, I looked at prior related issues: #4341, #4002, #3476

I'd like to create a piece of text, align it to the right of the screen, then update the text, while keeping it aligned to the right of the screen.

Here's my first approach, using anchor.x = 1:
https://jsfiddle.net/r63ud8yL/1/

const app = new PIXI.Application({resizeTo: document.body, backgroundColor: 0xFFFFFF})
document.body.append(app.view)

// Forcing app.stage to occupy width of screen
// Is there a better way to do this?
const neCorner = new PIXI.Sprite()
neCorner.x = 0
neCorner.y = 0
const swCorner = new PIXI.Sprite()
swCorner.x = document.body.clientWidth
swCorner.y = document.body.clientHeight
app.stage.addChild(neCorner, swCorner)

const text = new PIXI.Text('first')
app.stage.addChild(text)
text.anchor.x = 1

// Align text to right edge
text.x = app.stage.width

// Replacing text with a longer string
text.text = 'second'
// It extends past the edge of the window; only 'sec' is visible

Here's my second approach, using (stage.width - text.width), which has the exact same result:
https://jsfiddle.net/r63ud8yL/2/

const app = new PIXI.Application({resizeTo: document.body, backgroundColor: 0xFFFFFF})
document.body.append(app.view)

// Forcing app.stage to occupy width of screen
// Is there a better way to do this?
const neCorner = new PIXI.Sprite()
neCorner.x = 0
neCorner.y = 0
const swCorner = new PIXI.Sprite()
swCorner.x = document.body.clientWidth
swCorner.y = document.body.clientHeight
app.stage.addChild(neCorner, swCorner)

const text = new PIXI.Text('first')
app.stage.addChild(text)

// Align text to right edge
text.x = (app.stage.width - text.width)

// Replacing text with a longer string
// It extends past the edge of the window; only 'sec' is visible
text.text = 'second'

// Attempting to align text to right edge again...
text.x = (app.stage.width - text.width)
// ...but the longer text expanded the width of app.stage
// so still only 'sec' is visible

I suppose I could store the original width of app.stage and align my text to that, but I would prefer to not need to store that value. It seems to me that there should be a way to get single-line text to "flow" from right to left from a permanent anchor. Am I missing a step?

Environment

@mofnire
Copy link
Contributor

mofnire commented Aug 3, 2019

How about this? (If the parent bounds is static.)

https://jsfiddle.net/5o0yjusv/

If the bounds of the parent change unpredictably, need to calculate its bounds excluding the text, I think.

https://jsfiddle.net/2xwb9yk8/

@themoonrat
Copy link
Member

Here's my take: https://jsfiddle.net/b9zp5kuh/

Just go straight to using the width of the renderer itself.

@themoonrat themoonrat added 🤔 Question User question, similar to Help Wanted or Needs Help. These can be addressed whenever someone has tim Version: v5.x labels Aug 3, 2019
@RobertAKARobin
Copy link
Author

Thanks, @themoonrat . This got me on the right track. I'm going to go with app.screen instead of app.renderer. My window.devicePixelRatio is 2, so the width of the renderer is 2x the "actual" width of my window. app.screen is 1x, though.

@themoonrat
Copy link
Member

Yeps that makes sense! When doing anything 'pinning', you have to work in exact known pixels, and container widths / heights don't work well for that because they're dynamic to the area of their children.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤔 Question User question, similar to Help Wanted or Needs Help. These can be addressed whenever someone has tim
Projects
None yet
Development

No branches or pull requests

3 participants