是的,我查看了之前的相关问题:#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这是我的看法: https :
直接使用渲染器本身的宽度即可。
谢谢, @themoonrat 。 这让我走上了正轨。 我将使用app.screen
而不是app.renderer
。 我的window.devicePixelRatio
是 2,所以渲染器的宽度是我的窗口“实际”宽度的 2 倍。 app.screen
是 1 倍。
是的,这是有道理的! 在进行任何“固定”操作时,您必须使用精确的已知像素,而容器宽度/高度对此效果不佳,因为它们对子节点的区域是动态的。