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.
drawElementImage() unavailable):
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
- The canvas and its inline drawable children. The
<canvas>takestabindex="0",content="drawable"andlayoutsubtree; each inline control carriesdrawable. - Attach
EditContextto the canvas and wiretextupdate,textformatupdate,compositionstart,compositionendandcharacterboundsupdate. - Report viewport rectangles for IME with
updateControlBounds(),updateSelectionBounds()andupdateCharacterBounds(). - Draw the inline controls beside their code literals with five-argument
drawElementImage(), toggledata-hitwhen a widget moves, and setinertwhen a widget scrolls off screen. - Keep widgets and
EditContextin sync withupdateText()and guardkeydownso keystrokes inside a child widget never get intercepted. - 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.
| 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.
- Two text models share one canvas: attaching
EditContextto the<canvas>handles text and IME for the canvas surface, while eachdrawablechild control keeps its own native focus, pointer hit box and keyboard input. - Event guards are needed on the canvas:
keydownandpointerdownbubble from drawable children to the parent<canvas>, so both handlers checke.target !== canvasbefore moving selection or callingpreventDefault(). Taborder stays native: leavingTabunhandled lets focus step from the<canvas tabindex="0">through each drawn inline widget in DOM order. Any widget scrolled out of view getsinert = trueso it doesn't remain an invisible Tab stop.- Moving widgets need a hit-test cache nudge: when typing pushes an inline widget right or down to a new coordinate, toggling a
data-hitattribute on that element clears Chromium's cached hit box. - Not verified yet: real OS IME candidate window alignment (Japanese/Chinese/Korean desktop IME and Android Gboard composition), native
<input type="color">and<select>popups anchored to drawn coordinates, screen-reader announcements, and live check pass/fail in headless Chromium 157 and Android Chrome 159.