Takumi

The measure() API

Measure node dimensions and text runs with measure() without painting.

measure() lays out a tree and returns each box's size and position without painting it. Use it to fit a headline into a fixed width, or to place a badge next to a measured box.

Measure an element
import {  } from "takumi-js";

const { width,  } = await (< ="text-xl">I'm a text node</>); 
const width: number

The measured width of the node.

measure() takes the same input and options as render(): JSX, an HTML string, or a node tree, with css, fonts, and images. It runs on the same default renderer, so fonts loaded for rendering measure the same way. Without width and height, the root sizes to its content.

To measure on a renderer you manage, pass it a node tree:

Measure with the native renderer
import {  } from "takumi-js/node";
import {  } from "takumi-js/helpers/jsx";

const  = new ();
const { ,  } = await (< ="text-xl">I'm a text node</>);

const { width,  } = await .(, {  }); 
const width: number

The measured width of the node.

Returned fields

measure() resolves to a MeasuredNode.

Prop

Type

Description

width?number

Laid-out width

height?number

Laid-out height

transform?[number, number, number, number, number, number]

Matrix that places the box in the image

children?MeasuredNode[]

Measured child nodes

runs?MeasuredTextRun[]

Text runs the node lays out

Lengths are in device pixels. A 20px box measures 40 with a devicePixelRatio of 2.

Text runs

A node that lays out text reports one MeasuredTextRun per shaped run.

Prop

Type

Description

text?string

The run's text

x?number

Left edge, from the content box of the node that owns the run

y?number

Top edge, from the content box of the node that owns the run

width?number

Run width

height?number

Run height

Coordinates

transform is absolute: it places the box in the image. Every node has one, including the boxes Takumi generates to lay out inline content.

A run's x and y are not absolute. They start at the content box of the node that owns the run. To place a run in the image, add that node's padding and border to the run's x and y, then apply the node's transform.

Last updated on