The CanvasRenderingContext2D.letterSpacing
property of the Canvas API specifies the spacing between letters when drawing text.
This corresponds to the CSS letter-spacing
property.
The CanvasRenderingContext2D.letterSpacing
property of the Canvas API specifies the spacing between letters when drawing text.
This corresponds to the CSS letter-spacing
property.
The letter spacing as a string in the CSS length data format. The default is 0px
.
The property can be used to get or set the spacing. The property value will remain unchanged if set to an invalid/unparsable value.
In this example we display the text "Hello World" three times, using the letterSpacing
property to modify the letter spacing in each case. The spacing is also displayed for each case, using the value of the property.
html
<canvas id="canvas" width="700"></canvas>
js
const canvas = document.getElementById("canvas"); const ctx = canvas.getContext("2d"); ctx.font = "30px serif"; // Default letter spacing ctx.fillText(`Hello world (default: ${ctx.letterSpacing})`, 10, 40); // Custom letter spacing: 10px ctx.letterSpacing = "10px"; ctx.fillText(`Hello world (${ctx.letterSpacing})`, 10, 90); // Custom letter spacing: 20px ctx.letterSpacing = "20px"; ctx.fillText(`Hello world (${ctx.letterSpacing})`, 10, 140);
Specification |
---|
HTML Standard # dom-context-2d-letterspacing |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
letterSpacing |
99 | 99 | 115 | No | 85 | No | 99 | 99 | 115 | 68 | No | 18.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/letterSpacing