EditContext and inline widgets

In Carets and highlights I left out a canvas-drawn editor using EditContext. I wanted to know if attaching new EditContext() to a drawable <canvas> can drive typing, selection and IME composition while native <input drawable>, <select drawable> and <button drawable> controls sit inline inside the code lines. Click the code to type, drag the widgets, or press Tab to watch focus move between the canvas and its children.

Checking EditContext and drawElementImage() support...
Focus ownernone
Caret / selectionline 1, col 1
IME stateidle
Canvas paints0

controlBounds: waiting for layout

Interact with the editor or inline widgets to see focus, keydown, widget and EditContext events.
Show the code EditContext, IME bounds, inline widgets and checks
  1. The canvas and its inline drawable children. The <canvas> takes tabindex="0", content="drawable" and layoutsubtree; each inline control carries drawable.
  2. Attach EditContext to the canvas and wire textupdate, textformatupdate, compositionstart, compositionend and characterboundsupdate.
  3. Report viewport rectangles for IME with updateControlBounds(), updateSelectionBounds() and updateCharacterBounds().
  4. Draw the inline controls beside their code literals with five-argument drawElementImage(), toggle data-hit when a widget moves, and set inert when a widget scrolls off screen.
  5. Keep widgets and EditContext in sync with updateText() and guard keydown so keystrokes inside a child widget never get intercepted.
  6. The live check fixtures, run on tap so page load never moves focus.

Comparison: EditContext on a canvas vs a drawable contenteditable child

The other way to edit text in a canvas is to draw a contenteditable element with drawElementImage(), as Carets and highlights does. Type in the box on the left and compare its paint counts with the EditContext editor above.

const tint = "#ff6b35"; Type here to compare paint counts.
paint events: 0 | input events: 0 | compositionstart: 0
Area EditContext on canvas Drawable contenteditable
Text and caret Drawn by your 2D or WebGL code. Laid out and drawn by the browser.
Idle blink paints None while unfocused; JS controls the blink timer when focused. Browser fires paint twice a second while focused unless caret-animation: manual is set.
Inline controls Separate drawable siblings placed with drawElementImage(). Inline DOM nodes inside the editable subtree.
IME candidate box Your code reports viewport rects via updateSelectionBounds() and updateCharacterBounds(). Browser computes rects from the DOM selection.
Accessibility tree Canvas text isn't exposed to screen readers; inline widgets keep their native roles. Browser exposes text, caret and selection natively.

How it works

Assigning canvas.editContext = new EditContext({ text, selectionStart, selectionEnd }) turns the focusable <canvas tabindex="0"> into an editable text host without a hidden <textarea>. Printable characters, Backspace, Delete and IME composition arrive as textupdate events on the EditContext, while textformatupdate gives underline ranges for active compositions. Navigation keys (arrows, Home, End) and Enter are handled in the canvas's keydown listener.

Inside the same <canvas content="drawable" layoutsubtree> sit four native HTML controls with the drawable attribute: a colour picker, a range slider, a <select> and a <button>. On each paint event, the canvas draws syntax-highlighted lines, the selection, IME underlines and the live wave strip at the bottom. Then it measures the width of each matching line and calls ctx.drawElementImage(el, x, y, el.offsetWidth, el.offsetHeight) to place each widget right after its literal.

Because Chromium hit-tests each drawable child where drawElementImage() placed it, clicking or tabbing into an inline widget hands focus to that HTML control instead of the canvas. The canvas's pointerdown and keydown listeners bail out whenever e.target !== canvas, so the widgets keep their native keyboard and pointer behaviour. When a widget changes, the script replaces the matching literal in the buffer and calls editContext.updateText() and editContext.updateSelection() so the browser's text model stays in sync.

Live checks

These wait for a tap so loading the page never moves focus. Each check builds its own fixture in the corner and frees its canvas afterwards.

Findings

Not yet run in headless Chromium 157 or on Android Chrome 159. Press the button under Live checks to test this browser.