image

String metrics in canvas :

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "30px Arial";
var txt = "Hello World"
ctx.fillText("width:" + ctx.measureText(txt).width, 10, 50)
ctx.fillText(txt, 10, 100);

HTML canvas measureText() Method I can only take the width.

I was able to insert
in SVG, but it doesn’t seem to be possible in canvas

There is a possible breakable:0/1 attribute between the letters.

  • For now, I’ll just target Japanese and set everything to 1.

lineheight

If you write 333 with point 0,0 and size:100 and getBounds

  • ctx.measureText(“333”)
    • TextMetrics {width: 166.845703125}
  • window.x.textItem.getBounds()
    • Rectangle {_x: -98.54999542236328, _y: -50, _width: 197.09999084472656, _height: 120, _owner: PointText, 
} “33.”
  • textItem.getBounds()
    • Rectangle {_x: -98.54999542236328, _y: -50, _width: 131.39999389648438, _height: 120, _owner: PointText, 
}
  • ctx.measureText(“33”)
    • TextMetrics {width: 111.23046875}

paper.getBounds.height is simply fontsize * 1.2

pRegroup-done-2019


This page is auto-translated from /nishio/ä»˜çź‹ăźè‡Șć‹•ăƒ•ă‚©ăƒłăƒˆă‚”ă‚€ă‚șèȘżæ•Ž using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I’m very happy to spread my thought to non-Japanese readers.