例如,如果我在400 x 400画布上执行此操作,ID为off屏幕:
<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);
以下内容无法为我提供新添加的文字高度/宽度!
var rawWidth = parseInt(ctx.width);
console.log("The Raw Width is "+rawWidth);
var rawHeight = parseInt(ctx.height);
console.log("The Raw Height is "+rawHeight);