Is it possible to get the width and height of JUST THE TEXT ELEMENT in HTML5 canvas?

64 Views Asked by At

For example, if I do this on a 400 x 400 canvas with id of offscreen:

<canvas id="offscreen" width="400" height="400">
Your crappy browser does not support this!
</canvas>

var fauxCanvas = document.getElementById('offscreen');
var ctx = fauxCanvas.getContext("2d");
ctx.font = "bold 80px Arial";
ctx.fillStyle = "#000000";
ctx.fillText('Hello World', 10, 60);

The following doesn't work to give me the newly added text height/width!

var rawWidth = parseInt(ctx.width);
console.log("The Raw Width is "+rawWidth);

var rawHeight = parseInt(ctx.height);
console.log("The Raw Height is "+rawHeight);
0

There are 0 best solutions below