/

HTML / CSS / JavaScript Tutorial

JavaScript canvas2d object property: textBaseline

[this page | pdf | back links]

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

 

It can take the following values: alphabetic (default), top, hanging, middle, ideographic, bottom. These are not always interpreted in the same manner in all browsers.

 

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","420");
x2.setAttribute("height","120");
x2.setAttribute("style","border: 1px solid black");
var c2 = x2.getContext("2d");
c2.strokeStyle = "red";
c2.moveTo(10,60);
c2.lineTo(410,60);
c2.stroke();
c2.fillStyle = "blue";
c2.font = "12px Verdana";
c2.textBaseline = "alphabetic"; c2.fillText("alphabetic", 10, 60);
c2.textBaseline = "bottom"; c2.fillText("bottom", 80, 60);
c2.textBaseline = "hanging"; c2.fillText("hanging", 150, 60);
c2.textBaseline = "ideographic"; c2.fillText("ideographic", 200, 60);
c2.textBaseline = "middle"; c2.fillText("middle", 270, 60);
c2.textBaseline = "top"; c2.fillText("top", 340, 60);
document.getElementById("element").appendChild(x2);
</script>

</body>
</html>


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


Desktop view | Switch to Mobile