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.
Starting the 2D lasso overlay.
Inside the lasso
Draw a loop round part of the article.
- Links
- none
- Table
- none
- Figure
- none
Show the code lasso sampling, caret hits and the dimmer
- 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 withleft,top,widthandheight. - 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 toelementsFromPoint(), which gives the link, table cell or figure under the point. - Dimmer and shimmer rim. Outside the loop a
saturationfill takes the colour out and a radial gradient with ten hard steps darkens it. A conic gradient strokes the rim and turns over time. - Size and hit sync. Copies the canvas box into
--card-wand--card-h, then callsupdateElementGeometry()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
- Measured in Chrome Canary
161.0.8200.0on 2026-12-04 at412x915(DPR2.625, SwiftShader): all 10 live checks passed with zero script errors, zero lost canvases, zero open snapshots and no horizontal overflow, on a924x18062D canvas (1.67 MP,6.4 MB). caretPositionFromPoint()works on text inside a<canvas layoutsubtree>. The seeded loop sent845points to it; all845landed in the article's text,609inside a word's own box, and the words came back as1,284 moths of 96 speciesin8.7ms. A Range walk over all117words found the same5(Jaccard1.00).- A child of the canvas is not hit-testable until the canvas has drawn it: before the first
drawElementImage(), and inside the firstpaintevent, both calls returned the<canvas>. Straight after the drawelementsFromPoint()found the paragraph, butcaretPositionFromPoint()still returned the canvas for 1 to 3 more frames. I seed the loop after the first draw and retry when the caret route comes back empty. - A loop drawn round the
212cell came back asElephant hawk-moth · Count: 212, read from the row and column headings, and a loop round a link gave its URL. Trusted touch and mouse drags through DevTools input gaveLarge yellow underwing · Count: 187without scrolling the page. - Outside the loop mean luma fell from
218to87and saturation from0.15to0.04, while inside it stayed at206. The right margin showed20distinct luma levels from the stepped gradient, and the rim covered6of 8 hue buckets. On the page cardelementsFromPoint()hit Reset, Clear and Read at0.00px from where they are drawn (card688px, canvas688px; also0.00px at1280x900). Two reduced-motion frames 0.7 s apart matched in every byte; with motion on,74395bytes differed. I haven't tried it on a phone.