Pixi.js: 如果不扩展父容器的宽度,则无法更新右对齐文本

创建于 2019-08-01  ·  4评论  ·  资料来源: pixijs/pixi.js

是的,我查看了之前的相关问题:#4341、#4002、#3476

我想创建一段文本,将其与屏幕右侧对齐,然后更新文本,同时使其与屏幕右侧对齐。

这是我的第一种方法,使用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

这是我的第二种方法,使用(stage.width - text.width) ,结果完全相同:
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

我想我可以存储app.stage的原始宽度并将我的文本与其对齐,但我宁愿不需要存储该值。 在我看来,应该有一种方法可以让单行文本从永久锚点从右向左“流动”。 我错过了一步吗?

环境

  • pixi.js版本:5.1.0
  • 浏览器和版本:75.0.3770.142
  • 操作系统和版本:Macbook Pro,操作系统 10.13.6
  • 运行示例https : //jsfiddle.net/r63ud8yL/2/
🤔 Question

所有4条评论

这个怎么样? (如果父边界是静态的。)

https://jsfiddle.net/5o0yjusv/

如果父的边界不可预测地变化,我认为需要计算不包括文本的边界。

https://jsfiddle.net/2xwb9yk8/

这是我的看法: https :

直接使用渲染器本身的宽度即可。

谢谢, @themoonrat 。 这让我走上了正轨。 我将使用app.screen而不是app.renderer 。 我的window.devicePixelRatio是 2,所以渲染器的宽度是我的窗口“实际”宽度的 2 倍。 app.screen是 1 倍。

是的,这是有道理的! 在进行任何“固定”操作时,您必须使用精确的已知像素,而容器宽度/高度对此效果不佳,因为它们对子节点的区域是动态的。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

Darker picture Darker  ·  3评论

readygosports picture readygosports  ·  3评论

distinctdan picture distinctdan  ·  3评论

softshape picture softshape  ·  3评论

lunabunn picture lunabunn  ·  3评论