Although drawing text with canvas element is not as powerful as fully blown CSS, it can be used to draw and style text as well.

The main methods used for drawing text with canvas are:

  • font - explains the font property that will be used
  • fillText(text,x,y) - fills the given text at the given position indicated by the given coordinates x and y
  • fillStyle - CSS based color for text that call fillText method
  • strokeText(text,x,y) - strokes the given textat the given position indicated by the coordinates as x and y
  • strokeStyle - CSS based color for text written by strokeText method - textBaseline - explains the baseline alignment to be set for the text; possible values are top, hanging, middle , alphabetic, ideographic and bottom

To set the font, size, and style of HTML5 Canvas text, we can set the font property of the canvas context to a string containing the font style, size, and family, delimited by spaces. The style can be normal, italic, or bold. unless otherwise specified, the font style is defaulted to normal.

Example

Example how to create text with canvas element:

 

›› go to examples ››