Open In App

HTML canvas textBaseline Property

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:

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






<!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. 




<!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:


Article Tags :