A lens that translates, simplifies or summarises what is under it
The canvas draws a translated, simplified or summarised copy of this article inside the circle. Move the mouse over it, or drag the circle with a finger. The link, the button and the editable sentence under the circle still work.
Show the code a converted copy of an element, drawn inside a circle
- Two drawable children in one canvas. The article, and an empty element that will hold its copy. Both are absolutely positioned at the same place with the same width.
- Copy the article and pair up the blocks. Each heading, paragraph, list item and button of the original is matched with the same one in the copy.
- Make a translator.
LanguageDetectornames the source language andTranslator.create()loads the pair. The model may have to be downloaded, which needs a tap first. - Convert the copy's text. One text node at a time for translation, so links and bold text stay in place. One block at a time for rewriting and summarising.
- Draw the original, then the copy inside the circle. For each block the lens touches, the copy is drawn once, clipped to the original block's rows and moved so the two blocks start at the same place.
- Or make each block of the copy drawable. Then each block is passed to
drawElementImage()by itself ("each block drawn as its own element" in the menu). - Without a model: text recorded in advance. The same provider shape, answering from a table in recorded.js.
- Keep the copy in step. A
MutationObserveron the original copies and converts again when its text changes. - Moving the lens. Mouse hover, a finger drag that starts on the lens, a tap, and keyboard focus.
- The page script: choosing what goes under the lens.
- The whole library.
Zoom: a card too small to read is replaced by its summary
Three cards drawn at the scale the slider sets. When the body text would be drawn smaller than 9 px, the canvas stops drawing the card and draws a one-line summary of it at 13 px in the same place. The summary is a real element too: tap it to zoom in.
Show the code swap a card for its summary by drawn size
- Cards and their summaries are all children of the canvas.
- Each frame, decide by the drawn text size. The card is drawn scaled. Below 9 px of text the summary is drawn in its place, laid out at the width the card covers on screen.
How it works
drawElementImage() draws a child of the canvas wherever the 2D transform and clip say. The page keeps two children: the article,
and a copy of it made by TextTwin. The original is drawn once at its layout position, which is also
where it receives clicks. The copy is inert and is only drawn inside the lens circle. A translated paragraph rarely has the same
number of lines as the original, so each block of the copy is drawn separately and moved to the top of the block it belongs to.
The text comes from a provider. With Translator, Rewriter or Summarizer in the browser, the provider calls
the on-device model. Without them it answers from a table written in advance for this article, and a block whose text isn't in the table is
hatched in red. The line under the demo says which one is in use.
Live checks
| Result | Experiment | Detail |
|---|
Findings
- A copy drawn inside a clip works as a lens with one 2D context. The original keeps its hit testing because it's drawn at its layout position and the copy is
inert.elementFromPoint()under the lens returns the original's link. - Without alignment the copy drifts. In a 340 px column the recorded German copy starts its third block 24 px lower than the English original and its last blocks 48 px lower (measured by the live check). The "Line up" menu shows it.
- Two ways to line up a block were tried, and both work. The first draws the whole copy once for each block under the lens, clipped to the original block's rows and moved. The second puts the
drawableattribute on every block of the copy and passes each block todrawElementImage()by itself, which is one small draw per block. - A
drawabledescendant is left out of its ancestor's picture. In Chromium 157, once a block of the copy has the attribute, drawing the copy as a whole leaves a hole where the block is. A list item drawn by itself also has no bullet, because the marker is outside its box. So a page picks one of the two ways for a given copy. getBoundingClientRect()couldn't be used to measure the copy. For a child of a drawable canvas it reports where the element was last drawn. The copy is drawn moved and scaled, so the page measures blocks withoffsetTopandoffsetLeft.- "Shrunk to fit" keeps all the text of a longer block but makes it smaller. "By its top" keeps the size and cuts off lines that don't fit. A shorter block leaves empty rows in both.
- Translating one text node at a time keeps inline elements but loses word order across them. "measured from chart datum, the lowest level" is three text nodes. The recorded German puts the verb in the third node, which a per-node model call can't know to do. Rewriting and summarising work on the whole block and return plain text, so the link and the bold word aren't in the copy.
- The built-in AI APIs weren't usable in the test browser. In headless Chromium 157
SummarizerandLanguageModelreport "downloadable",LanguageDetectorreports "unavailable",Rewriteris missing andTranslator.availability()never settles. No model can be downloaded in the cloud container, so the code paths that callTranslator,RewriterandSummarizerhaven't been run. The line under the demo shows what this browser reports. - The recorded Welsh, Cornish, Breton and Manx haven't been checked by a speaker. They were written by a language model for this page, and the Cornish and Manx in particular are likely to hold errors. The page says so under the demo when one of them is chosen. Whether
Translatorhas any of the four on a device isn't verified: when it does not, the lens stays on the recorded text and says why. - Not verified: model download and speed on Android, the quality of per-node translation from a real model, and the drag on a real touch screen.