kittykatattack / ga

The world's tiniest, cutest and funnest game engine
453 stars 85 forks source link

Text width property is inaccurate #70

Open ghost opened 6 years ago

ghost commented 6 years ago

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