This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The EditContext.updateCharacterBounds() method of the EditContext interface should be called as response to a characterboundsupdate event to inform the operating system about the position and size of the characters in the EditContext object.
The characterboundsupdate event is the only time you need to call the updateCharacterBounds() method.
The character bounds information is then used by the operating system to correctly position the Input Method Editor (IME) window when needed. This is especially important in situations where the operating system can't automatically detect the position and size of the characters, such as when rendering text in a <canvas> element.
updateCharacterBounds(rangeStart, characterBounds)
rangeStartA number representing the start of the range of text for which character bounds are provided.
characterBoundsAn Array containing DOMRect objects representing the character bounds.
None (undefined).
TypeErrorThrown if the method is called with less than two arguments, or if the first argument is not a number or the second argument is not an iterable (like an array).
Calculating the character bounds and calling updateCharacterBounds synchronously, within the characterboundsupdate event ensures that the operating system has the information it needs when it displays the IME window. If you don't call updateCharacterBounds() synchronously within the event handler, users may observe the IME window being displayed in the wrong position before being moved to the correct position.
The updateCharacterBounds() method should only be called when the operating system indicates that it requires the information, and only for the characters that are included in the current IME composition.
The event object passed to the characterboundsupdate event handler contains a rangeStart and rangeEnd properties that indicate the range of characters that are currently being composed. The updateCharacterBounds() method should only be called for the characters in this range.
This example shows how to use the updateCharacterBounds method to update the character bounds in the EditContext of a <canvas> element when the operating system indicates that it requires the information. Note that the characterboundsupdate event listener callback in this example is only called when using an IME window, or other platform-specific editing UI surfaces, to compose text.
<canvas id="editor-canvas"></canvas>
const FONT_SIZE = 40;
const FONT = `${FONT_SIZE}px Arial`;
const canvas = document.getElementById("editor-canvas");
const ctx = canvas.getContext("2d");
ctx.font = FONT;
const editContext = new EditContext();
canvas.editContext = editContext;
function computeCharacterBound(offset) {
// Measure the width from the start of the text to the character.
const widthBeforeChar = ctx.measureText(
editContext.text.substring(0, offset),
).width;
// Measure the character width.
const charWidth = ctx.measureText(editContext.text[offset]).width;
const charX = canvas.offsetLeft + widthBeforeChar;
const charY = canvas.offsetTop;
// Return a DOMRect representing the character bounds.
return DOMRect.fromRect({
x: charX,
y: charY - FONT_SIZE,
width: charWidth,
height: FONT_SIZE,
});
}
editContext.addEventListener("characterboundsupdate", (e) => {
const charBounds = [];
for (let offset = e.rangeStart; offset < e.rangeEnd; offset++) {
charBounds.push(computeCharacterBound(offset));
}
editContext.updateCharacterBounds(e.rangeStart, charBounds);
});
| Specification |
|---|
| EditContext API> # dom-editcontext-updatecharacterbounds> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
updateCharacterBounds |
121 | 121 | No | 107 | No | 121 | No | 81 | No | 25.0 | 121 | No |
EditContext interface it belongs to.
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/EditContext/updateCharacterBounds