Skip to content

Text width property is inaccurate #70

@ghost

Description

I've noticed that when creating a text sprite, the width property returned is inaccurate. This makes positioning text difficult. I think it's something to do with the font having to be set on the canvas context before measureText is called. The following code seems to fix the issue.

    testText = g.text("HELLO, WORLD!", "75px Verdana", "orange")
    console.log(testText.width) //inaccurate
    g.canvas.ctx.font = testText.font
    testText.width = g.canvas.ctx.measureText(testText.content).width 
    console.log(testText.width) //now accurate

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions