Text measured by CSS for signs, labels and particles
Type in the field at the bottom of the picture. It's a real <input> drawn by the canvas. Each character you type
grows into place and a copy flies up to the sign, and each character you delete breaks into pieces. The canvas knows where each
character is from input.createValueRange(). The sign, the book spines and the ticker are elements sized by
text-fit and text-box-trim, drawn at the size CSS gave them.
Show the code CSS sizes the labels, OpaqueRange finds the characters
- The markup. One canvas. The labels and the input are its children.
- The labels, in CSS. No script measures them.
- Drawing a label. The element's own size is the size of the texture.
- One rectangle per character of the input.
- What changed. On each
inputevent the old and new values are compared. Deleted characters are cut out of the last picture of the field. - Drawing the characters. Each piece is a part of the field's own picture, drawn again somewhere else.
- Without
createValueRange(). The same rectangles frommeasureText(). - The whole module.
How it works
- The picture is one 2D canvas. Every label and the input are drawn with
drawElementImage()at theiroffsetWidthandoffsetHeight. - The sign is 250 px wide in CSS.
text-fit: growscales whatever you type to that width, andtext-box: trim-both cap alphabeticmakes its box end at the top of the capitals and at the baseline. "Show boxes" outlines each element's box. Turn "Trim" off to see the box that the same text has without it. - The book spines use
writing-mode: sideways-lr. Each is 132 px along the spine, andtext-fitpicks the font size, so a short title is drawn large and its book is thick. - After each frame in which the field changed, the field's part of the canvas is copied to a second canvas and one rectangle per character is read. A deleted character is cut from that copy as 4 px pieces. A new character is drawn again from the copy: its dark pixels become a coloured, see-through picture of the letter, which is drawn large over the field and once more on a path to the sign.
- The effects draw over the field. The field itself is drawn where the canvas says it is, so a tap focuses it and the caret and selection are the browser's.
Live checks
Each check builds a small canvas of its own in a corner of the window. The picture above isn't touched.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157 with software rendering. The rows above say what your browser does.
- Works:
text-fitin the picture. The fitted size is what is drawn, and it follows edits and a CSS animation offont-weightwith no script. The element's height follows the fitted size, so a short text makes a tall label. This page caps the sign's drawn height at 96 px by scaling the draw. - Works:
text-box-trimmakes the box the ink. Withcap alphabeticthe first row of the element's picture has ink in it. Descenders and accents fall outside the box and are cut off by the element's picture, which is the stated behaviour of the property and matters for a texture. - Works:
writing-mode: sideways-lrwithtext-fit. The fitted axis is the vertical one. - Works:
createValueRange()rectangles of a drawn input. One call per character. The check compares them withmeasureText()and reports the difference. - Limit: the pieces are pictures. A character in flight is a copy of pixels. The text that can be selected and edited is the field's. This differs from /particles/, where the hit box follows the letters.
- Not verified here: the on-screen keyboard on a phone (whether the field stays in view when the keyboard opens, and input from predictive text, which can replace a whole word in one event), and whether
createValueRange()exists in Chrome 159 on Android. The status line under the picture says which method is in use.