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.

Reading a tide table

A tide table lists the times of high and low water for one harbour. The heights are measured from chart datum, the lowest level the sea is expected to reach.

Two high tides arrive each day, about twelve hours and twenty-five minutes apart. At new moon and full moon the range is largest. These are spring tides.

  • High water: 06:12, 4.8 metres
  • Low water: 12:31, 0.9 metres
  • High water: 18:40, 4.6 metres

Edit this sentence to see the copy under the lens change.

Show the code a converted copy of an element, drawn inside a circle
  1. 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.
  2. 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.
  3. Make a translator. LanguageDetector names the source language and Translator.create() loads the pair. The model may have to be downloaded, which needs a tap first.
  4. 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.
  5. 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.
  6. 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).
  7. Without a model: text recorded in advance. The same provider shape, answering from a table in recorded.js.
  8. Keep the copy in step. A MutationObserver on the original copies and converts again when its text changes.
  9. Moving the lens. Mouse hover, a finger drag that starts on the lens, a tap, and keyboard focus.
  10. The page script: choosing what goes under the lens.
  11. 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.

Tides

Two high tides arrive each day, about twelve hours and twenty-five minutes apart. The range is largest at new moon and full moon, when the sun and the moon pull along the same line.

Knots

A bowline makes a fixed loop at the end of a rope. It does not slip under load, and it can be untied by hand after it has carried a heavy weight.

Lights

Each lighthouse shows its own pattern of flashes, called its character. A chart prints the pattern beside the light, so a navigator can tell two lights apart at night.

Show the code swap a card for its summary by drawn size
  1. Cards and their summaries are all children of the canvas.
  2. 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

ResultExperimentDetail

Findings