Marker-pen highlight on mark
The text below is one element drawn by a canvas. Its <mark> elements have no background. The canvas draws a pen stroke for each line that a mark covers, from the boxes
that getClientRects() returns, and draws the text over it. Select some words and press Highlight selection to wrap them in a new <mark>. Tap a mark to redraw it with the chosen pen.
Switch on editing and type: the strokes follow the new line breaks.
Show the code line boxes of a mark, strokes under the text
- The text. An article with
<mark>elements, as a child of the canvas. - The styles. The mark background is off inside the canvas.
--inkcarries the colour. - The boxes. One for each line a mark covers, relative to the article.
- The strokes. Each pen is a list of polylines with a seeded wobble.
- On
paint. Paper, ink, then the article withdrawElementImage(). - Highlight selection. Wrap the selected range in a new
<mark>. The change firespaint.
How it works
The article is a child of a <canvas content="drawable">. On every paint event the page asks each <mark> for its client rects. An inline element that wraps
returns one rect for each line, so a mark over three lines gives three boxes. The page turns each box into strokes for the mark's pen (data-pen), in the colour of its --ink custom property,
and draws them on the canvas. Highlighter and underline strokes are drawn before the article, so the glyphs are on top of the ink. Circle, box and strike strokes are drawn after it.
A stroke is a polyline with small random offsets. The random numbers come from a generator seeded per mark, so the stroke has the same shape on every frame and after an edit. A new mark is drawn in over time:
the page draws only the first part of each polyline and asks for another frame until the stroke is complete. When nothing is animating the page requests no frames. Typing, selecting and resizing fire paint themselves.
The text stays a normal element. It can be selected, copied, edited and found. Highlight selection changes the document: it wraps the selected range in a <mark>, which a screen reader
and a copy to the clipboard both see.
Live checks
These run on their own fixtures in the corner of the window, after a tap on the button.
Findings
mark.getClientRects()on a mark inside a drawn element returns one rect per line, as it does outside a canvas. The rects are reported where the element was last drawn and at the scale it was drawn with. Measured against the element's owngetBoundingClientRect()and divided by the drawn scale, they give layout positions that can be used in the canvas's coordinate system.- Wrapping a range in a new
<mark>, removing a mark and typing in the article each firepaintwith no call from the page. - With
background: nonethe mark paints nothing of its own, so the canvas can put ink under the glyphs by drawing the ink first and the element second. The selection highlight is part of the element's picture, so it's drawn over the ink. - A
Rangereturns the same boxes as a<mark>around the same text. A canvas could draw pen strokes for ranges without changing the document. This page changes the document so that the highlight is in the markup. - Not verified: long-press selection and its handles on Chrome 159 for Android, and the on-screen keyboard while editing. Open the page on the phone, select two words, press Highlight selection, then switch on editing and type.