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.

2D canvas Shader (WebGPU, else WebGL) For comparison

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.

Nothing is selected.
Show the code selection rectangles to a canvas effect
  1. 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.
  2. The page script. It changes the effect attribute and fills the highlight for the search box.
  3. Inside the element: hide the browser's highlight colour. One ::selection rule with a transparent background. The selection itself is untouched.
  4. Find the selected text. One Range per text node inside the selection, getClientRects() on each, then rectangles on the same line are joined.
  5. Convert to the element's coordinates on every selectionchange, and find the end of the selection that is moving.
  6. A 2D effect. It draws the element once dimmed, then draws it again clipped to each rectangle, scaled, on a plate with a shadow.
  7. 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.
  8. 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.
  9. The WebGL frame. texElementSubImage2D for the element, texImage2D for the mask, one triangle.
  10. The WebGPU frame. queue.drawElementImageToTexture() for the element, copyExternalImageToTexture() for the mask.
  11. When the canvas loses its context, move the content to a new 2D canvas.
  12. 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).

Select across these lines. On a phone, press and hold a word, then drag a handle. The handles are drawn by the browser on top of the page, so where they appear on a rotated line is something to look at on a real device.

Show the code rectangles of a rotated element
  1. A canvas with one drawable paragraph.
  2. Draw it rotated. drawElementImage() also moves hit testing, so dragging on the rotated text selects the right characters.
  3. 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.

Not verified here, needs a real device:

Ideas not built