Annotated article: ruby, pull quote, selection glow, kinetic heading, footnotes, strike-through
The article below is one element drawn by a 2D canvas. The canvas draws parts of the article's picture a second time, or leaves parts out, to make six effects. The heading's glyphs fly in. Ruby annotations show only near the pointer or the last tap. The quote is drawn again behind the text, large and faint, and moves as the page scrolls. Selected text glows. A tap on a footnote number slides the note out beside the line. Struck and inserted words get a pen stroke, and with the checkbox on a tap on a word strikes it.
Show the code six effects from one element's picture
- The article and its notes. Children of the canvas. The notes are separate children so that each can be drawn, and clicked, on its own.
- The styles. The selection colour, and no browser line on
s,delandinsinside the canvas. - Read the rectangles first. Every box is read before the first partial draw of the frame.
- On
paint. The order of the layers. - Pull quote. One source rectangle of the article, drawn larger at low alpha.
- Selection glow, the filter. An SVG filter used as
ctx.filter. - Selection glow, the draw. The article drawn through the filter, or blurred rectangles of the Range.
- Ruby. The article is drawn with the
rtboxes clipped out. Boxes near the pointer are drawn back. - Kinetic heading. One piece of the picture for each glyph box.
- Pen strokes. Along the line boxes of
s,delandins. - Footnotes. A click on the link is cancelled and the note element is drawn beside the line.
- Tap a word.
caretPositionFromPoint()gives the text node under the tap.
How it works
The article is a child of a <canvas content="drawable">. On every paint event the page reads the client rectangles it needs and then draws the layers in this order:
background, pull quote, glow, the article, ruby, heading glyphs, pen strokes, the open footnote. The page asks for frames only while something is moving.
- Ruby near the pointer
- The article is drawn through a clip path that has a hole for the box of every
<rt>, so no annotation is drawn. For eachrtwithin 130 px of the pointer the page draws that box of the article again withdrawElementImage()and a source rectangle, at an alpha that falls with distance. Thertelements stay in the document. - Pull quote behind the column
- The rectangle of the
<blockquote>is drawn before the article, scaled up to about three times its size at 10% alpha and turned by 4 degrees. Its horizontal position follows the scroll position of the page. - Selection that glows
- The browser paints the selection into the article's picture. With "Colour key filter" the page draws the article once more through an SVG filter set as
ctx.filter: a colour matrix keeps pixels near the selection colour, a blur spreads them, and the result is added to the canvas. The filter knows nothing about the selection, so the<mark>with the same colour glows too. With "Range rectangles" the page blurs filled rectangles fromRange.getClientRects(). - Kinetic heading
- While the heading animates, its box is clipped out of the article and each glyph box is drawn as its own piece, with a translation, a rotation and a scale that all end at the glyph's place in the layout.
The
<h2>isn't changed. It keeps its text, its place in the outline and its hit region. - Footnotes that slide out
- Each note is an
<aside>that is a child of the canvas and isn't drawn until its link is tapped. The click on the link is cancelled, and the canvas draws the note under the line (in the margin beside the line on a wide screen), moving in from the right edge. A drawn note has a hit region, so the link inside it works. Without the API the notes are at the foot of the article and the links jump to them. - Strike-through drawn by hand
s,delandinshavetext-decoration: noneinside the canvas. The canvas draws a pen stroke along each of their line boxes: two passes through struck text, an underline and a caret for inserted text. A new element is drawn in over time. "Tap a word to strike it" wraps the word under the tap in a<del>. A second tap removes it.
Live checks
These run on their own fixtures in the corner of the window, after a tap on the button. Two of them set the document's selection for a moment and put it back.
Findings
- A 2D canvas
filterapplies todrawElementImage(), both ablur()function and aurl()reference to an SVG filter. A colour matrix can therefore select pixels of the element's picture by colour with no readback. - The selection highlight is in the element's picture, and a change of selection fires
paintwith no call from the page. - A source rectangle of an element drawn at five times its size is drawn from the recorded picture at that size. The glyph edges are as sharp as at the original size, unlike a scaled copy of the canvas pixels.
- An
<rt>has its own client rectangle. A clip path with a hole for that rectangle removes the annotation and leaves the base text, because ruby text is laid out in its own box above the line. - A drawable child of the canvas has a hit region only in a frame that draws it, and where two children overlap the one drawn last takes the click. A footnote that isn't drawn doesn't take clicks, so a closed note needs no extra step.
caretPositionFromPoint()on a drawn element returns the text node and offset under the point, at the drawn position.- A draw with a source rectangle moves the element's hit region to the piece and gives it the size of the piece.
updateElementGeometry()afterwards moves the region back but keeps the size of the piece: a 120 px heading answered at 6 of 36 points. A whole draw inside an empty clip restores the whole region. The page ends every frame with that draw. The rectangles fromgetClientRects()move with the piece too, so the page reads them all before the first such draw. - After a mouse click inside the article in the test browser, a selection set from script was in
getSelection()but not in the picture until a control outside the canvas was clicked. Cause not found. - Not verified: Chrome 159 for Android. Long-press selection inside the article, the cost of the filter on a phone GPU, and whether a tap near a ruby word is a usable replacement for hover. The status line under the demo prints the time of the last paint.