/

HTML / CSS / JavaScript Tutorial

JavaScript canvas2d object property: textAlign

[this page | pdf | back links]

The textAlign property of the JavaScript DOM object returned by the getContext("2d") method applied to the HTML <canvas> element sets / returns the CSS text-align property for the current text.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
<span id="element"></span>

<script>
var x2 = document.createElement("CANVAS");
x2.setAttribute("width","180");
x2.setAttribute("height","180");
x2.setAttribute("style","border: 1px solid black");
var c2 = x2.getContext("2d");
c2.strokeStyle = "red";
c2.moveTo(90,10);
c2.lineTo(90,170);
c2.stroke();
c2.fillStyle = "blue";
c2.font = "15px Verdana";
c2.textAlign = "start"; c2.fillText("start", 90, 30);
c2.textAlign = "end"; c2.fillText("end", 90, 60);
c2.textAlign = "left"; c2.fillText("left", 90, 90);
c2.textAlign = "center"; c2.fillText("center", 90, 120);
c2.textAlign = "right"; c2.fillText("right", 90, 150);
document.getElementById("element").appendChild(x2);
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile