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.
import { } from "takumi-js";
const { width, } = await (< ="text-xl">I'm a text 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:
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 .(, { }); Returned fields
measure() resolves to a MeasuredNode.
Prop
Type
Description
width?numberLaid-out width
height?numberLaid-out height
transform?[number, number, number, number, number, number]Matrix that places the box in the image
children?MeasuredNode[]Measured child nodes
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?stringThe run's text
x?numberLeft edge, from the content box of the node that owns the run
y?numberTop edge, from the content box of the node that owns the run
width?numberRun width
height?numberRun 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