Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

386
Views
Canvas: Text width is not equal to the summary of the widths of its characters

In most cases, they are the same. But I found this special case, it is different when I change the order of characters.

const ctx = document.createElement('canvas').getContext('2d');
const tw = text => ctx.measureText(text).width;

tw('Tr') - tw('T') - tw('r') // -0.3662109375
tw('rT') - tw('T') - tw('r') // 0

Can anyone tell me the rules of this behavior? Thank you!

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

This is because of kerning, which is how some characters will be more or less close to their neighbors. This will depend on the font used, and the characters shown.

Chrome recently added support for a .fontKerning property that we can use to disable this,

if ("fontKerning" in CanvasRenderingContext2D.prototype) {
  const canvas = document.querySelector("canvas");
  const ctx = canvas.getContext("2d");
  ctx.textBaseline = "middle";
  ctx.font = "30px sans-serif";
  ctx.fillText("AV", 0, 30);
  console.log("with kerning", ctx.measureText("AV").width);
  ctx.fontKerning = "none";
  console.log("without kerning", ctx.measureText("AV").width);
  ctx.fillText("AV", 0, 60);
  console.log("sum of chars", ctx.measureText("A").width + ctx.measureText("V").width);
}
else {
  console.log("your browser doesn't support setting fontKerning");
}
<canvas></canvas>

and you could use mono-space fonts to avoid it, but that would still not be bullet proof because even then some glyphs just can't be measured as you did.

So the best is to measure the full text that you want to measure.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!