Word-by-word highlighting from SpeechSynthesis boundary events
Press Speak and the browser reads the paragraph aloud. Each boundary event names the word being started, and the canvas draws an
effect on that word. Tap any word to start from it. If the voice sends no boundary events, the words are stepped by a timer and the line under
the buttons says so.
Nothing has been spoken yet.
Show the code boundary event, range, rectangle, effect
- The markup. One
drawablearticle in a canvas. - Speak and listen for
boundary.charIndexis the position of the word in the utterance text.charLengthis missing from some engines. - Position to rectangle. A
Rangeover the word andgetClientRects(), made relative to the article. - Draw. The paragraph dimmed, the spoken part at full ink through a clip, then the effect on the current word.
- The magnified copy. The nine-argument
drawElementImage()takes the word's rectangle as its source. A last draw with an empty clip puts the hit-test region back on the paragraph. - Tap a word.
caretPositionFromPoint()gives the text offset under the finger. - The CSS. Including the two
::highlight()rules of the fourth effect. - The whole speech file. With the timer used when no boundary events arrive.
How it works
speechSynthesis.speak()takes aSpeechSynthesisUtterance. While it speaks, the utterance firesboundaryevents withcharIndex, and in newer enginescharLengthand the namewordorsentence.- The page makes a
RangefromcharIndexto the end of the word and readsgetClientRects(). The rectangles are relative to the article, so the canvas can use them whatever size it draws the article at. - The canvas draws the article three times: once at 32% opacity, once clipped to the rectangles of the words already spoken, and once for the current word with the chosen effect. The spotlight's rectangle moves a fraction of the way to the new word on every frame.
- The lens draws only the word's rectangle, 1.7 times larger. A draw call also moves the element's hit-test region, so after it the paragraph couldn't be tapped where it appears. The handler ends with one more full-size draw inside an empty clip, which draws nothing and restores the region.
- The fourth effect draws nothing extra. Two CSS Custom Highlights are set on the same ranges and the browser paints them into the element's picture.
- A tap calls
document.caretPositionFromPoint(). The offset it returns is moved back to the start of the word, and a new utterance is spoken from there. ItscharIndexvalues are then offsets from that point.
Live checks
Each check builds its own small canvas in a corner of the window. The paragraph above isn't touched. The last check speaks three words at zero volume.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157 headless, 2026-11-21, which has no voices. The rows above and the line under the buttons say what your browser does.
- Works: a boundary event to a drawn word. With boundary events dispatched by hand, the range, its rectangle and the three canvas effects land on the right word.
- Works:
charLengthcan be absent. The word's end is then found in the text fromcharIndex. - Found: a partial draw moves the whole hit-test region. After the nine-argument draw of one word,
elementFromPoint()at another word answered with the canvas, not the article. A final full-size draw inside an empty clip puts it back and adds no pixels. - Works:
caretPositionFromPoint()inside a drawn element. It returns the text node and the offset of the word under the point. - Works: Custom Highlights are in the picture. A change to a
Highlightis drawn afterrequestPaint(). - Not run here: real speech. The test browser has no voices and
speak()ends with the errorsynthesis-failed. The page then steps the words by a timer, which is the same path a voice without boundary events takes. - Not verified: whether the voices of Chrome 159 on Android fire
boundaryevents, whether they carrycharLength, and how far the events lead or trail the sound. Press Speak and read the line under the buttons: it shows the count of events and the time to thestartevent. The last live check does the same with three words.