TextMetrics: fontBoundingBoxAscent property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since August 2023.
Note: This feature is available inWeb Workers.
The read-onlyfontBoundingBoxAscent property of theTextMetrics interface returns the distance from the horizontal line indicated by theCanvasRenderingContext2D.textBaseline attribute, to the top of the highest bounding rectangle of all the fonts used to render the text, in CSS pixels.
In this article
Value
A number, in CSS pixels.
Examples
The code below shows how you can get thefontBoundingBoxAscent for some text in a particular font.
js
const canvas = document.createElement("canvas");const ctx = canvas.getContext("2d");ctx.font = "25px serif";const text = "Foo";const textMetrics = ctx.measureText(text); // returns TextMetrics objectconst ascentCssPixels = textMetrics.fontBoundingBoxAscent;<p></p>const log = document.getElementById("log");log.innerText = `fontBoundingBoxAscent: ${ascentCssPixels}`;The ascent in CSS pixels for the text "Foo" in a 25px serif font is shown below.
Specifications
| Specification |
|---|
| HTML> # dom-textmetrics-fontboundingboxascent-dev> |