CanvasRenderingContext2D: textRendering property

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The CanvasRenderingContext2D.textRendering property of the Canvas API provides information to the rendering engine about what to optimize for when rendering text.

The values correspond to the SVG text-rendering attribute (and CSS text-rendering property).

Value

A text-rendering hint to the browser engine. This one of:

auto

The browser makes educated guesses about when to optimize for speed, legibility, and geometric precision while drawing text.

optimizeSpeed

The browser emphasizes rendering speed over legibility and geometric precision when drawing text. It disables kerning and ligatures.

optimizeLegibility

The browser emphasizes legibility over rendering speed and geometric precision. This enables kerning and optional ligatures.

geometricPrecision

The browser emphasizes geometric precision over rendering speed and legibility. Certain aspects of fonts — such as kerning — don't scale linearly. For large scale factors, you might see less-than-beautiful text rendering, but the size is what you would expect (neither rounded up nor down to the nearest font size supported by the underlying operating system).

The property can be used to get or set the value.

Examples

In this example we display the text "Hello World" using each of the supported values of the textRendering property. The value is also displayed for each case by reading the property.

HTML

html
<canvas id="canvas" width="700" height="220"></canvas>

JavaScript

js
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.font = "20px serif";

// Default (auto)
ctx.fillText(`Hello world (default: ${ctx.textRendering})`, 5, 20);

// Text rendering: optimizeSpeed
ctx.textRendering = "optimizeSpeed";
ctx.fillText(`Hello world (${ctx.textRendering})`, 5, 50);

// Text rendering: optimizeLegibility
ctx.textRendering = "optimizeLegibility";
ctx.fillText(`Hello world (${ctx.textRendering})`, 5, 80);

// Text rendering: geometricPrecision
ctx.textRendering = "geometricPrecision";
ctx.fillText(`Hello world (${ctx.textRendering})`, 5, 110);

Result

Specifications

Specification
HTML
# dom-context-2d-textrendering

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
textRendering
Chrome – Full support
Chrome 99 (Release date: 2022-03-01)
footnote Full support
Edge – Full support
Edge 99 (Release date: 2022-03-03)
footnote Full support
Firefox – Full support
Firefox 116 (Release date: 2023-08-01)
footnote Full support
Opera – Full support
Opera 85 (Release date: 2022-03-23)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 99 (Release date: 2022-03-01)
footnote Full support
Firefox for Android – Full support
Firefox for Android 116 (Release date: 2023-08-01)
footnote Full support
Opera Android – Full support
Opera Android 68 (Release date: 2022-03-30)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 18 (Release date: 2022-08-08)
footnote Full support
WebView Android – Full support
WebView Android 99 (Release date: 2022-03-01)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support