Limited availability
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)
Parameters
rangeStart
A number representing the start of the range of text for which character bounds are provided.
characterBounds
An Array
containing DOMRect
objects representing the character bounds.
None (undefined
).
TypeError
Thrown 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);
});
Specifications Browser compatibility See also
EditContext
interface it belongs to.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4