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.

Notes drawn by the canvas

Ruby text is a small annotation above a word. Japanese print uses it for readings: 東京とうきょう is Tokyo, 漢字かんじ are Chinese characters, 振り仮名ふりがな is the annotation itself and 日本語にほんご is the language. Chinese uses pinyin: 北京Běijīng, 上海Shànghǎi.

A canvas that draws an element can draw it twice, at any size.

The canvas element came from Apple in 2004.1 Drawing HTML into one was not possible done with an SVG image added as drawElementImage(), which Chrome has behind a flag.2 Text with this background has the colour of the selection.3

 

Show the code six effects from one element's picture
  1. 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.
  2. The styles. The selection colour, and no browser line on s, del and ins inside the canvas.
  3. Read the rectangles first. Every box is read before the first partial draw of the frame.
  4. On paint. The order of the layers.
  5. Pull quote. One source rectangle of the article, drawn larger at low alpha.
  6. Selection glow, the filter. An SVG filter used as ctx.filter.
  7. Selection glow, the draw. The article drawn through the filter, or blurred rectangles of the Range.
  8. Ruby. The article is drawn with the rt boxes clipped out. Boxes near the pointer are drawn back.
  9. Kinetic heading. One piece of the picture for each glyph box.
  10. Pen strokes. Along the line boxes of s, del and ins.
  11. Footnotes. A click on the link is cancelled and the note element is drawn beside the line.
  12. 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 each rt within 130 px of the pointer the page draws that box of the article again with drawElementImage() and a source rectangle, at an alpha that falls with distance. The rt elements 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 from Range.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, del and ins have text-decoration: none inside 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