Open In App

HTML canvas textBaseline Property

Last Updated : 12 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The HTML canvas textBaseline property is used to set or return the baseline of canvas text. This property is used to set the font baseline alignment in HTML. Basically, this property is used to control the vertical alignment of text to draw onto the canvas. 

Syntax:

context.textBaseline="alphabetic|top|hanging|middle|ideographic|bottom";

Property Values:

  • alphabetic: This is the default property value. It specifies that the text baseline is the normal alphabetic baseline.
  • top: This property is used to specify the text baseline is on the top of the em square.
  • Hanging: This property is used to specify the text baseline is the hanging baseline.
  • middle: This property is used to specify the text baseline is the middle of the em square.
  • ideographic: This property is used to specify the text baseline is the ideographic baseline.
  • bottom: This property is used to specify the text baseline is the bottom of the bounding box.

Example 1: This example displays the top, middle and ideographic properties. 

html




<!DOCTYPE html>
<html>
  
<head
    <title
        HTML canvas textBaseline Property
    </title
</head
  
<body>
    <center
        <h1 style="color:green"
            GeeksforGeeks 
        </h1
          
        <h2>HTML canvas textBaseline Property</h2
  
        <canvas id="GFG" width="300" height="300">
        </canvas>
      
        <script>
            var doc_id = document.getElementById('GFG');
            var context = doc_id.getContext('2d');
            var b = ['top', 'middle', 'ideographic'];
            context.font = '20px Arial';
            context.strokeStyle = 'green';
              
            b.forEach(function (b, index) {
                context.textBaseline = b;
                var y = 60 + index * 60;
                context.beginPath();
                context.moveTo(0, y + 1);
                context.lineTo(550, y + 1);
                context.stroke();
                context.fillText('GeeksforGeeks (' + b + ')', 0, y);
            });
        </script>
    </center>
</body>
  
</html>             


Output:

Example 2: This example displays the hanging, alphabetic, and bottom properties. 

html




<!DOCTYPE html>
<html>
  
<head
    <title
        HTML canvas textBaseline Property
    </title
</head
  
<body>
    <center
        <h1 style="color:green"
            GeeksforGeeks 
        </h1
          
        <h2>HTML canvas textBaseline Property</h2
  
        <canvas id="GFG" width="300" height="300">
        </canvas>
          
        <script>
            var doc_id = document.getElementById('GFG');
            var context = doc_id.getContext('2d');
            var b = ['hanging', 'alphabetic', 'bottom'];
              
            context.font = '20px Arial';
            context.strokeStyle = 'green';
              
            b.forEach(function (b, index) {
                context.textBaseline = b;
                var y = 60 + index * 60;
                context.beginPath();
                context.moveTo(0, y + 1);
                context.lineTo(550, y + 1);
                context.stroke();
                context.fillText('GeeksforGeeks (' + b + ')', 0, y);
            });
        </script>
    </center>
</body>
  
</html>          


Output:

Supported Browsers: The browsers supported by HTML canvas textBaseline Property are listed below:

  • Google Chrome
  • Internet Explorer 9.0
  • Firefox
  • Opera
  • Safari


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads