最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vertical alignment of canvas text - Stack Overflow

programmeradmin4浏览0评论

I am working on a canvas based app, and I have some problems with the vertical alignment of my texts. I am using floating point coordinates, which I think are the cause of the problem. I am not sure though, and am nonetheless looking for a solution. Anyway, the code I use for displaying the letter 'O' in a square, for instance, is:

context.fillRect(0, 0, 21.864951768488744, 21.864951768488744);
context.textBaseline = 'middle';
context.textAlign = alignLeft ? 'left' : 'center';
context.fillText('O', 10.932475884244372, 10.932475884244372);

The result on the canvas is that the 'O' is centered horizontally, but placed about 1 - 2 pixels above the center.

Does anyone have any thoughts on this?

I am working on a canvas based app, and I have some problems with the vertical alignment of my texts. I am using floating point coordinates, which I think are the cause of the problem. I am not sure though, and am nonetheless looking for a solution. Anyway, the code I use for displaying the letter 'O' in a square, for instance, is:

context.fillRect(0, 0, 21.864951768488744, 21.864951768488744);
context.textBaseline = 'middle';
context.textAlign = alignLeft ? 'left' : 'center';
context.fillText('O', 10.932475884244372, 10.932475884244372);

The result on the canvas is that the 'O' is centered horizontally, but placed about 1 - 2 pixels above the center.

Does anyone have any thoughts on this?

Share Improve this question edited Sep 2, 2016 at 13:58 leigero 3,28312 gold badges44 silver badges66 bronze badges asked Sep 2, 2016 at 13:50 PeterPeter 811 gold badge1 silver badge8 bronze badges 5
  • Could potentially be that the font isn't actually naturally central, which font are you using? Some fonts can display in noticeably different positions on different systems (Which is incredibly annoying at times) – DBS Commented Sep 2, 2016 at 14:15
  • I am using Arial.. – Peter Commented Sep 2, 2016 at 14:34
  • Vertical alignment refers to the font's baseline. Each individual character is not vertically centered -- or you would see dipping & rising characters of text. The capital "O", being a tall character, is likely to extend higher vertically than shorter characters of the same font. – markE Commented Sep 3, 2016 at 0:35
  • This does make sense, but when I draw larger characters I feel that I don't see this misalignment. – Peter Commented Sep 3, 2016 at 12:31
  • On further inspection I see that the misalignment remains 1 - 2 pixels when using larger fonts, it is simply not clearly visible. This means that the misalignment does not seem to scale which makes my original hypothesis (some sort of rounding error for floating coordinates) would still still make sense. As I explained in my other ment, adding a dipping character places the text 1 - 2 pixels below the center.. – Peter Commented Sep 4, 2016 at 18:20
Add a ment  | 

2 Answers 2

Reset to default 11

It's leaving space for characters with descenders. Add something with a descender like a "y" to see what I mean.

var c = document.getElementById("myCanvas");
var context= c.getContext("2d");
context.fillStyle = "#FF0000";
context.fillRect(0, 0, 21.864951768488744, 21.864951768488744);
context.font = "10px Arial";
context.fillStyle = "#FFffff";
context.textBaseline = 'middle';
context.textAlign = 'center';
context.fillText('Oy', 10.932475884244372, 10.932475884244372);
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;">
Your browser does not support the HTML5 canvas tag.
</canvas>

It seems that there indeed is a problem with floating coordinates when writing texts. As the example - http://codepen.io/petor/pen/NRPgVq - clearly shows texts cannot be displayed on subpixel coordinates and will be placed on rounded coordinates (at least on Chrome).

<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
var c = document.getElementById("myCanvas");
var context= c.getContext("2d");;
context.font = "10px Arial";
context.fillText('O', 0, 10);
context.fillText('O', 10, 10.1);
context.fillText('O', 20, 10.2);
context.fillText('O', 30, 10.3);
context.fillText('O', 40, 10.4);
context.fillText('O', 50, 10.5);
context.fillText('O', 60, 10.6);
context.fillText('O', 70, 10.7);
context.fillText('O', 80, 10.8);
context.fillText('O', 90, 10.9);
context.fillText('O', 100, 11);
</script>
</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论