如何在HTML画布上找到文本的高度? [英] How can you find the height of text on an HTML canvas?

查看:117
本文介绍了如何在HTML画布上找到文本的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

规范有一个context.measureText(text)函数,它会告诉你需要多少宽度来打印该文本,但我找不到一种方式来找出它是多高。我知道它是基于字体,但我不知道将字体字符串转换为文本高度。

The spec has a context.measureText(text) function that will tell you how much width it would require to print that text, but I can't find a way to find out how tall it is. I know it's based on the font, but I don't know to convert a font string to a text height.

推荐答案

strong> UPDATE - 有关此工作的示例,我在 Carota编辑器中使用了此技术

UPDATE - for an example of this working, I used this technique in the Carota editor.

从ellisbben的回答开始,下面是一个增强版本,以获得从基线上升和下降,即 tmAscent tmDescent 由Win32的 GetTextMetric API。如果您想使用不同字体/大小的跨度进行字封装的文本运行,则需要这样做。

Following on from ellisbben's answer, here is an enhanced version to get the ascent and descent from the baseline, i.e. same as tmAscent and tmDescent returned by Win32's GetTextMetric API. This is needed if you want to do a word-wrapped run of text with spans in different fonts/sizes.

上述图片是在Safari的画布上生成的,红色是顶部行,其中画布被告知绘制文本,绿色是基线,蓝色是底部(因此红色到蓝色是完整高度)。

The above image was generated on a canvas in Safari, red being the top line where the canvas was told to draw the text, green being the baseline and blue being the bottom (so red to blue is the full height).

使用jQuery简洁:

Using jQuery for succinctness:

var getTextHeight = function(font) {

  var text = $('<span>Hg</span>').css({ fontFamily: font });
  var block = $('<div style="display: inline-block; width: 1px; height: 0px;"></div>');

  var div = $('<div></div>');
  div.append(text, block);

  var body = $('body');
  body.append(div);

  try {

    var result = {};

    block.css({ verticalAlign: 'baseline' });
    result.ascent = block.offset().top - text.offset().top;

    block.css({ verticalAlign: 'bottom' });
    result.height = block.offset().top - text.offset().top;

    result.descent = result.height - result.ascent;

  } finally {
    div.remove();
  }

  return result;
};

除了文本元素之外,我还添加了一个带有 display:inline -block ,所以我可以设置它的 vertical-align 样式,然后找出浏览器放在哪里。

In addition to a text element, I add a div with display: inline-block so I can set its vertical-align style, and then find out where the browser has put it.

所以你得到一个对象与 ascent descent height (为方便起见,它只是 ascent + descent )。要测试它,值得有一个绘制水平线的函数:

So you get back an object with ascent, descent and height (which is just ascent + descent for convenience). To test it, it's worth having a function that draws a horizontal line:

var testLine = function(ctx, x, y, len, style) {
  ctx.strokeStyle = style; 
  ctx.beginPath();
  ctx.moveTo(x, y);
  ctx.lineTo(x + len, y);
  ctx.closePath();
  ctx.stroke();
};

然后你可以看到文本在canvas上相对于顶部,基线和底部的位置:

Then you can see how the text is positioned on the canvas relative to the top, baseline and bottom:

var font = '36pt Times';
var message = 'Big Text';

ctx.fillStyle = 'black';
ctx.textAlign = 'left';
ctx.textBaseline = 'top'; // important!
ctx.font = font;
ctx.fillText(message, x, y);

// Canvas can tell us the width
var w = ctx.measureText(message).width;

// New function gets the other info we need
var h = getTextHeight(font);

testLine(ctx, x, y, w, 'red');
testLine(ctx, x, y + h.ascent, w, 'green');
testLine(ctx, x, y + h.height, w, 'blue');

这篇关于如何在HTML画布上找到文本的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆