Text Manipulation Using Canvas in HTML5

Specifying Text Baseline using Canvas in HTML5

 
We use the textBaseline property to align text in the HTML5 Canvas. The textBaseline property can be aligned to any of the values listed below. By default, the textBaseline property has the "alphabetic" value.
 
There are six options available for specifying the baseline of the text :
  • Top It represents the top of the em square.
  • Hanging It represents a hanging baseline.
  • Middle It represents the middle of the em square.
  • Alphabetic It represents an alphabetic baseline.
  • Ideographic It represents an ideographic baseline.
  • Bottom It represents the bottom of the em square.
Syntax
  1. <script>  
  2.     context.textBaseline = 'middle';  
  3. </script> 
Examples
 
In the following example the first the text is aligned to the top at y=40:
  1. ctx.textBaseline = "top";  
  2. ctx.fillText("Hello", 0, 40); 
Then the text is aligned to the center at y=40:
  1. ctx.textBaseline = "middle";  
  2. ctx.fillText("Hello", 150, 40); 
At last the text is aligned to the bottom at y=40:
  1. ctx.textBaseline = "bottom";  
  2. ctx.fillText("Hello", 300, 40); 
Example
  1. <!DOCTYPE html>  
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <script type="application/javascript">  
  6.         function init() {  
  7.             var canvas = document.getElementById("canvas");  
  8.             if (canvas.getContext) {  
  9.                 var ctx = canvas.getContext("2d");  
  10.                 ctx.textAlign = "start";  
  11.                 ctx.fillStyle = "blue";  
  12.                 ctx.font = "40px verdana";  
  13.    
  14.                 // The top of the text will be aligned at y = 40  
  15.                 ctx.textBaseline = "top";  
  16.                 ctx.fillText("Hello", 0, 40);  
  17.    
  18.                 // The vertical centre of the text will be aligned at y = 40  
  19.                 ctx.textBaseline = "middle";  
  20.                 ctx.fillText("Hello", 150, 40);  
  21.    
  22.                 // The bottom of the text will be aligned at y = 40  
  23.                 ctx.textBaseline = "bottom";  
  24.                 ctx.fillText("Hello", 300, 40);  
  25.             }  
  26.         }  
  27.     </script>  
  28.     <title>Specify text baseline</title>  
  29. </head>  
  30. <body onload="init();">  
  31.     <canvas id="canvas" width="900" height="500"></canvas>  
  32.     <br>  
  33. </body>  
  34. </html> 
Output
 
baseline.jpg
 

Measuring Text in the HTML5 canvas

 
In order to get the text metrics of the canvas we use the measureText() method that measures the width of a given text in pixels and returns a textMetrics object containing the width property.
 
When this method is used a text string is required.
 
Syntax 
  1. <script>  
  2.     var metrics = context.measureText('measure!');  
  3.     var width = metrics.width;  
  4. </script> 
Example
  1. <!DOCTYPE html>  
  2.    
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <script type="application/javascript">  
  7.         function init() {  
  8.             var canvas = document.getElementById("canvas");  
  9.             if (canvas.getContext) {  
  10.                 var ctx = canvas.getContext("2d");  
  11.                 ctx.font = "40px verdana";  
  12.    
  13.                 // You don't have to draw the text on canvas to measure its width. This is only for reference  
  14.                 ctx.fillText("Noida", 0, 40);  
  15.    
  16.                 // The width of the text will vary with font size. The width is in pixels and is returned as a textMetrics object.  
  17.                 var textWidth = ctx.measureText("Noida");  
  18.    
  19.                 // The width of the text is contained in the textMetrics object.width  
  20.                 alert("Text Width: " + textWidth.width);  
  21.             }  
  22.         }  
  23.     </script>  
  24.     <title>Measuring Text </title>  
  25. </head>  
  26. <body onload="init();">  
  27.     <canvas id="canvas" width="900" height="500"></canvas>  
  28.     <br>  
  29. </body>  
  30. </html> 
Output
 
measuring.jpg

Specifying Text Alignment

 
We can use the text alignment property of the Canvas to set the alignment of the text. If we do not specify any alignment then by default it will be "start".
 
Text alignment can be done in five different ways:
  • Start The text begins at the location specified in the strokeText() or fillText() method and text direction is from left to right (ltr).
  • Left The text is aligned to the left. This is similar to using "start". The text is aligned to the left, with the starting point at the location specified in the strokeText() or fillText() method. 
  • End The text ends at the location specified in the strokeText() or fillText() method and the text direction is from right to left (rtl).
  • Right This is similar to using "end". The resulting text will end at the point specified in the strokeText() or fillText() method. It means that the text is aligned to the right. 
  • Centre The text is aligned to the center. The center of the text will be aligned with the point specified in the strokeText() or fillText() method.
Syntax
  1. <script>  
  2.     context.textAlign = 'center';  
  3. </script> 
Example
  1. <html>  
  2. <head>  
  3.     <script type="application/javascript">  
  4.         function init() {  
  5.             var canvas = document.getElementById("canvas");  
  6.             if (canvas.getContext) {  
  7.                 var ctx = canvas.getContext("2d");  
  8.    
  9.                 // Specify the font size and font name  
  10.                 ctx.font = "15px verdana";  
  11.    
  12.                 //The word "Hello" will begin at (100,20)  
  13.                 ctx.textAlign = "start";  
  14.                 ctx.strokeText("Hello Start", 100, 20);  
  15.    
  16.                 //The word "Hello" will end at (100,20)  
  17.                 ctx.textAlign = "end";  
  18.                 ctx.strokeText("Hello End", 100, 40);  
  19.    
  20.                 //The word "Hello" will begin at (100,20)  
  21.                 ctx.textAlign = "left";  
  22.                 ctx.strokeText("Hello Left", 100, 60);  
  23.    
  24.                 //The word "Hello" will center at (100,20)  
  25.                 ctx.textAlign = "center";  
  26.                 ctx.strokeText("Hello Center", 100, 80);  
  27.    
  28.                 //The word "hello" will begin at (100,20)  
  29.                 ctx.textAlign = "right";  
  30.                 ctx.strokeText("Hello Right", 100, 100);  
  31.    
  32.                 // Display the text at the specified location. In this case (5,20)  
  33.                 ctx.strokeText("Hello", 5, 20);  
  34.             }  
  35.         }  
  36.     </script>  
  37.     <title>Text Alignment </title>  
  38. </head>  
  39. <body onload="init();">  
  40.     <canvas id="canvas" width="900" height="500"></canvas>  
  41.     <br>  
  42. </body>  
  43. </html>   
Output
 
textalign.jpg