The CanvasRenderingContext2D.textAlign property of the Canvas 2D API specifies the current text alignment being used when drawing text. Beware that the alignment is based on the x value of the fillText() method. So if textAlign is "center", then the text would be drawn at x - (width / 2).


ctx.textAlign = "left" || "right" || "center" || "start" || "end";


Possible values:

The text is left-aligned.
The text is right-aligned.
The text is centered.
The text is aligned at the normal start of the line (left-aligned for left-to-right locales, right-aligned for right-to-left locales).
The text is aligned at the normal end of the line (right-aligned for left-to-right locales, left-aligned for right-to-left locales).

The default value is start.


Using the textAlign property

This is just a simple code snippet using the textAlign property to set a different text alignment.


<canvas id="canvas"></canvas>


var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

ctx.font = '48px serif';
ctx.textAlign = 'left';
ctx.strokeText('Hello world', 0, 100);

Edit the code below and see your changes update live in the canvas:


Specification Status Comment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.textAlign' in that specification.
Living Standard  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes123.5 Yes Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes4 Yes Yes Yes

See also

Document Tags and Contributors

Contributors to this page: fscholz, Sheppy, troeggla, nmve, erikadoyle, moscartong
Last updated by: fscholz,