Skip to content
Related Articles

Related Articles

HTML | canvas textBaseline Property

Improve Article
Save Article
  • Last Updated : 30 Jul, 2019
Improve Article
Save Article

The HTML canvas textBaseline property is used to set or return the baseline of current 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 on to 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 display 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>             

Example 2: This example display 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>          

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

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!