简体   繁体   English

如何使用JavaScript获取文本顶行的位置

[英]How do I get the position of text top line using JavaScript

I am looking at a Daniel Earwicker's answer to "How can you find the height of text on an HTML canvas?". 我正在看Daniel Touwicker的回答 “如何在HTML画布上找到文本的高度?”。
It provides me with a way to get certain metrics on text height. 它为我提供了一种获取文本高度的特定指标的方法。

However what I am interested in is getting the metric for a black line below: 但是我感兴趣的是获得下面黑线的指标:
ABCD指标

Is it possible to get it using this approach? 是否有可能使用这种方法?
See http://jsfiddle.net/BL5nP (hardcoded black line). http://jsfiddle.net/BL5nP (硬编码黑线)。

HTML: HTML:

<canvas width="500" height="500"></canvas>

JavaScript: JavaScript的:

// function from https://stackoverflow.com/questions/1134586/how-can-you-find-the-height-of-text-on-an-html-canvas/9847841#answer-9847841
var getTextHeight = function(font) 
{
    var text = $('<span>Ag</span>');
    text[0].style.font = 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;
};

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();
};

var ctx = $('canvas')[0].getContext('2d');
var x = 10;
var y = 10;

var font = '36pt Times';
var message = 'ABCD';

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

var w = ctx.measureText(message).width;
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');
testLine(ctx, x, y + 10, w, 'black'); // this line is what I am interested in

There is this small fontmetrics library that does the trick https://github.com/Pomax/fontmetrics.js 有一个小的fontmetrics库可以解决这个问题https://github.com/Pomax/fontmetrics.js

The only reliable method to get the ascent (top of letter l) is to simply draw the text on the canvas and count white lines until top boundary is visible (this is what the library above does). 获得上升(字母l的顶部)的唯一可靠方法是简单地在画布上绘制文本并计算白线直到顶部边界可见(这是上面的库所做的)。

There are couple of approximations by aligning inline spans and divs, but none of them gives reliable cross-browser results. 通过对齐内联跨度和div,有几个近似值,但它们都没有提供可靠的跨浏览器结果。 Eg this one relies on getBoundingClientRect() and can be made to work in certain limited cases, but not in general: http://jsfiddle.net/Exceeder/rP7Jj/ 例如,这个依赖于getBoundingClientRect()并且可以在某些有限的情况下工作,但不是一般的: http//jsfiddle.net/Exceeder/rP7Jj/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文本放置在圆圈的顶部? - How do I position a text on top of a circle? 当我随机化它们的 position(加载并使用 setInterval)时,带有图像的 div 总是位于带有文本的 div 的顶部。 JavaScript - divs with images always get on the top of divs with text when I randomize their position (on load and using setInterval). JavaScript 如何使用javascript仅设置第一行文本的样式 - How do I style only the first line of text using javascript 如何使用javascript获取文本光标位置 - How to get text cursor position using javascript 如何获得包装内容的左上角位置? - How do I get top left position of wrapped content? 使用javascript获取网页的顶部位置 - get top position of the web page using javascript 如何在 JavaScript/Vue 的文本字段中获取插入符号的当前 position? - How do I get the current position of the caret in a text-field in JavaScript/Vue? 如何使用JavaScript获取活动标签的文本值? - How do I get the text value of the active tab using JavaScript? 如何使用 JavaScript 获取文本输入字段的值? - How do I get the value of text input field using JavaScript? 如何在jQuery / javascript中获取文本本身而不是文本元素顶部的位置 - How to get position of top of text itself, rather than text element, in jQuery/javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM