Circle to search on a live article

I wanted to know if a lasso drawn over an article inside a <canvas layoutsubtree> can dim everything outside the loop and then read the words, links and table values inside it straight out of the page with caretPositionFromPoint() and elementsFromPoint(), with no text recognition. Draw a loop round anything in the article.

seeded lasso

Field notes · sample text for this demo

Moth night at Wicken Fen

1,284 moths of 96 species

That was the catch at the light trap by the lode over one warm July night, the best count since the survey began. Most came between 23:00 and midnight, when the wind dropped.

Top five species at the trap
SpeciesCountPeak
Elephant hawk-moth21223:00
Large yellow underwing18700:00
Drinker9623:00
Garden tiger4101:00
Fen wainscot3322:00
88214402331249 21:0022:0023:0000:0001:00
Moths per hour at the trap, 1,284 in all.

The elephant hawk-moths came in pairs. The next survey is on 14 August; see the Wicken Fen reserve page.

Pointer mode

Starting the 2D lasso overlay.

Inside the lasso

Draw a loop round part of the article.

Links
Table
none
Figure
none

No lasso yet.

Show the code lasso sampling, caret hits and the dimmer
  1. An ordinary article inside the canvas. Paragraphs, a <table class="data">, an SVG figure with a caption, links and the lasso controls, on a transparent card sized from JS with left, top, width and height.
  2. Reading the page under the loop. Points on a grid inside the polygon go to caretPositionFromPoint(), which gives a text node and offset that I grow into a word, and to elementsFromPoint(), which gives the link, table cell or figure under the point.
  3. Dimmer and shimmer rim. Outside the loop a saturation fill takes the colour out and a radial gradient with ten hard steps darkens it. A conic gradient strokes the rim and turns over time.
  4. Size and hit sync. Copies the canvas box into --card-w and --card-h, then calls updateElementGeometry() only when the unit or buffer size changes.

How it works

The idea comes from Android's Circle to Search. On a phone it is a system overlay: it freezes the screen, you scribble round something, the rest dims and the picked region shimmers while the phone works out what is in the picture. Here the picture is a live article in a <canvas layoutsubtree>, drawn each frame with drawElementImage(), and the text under the loop is still text.

When the loop closes I lay a grid of points over its bounding box and keep the points inside the polygon. Each one goes to document.caretPositionFromPoint() (or caretRangeFromPoint() where that is missing), and I grow the caret offset into a whole word. A word only counts if the point lies inside the word's own box, because the caret call snaps to the nearest text even from blank space. The same points go to document.elementsFromPoint(), which names the link, the table cell or the figure underneath. A table cell is reported with its row heading and column heading, so a loop round a number says what the number is.

The caret calls only see what is on screen. As a second route I walk every text node in the article, split it into words, measure each word with a Range and keep the ones whose centre is inside the polygon. That works off screen too, and the live checks compare the two.

The overlay is plain 2D canvas. Outside the loop a grey fill with globalCompositeOperation = 'saturation' takes the colour out of the article, then a radial gradient with ten hard steps darkens it more the further it is from the loop. The rim is a wide soft white glow, a conic gradient that turns slowly, and a thin dashed line that runs round it. The words that were found get a pale highlight behind the text. Lasso mode takes the pointer for drawing; Read mode gives it back so the links and text selection work. On a touch screen the canvas fills most of the screen, so the page starts in Read mode and Lasso lasts for one loop: tap Lasso, draw, and the page scrolls again.

Live checks

These checks wait for a tap. The pixel checks copy the page canvas once into a plain canvas and read the copy. The hit and caret checks scroll the demo into view, probe and scroll back in the same task, so no frame is painted scrolled.

Findings