files/en-us/web/api/canvasrenderingcontext2d/textalign/index.md
{{APIRef("Canvas API")}}
The
CanvasRenderingContext2D.textAlign
property of the Canvas 2D API specifies the current text alignment used when drawing
text.
The alignment is relative to the x value of the
{{domxref("CanvasRenderingContext2D.fillText", "fillText()")}} method. For example, if
textAlign is "center", then the text's left edge will be at
x - (textWidth / 2).
Possible values:
"left"
"right"
"center"
"start"
"end"
The default value is "start".
This example demonstrates the three "physical" values of the textAlign
property: "left", "center", and "right".
<canvas id="canvas"></canvas>
const canvas = document.getElementById("canvas");
canvas.width = 350;
const ctx = canvas.getContext("2d");
const x = canvas.width / 2;
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvas.height);
ctx.stroke();
ctx.font = "30px serif";
ctx.textAlign = "left";
ctx.fillText("left-aligned", x, 40);
ctx.textAlign = "center";
ctx.fillText("center-aligned", x, 85);
ctx.textAlign = "right";
ctx.fillText("right-aligned", x, 130);
{{ EmbedLiveSample('General_text_alignment', 700, 180) }}
This example demonstrates the two direction-dependent values of the
textAlign property: "start" and "end". Note that
the {{domxref("CanvasRenderingContext2D.direction", "direction")}} property is manually
specified as "ltr", although this is also the default for English-language
text.
<canvas id="canvas"></canvas>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.font = "30px serif";
ctx.direction = "ltr";
ctx.textAlign = "start";
ctx.fillText("Start-aligned", 0, 50);
ctx.textAlign = "end";
ctx.fillText("End-aligned", canvas.width, 120);
{{ EmbedLiveSample('Direction-dependent_text_alignment', 700, 180) }}
{{Specifications}}
{{Compat}}