Text selection effects
The text below is HTML drawn by a canvas. Selecting it is done by the browser as usual, including the Android handles and the copy menu. The page reads the rectangles of the selected text and the canvas draws the highlight. Select some text, then change the effect.
Selecting text that a canvas draws
Press and hold a word on a phone, or drag with a mouse. The browser decides which characters are selected. The canvas only asks where they are, with Range.getClientRects(), and draws something there.
The effect is a picture and nothing else. Copy, the handles, double click to select a word, Shift with the arrow keys and this link to the explainer behave the way they do in any paragraph. Emoji and accents are measured like other text: 👩🚀 café, naïve, fine.
- A selection can start in one list item
- and end in another.
This paragraph is editable. Selections made while typing, with Shift and the arrow keys, get the same effect.
Show the code selection rectangles to a canvas effect
- Wrap ordinary text in
<selection-fx>. The text stays in the light DOM.highlight="found"also treats the ranges of that CSS Custom Highlight as selected. - The page script. It changes the
effectattribute and fills the highlight for the search box. - Inside the element: hide the browser's highlight colour. One
::selectionrule with a transparent background. The selection itself is untouched. - Find the selected text. One
Rangeper text node inside the selection,getClientRects()on each, then rectangles on the same line are joined. - Convert to the element's coordinates on every
selectionchange, and find the end of the selection that is moving. - A 2D effect. It draws the element once dimmed, then draws it again clipped to each rectangle, scaled, on a plate with a shadow.
- For shaders, the rectangles become a small mask picture. Red is selected now, green was selected in the last 1.2 seconds, blue is the selection blurred.
- A shader effect, in GLSL for WebGL and WGSL for WebGPU.
tex()is the element,sel()the mask,ink()how much glyph is at a point. - The WebGL frame.
texElementSubImage2Dfor the element,texImage2Dfor the mask, one triangle. - The WebGPU frame.
queue.drawElementImageToTexture()for the element,copyExternalImageToTexture()for the mask. - When the canvas loses its context, move the content to a new 2D canvas.
- The whole element.
Use it
<script type="module" src="https://html-in-canvas.page/lib/selection-fx.js"></script>
<selection-fx effect="burn" style="--selection-fx-color: #ff5a00">
<p>Any markup. Select some of it.</p>
</selection-fx>
Effects: marker, lift, loupe, trail, focus, highlight (2D canvas), glow, burn, wave (shader), none. Add your own with defineSelectionEffect(name, { draw }) for 2D or { glsl, wgsl } for a shader; the inputs are listed at the top of selection-fx.js. Without HTML-in-canvas the element is a plain <slot> and the browser's highlight is shown.
Selection on a rotated element
This canvas draws one paragraph with a rotation and a scale. Select text in it. The browser's highlight is part of the element's picture, so it's rotated with the text.
Range.getClientRects() returns upright rectangles: the bounding box of each rotated line (pink).
To get rectangles in the element's own coordinates, the page sets the element's geometry to the identity with updateElementGeometry(), measures, and puts the geometry back, all in one task. Drawn through the same rotation they fit the lines (green).
Show the code rectangles of a rotated element
- A canvas with one drawable paragraph.
- Draw it rotated.
drawElementImage()also moves hit testing, so dragging on the rotated text selects the right characters. - Measure in page coordinates and in element coordinates.
Live checks
These start when you press the button, because several of them create WebGL and WebGPU contexts and a phone allows only a few. They run on small fixtures in a corner of the window. They don't change the selection if you have text selected: the checks that need a real selection wait until nothing is selected.
Findings
Measured in headless Chromium 157 on Linux with a software renderer, 2026-11-21. The last list says what that browser couldn't show.
- The browser's selection highlight is part of the element's picture. It is in the pixels that
drawElementImage(),texElementSubImage2D()and a canvas read-back return, in the active colour, and it's transformed with the element. paintfires on every change of the selection, because the highlight changes the element's rendering. A 30-step mouse drag fired 19 paint events. Noselectionchangelistener is needed just to redraw. One is needed to measure.::selectionstyles apply. A custom background and colour are drawn.background-color: transparentwith nocolorleaves the picture identical to the unselected one, pixel for pixel, while the selection still exists. That's how<selection-fx>removes the browser's highlight and draws its own.::highlight()from the CSS Custom Highlight API is drawn too, and changing the registry firespaint. A Highlight with no style is invisible and still gives ranges to measure, so search matches can drive the same effects (the Find box above).Range.getClientRects()follows the drawn position, to within a pixel, when the element is drawn with a translation. The rectangles are in page coordinates as usual.- Under a rotation the rectangles are bounding boxes. There is no quad version (
getBoxQuads()isn't in Chromium), so the shape of a rotated line is lost.updateElementGeometry(el, { canvasTransform: identity })changes whatgetClientRects()returns synchronously, so measuring under the identity and restoring the transform in the same task gives element-local rectangles without a visible change. range.getClientRects()on a whole selection also returns the boxes of block elements that are fully inside it, which would highlight a whole paragraph box. Measuring one range per text node avoids that.- Hit testing for selection follows the drawn geometry. Mouse drag, double click and
caretPositionFromPoint()pick the right characters on an element drawn rotated and scaled. Dragging from one drawable element to another, and from outside the canvas into it, selects across them in document order. - Selection doesn't cross into a
contenteditablechild when the drag starts outside it. That's the usual editing-boundary rule, not something the canvas adds. - The caret is drawn, and each blink fires
paint(4 events in 2.2 seconds with a focused editable element). A shader effect therefore uploads the element's texture twice a second while a caret is showing. - Text selected inside an
<input>or<textarea>is highlighted in the picture but can't be measured.getSelection()has no range inside the control and there's no rectangle API forselectionStart/selectionEnd, so these effects don't apply to form controls. - Releasing a WebGL context can crash the tab.
WEBGL_lose_context.loseContext()on a drawable canvas that is still in the document and holds an element texture crashed the renderer in Chromium 157. Removing the canvas from the document first does not. A WebGL canvas also reports loss withwebglcontextlost, not thecontextlostevent of a 2D canvas. - Effects are visual only.
<selection-fx>leaves the element's geometry at its layout position, so the caret, the handles and hit testing stay on the line even while the wave effect moves the glyphs by 4 px. - A shader needs to know which pixels are glyphs. The slotted content has a transparent background, so the element texture's alpha is the glyph coverage. For text on its own opaque background the shaders compare against
--selection-fx-bg. - The rectangles reach a shader as a mask texture drawn with a 2D
OffscreenCanvas, not as a uniform array, so the count of lines isn't limited and the mask can carry a blurred copy and a fading history at no shader cost.
Not verified here, needs a real device:
- Touch selection. Long press didn't select anything in the headless test browser, inside or outside a canvas, so press and hold, the Android handles and the copy menu are untested. Things to look at on a phone: whether the handles appear at the drawn position of a rotated line, whether they still appear when
::selectionis transparent, and whether the loupe effect is hidden under the finger or under Chrome's own magnifier. ::target-text(text fragment links), find-in-page marks and spelling marks. The headless browser showed none of them even outside a canvas. The explainer says spelling marks and find-in-page marks are left out of an element's picture for privacy. This link scrolls to a text fragment in the demo above: if the browser marks it, the mark is either in the canvas (drawn with the text) or missing.- Why a canvas lost its context on an Android phone. The first version of this page started on the glow shader and ran its checks on load, and on Chrome 159 the demo was a white box with a broken-picture icon. The cause isn't identified. The page now starts on a 2D effect, creates GPU contexts only when a shader effect is picked or the checks are started, and
<selection-fx>moves its content to a new 2D canvas when its canvas loses its context or shows no picture. The line under the demo says when that happened. - WebGPU on screen. The WGSL of each effect compiles here and an off-screen WebGPU frame is compared with the WebGL one in the checks. The software adapter can't show a WebGPU canvas, so the on-screen route is untested.
- Speed on a phone. Glow takes 24 texture samples per pixel and burn 16.
Ideas not built
- Other people's selections. Send each reader's ranges over a WebSocket, put them in a named Highlight per person and draw each with its own colour and a name tag at the focus point.
highlight="name"already takes the ranges. - Selected glyphs as particles. Typography as particles can lift the glyphs of a range off the line. Joined with this page, deselected text could fall or blow away and return.
- A share card. On selection, draw the selected lines alone on a card with the page title and offer it as an image (capture has the export half).
- Selection in 3D. Extrude the selected rectangles from a page in three.js, or light them, using the same mask as a second texture.
- A heat map of what readers select. Accumulate the mask over many selections and draw it as a faint background.
- A minimap. A small copy of a long article at the side, drawn from the same element, with the selection and search matches marked on it.
- Redaction. Draw solid bars over the selection in the picture that is exported or shared to another tab, while the reader's own view stays readable.
- Translate or define in place. Draw the answer over the selected rectangles, in the same line boxes, while the original text stays in the DOM.
- Sound and haptics. A tick from the Vibration API each time the selection grows by a word.
- Form controls. Rebuild the selection rectangles of an
<input>by measuring a hidden copy of its text, to bring these effects to text fields.