Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

measureText make actualBoundingBoxRight mistake when textAlign is center #1909

Closed
sekaiamber opened this issue Oct 15, 2021 · 0 comments · Fixed by #2109
Closed

measureText make actualBoundingBoxRight mistake when textAlign is center #1909

sekaiamber opened this issue Oct 15, 2021 · 0 comments · Fixed by #2109

Comments

@sekaiamber
Copy link

sekaiamber commented Oct 15, 2021

when I set ctx.textAlign = 'center', then call measureText, will get wrong actualBoundingBoxRight result.

Steps to Reproduce

const Canvas = require('canvas');
const canvas = Canvas.createCanvas();
const ctx = canvas.getContext('2d');
console.log(ctx.measureText('aaaa'));
/***
{
  width: 24,
  actualBoundingBoxLeft: -0.400390625,
  actualBoundingBoxRight: 23.3466796875,
  actualBoundingBoxAscent: 5.3564453125,
  actualBoundingBoxDescent: 0.17578125,
  emHeightAscent: 7.7001953125,
  emHeightDescent: 2.2998046875,
  alphabeticBaseline: -2.8994140625
}
***/
// then set textAlign center
ctx.textAlign = 'center';
console.log(ctx.measureText('aaaa'));
/***
{
  width: 24,
  actualBoundingBoxLeft: 11.599609375,  
  actualBoundingBoxRight: 35.3466796875, // <---  should be 23.3466796875 - 12
  actualBoundingBoxAscent: 5.3564453125,
  actualBoundingBoxDescent: 0.17578125,
  emHeightAscent: 7.7001953125,
  emHeightDescent: 2.2998046875,
  alphabeticBaseline: -2.8994140625
}
***/

Here is the result screenshot when using node-canvas:
image

And here is the screenshot when run on Chrome:
image

It seem that, the correct way to get actualBoundingBoxRight is

actualBoundingBoxRight @ center align = actualBoundingBoxRight @ left align - width / 2

but now use

actualBoundingBoxRight @ center align = actualBoundingBoxRight @ left align + width / 2

Your Environment

  • yarn list v1.22.11, canvas@2.8.0
  • MacOS 10.14.6, node v14.17.4
@zbjornson zbjornson self-assigned this Sep 4, 2022
zbjornson added a commit that referenced this issue Sep 4, 2022
This bug goes back 10 years to the original implementation.

Fixes #1909
zbjornson added a commit that referenced this issue Sep 4, 2022
This bug goes back 10 years to the original implementation.

Fixes #1909
zbjornson added a commit that referenced this issue Sep 4, 2022
This bug goes back 10 years to the original implementation.

Fixes #1909
zbjornson added a commit that referenced this issue Sep 4, 2022
This bug goes back 10 years to the original implementation.

Fixes #1909
zbjornson added a commit that referenced this issue Sep 4, 2022
This bug goes back 10 years to the original implementation.

Fixes #1909
chearon pushed a commit that referenced this issue Sep 6, 2022
This bug goes back 10 years to the original implementation.

Fixes #1909
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants