HTML5CANVAS: Draw text-

Source: Internet
Author: User
We can draw text on HTML5canvas and set the font, size, and color of the text. The font attribute of the 2D context is used to control the font of the drawn text .,.
We can draw text on HTML5 canvas and set the font, size, and color of the text.
The font attribute of the 2D context controls the font of the drawn text. If you need to fill the text with color or make the stroke text, you can use the fillStyle and strokeStyle attributes of the 2D context.

To draw text on the canvas, you can use the fillText () function of the 2D context or the strokeText () function. The following is a simple example:
  1. Var canvas = document. getElementById ("ex1 ");
  2. Var context = canvas. getContext ("2d ");

  3. Context. font = "normal 36px Verdana ";
  4. Context. fillStyle = "#000000 ";
  5. Context. fillText ("HTML5 Canvas Text", 50, 50 );

  6. Context. font = "normal 36px Arial ";
  7. Context. strokeStyle = "#000000 ";
  8. Context. strokeText ("HTML5 Canvas Text", 50, 90 );

The following figure shows the returned results of the above Code:


   Font and Style
When you draw text on HTML5 canvas, you can set the font and style of the text. We can do this through a set of 2D context font attributes. These attributes are compatible with the font setting attributes in CSS:
  1. [Font style] [font weight] [font size] [font face]

For example, we can set the font as follows:
  1. Context. font = "normal 20px Verdana ";

For the above attributes, we can have the following optional values:

Font style values include:
Normal
Italic
Oblique
Inherit

The values of font weight include:
Normal
Bold
Bolder
Lighter
Auto
Inherit
100
200
300
400
500
600
700
800
900

Font size: font size, in pixels.
Ont face: font. For example: verdana, arial, serif, sans-serif, cursive, fantasy, monospace, etc.

Note that not all browsers support all these attributes. In actual use, you need to perform some tests based on the actual situation.

   Draw text
When you draw text in HTML5 canvas, you can draw filled text or stroke text. They are implemented through the fillText () and strokeText () functions in the 2D context. The two functions are defined as follows:
  1. FillText (textString, x, y [, maxWidth]);
  2. StrokeText (textString, x, y [, maxWidth]);

TextString is the text to be drawn.

X and y indicate the position of the text on the canvas. The x parameter is the x axis coordinate of the text, and y is the y axis coordinate of the text, but how it locates on the y axis depends on the text baseline. The text baseline will be described later.

MaxWidth indicates the maximum width of the text in the horizontal direction. For more information, see.

The following is an example code:
  1. Context. font = "36px Verdana ";
  2. Context. fillStyle = "#000000 ";
  3. Context. fillText ("HTML5 Canvas Text", 50, 50 );

   Maximum text width
The optional maxWidth parameter indicates that the maximum width of the text in the horizontal direction cannot be greater than the value specified by the parameter when drawing text on the canvas. If the text width is greater than the value specified by maxWidth, the text width is compressed. Note that it is not cropped. In the following example, two strings of the same text are drawn in the canvas, but different maxWidth attributes are used:
  1. Context. font = "36px Verdana ";
  2. Context. fillStyle = "#000000 ";
  3. Context. fillText ("HTML5 Canvas Text", 50, 50 );
  4. Context. fillText ("HTML 5 Canvas Text", 50,100,200 );

The returned results of the code above are as follows. Note that the second string of text is compressed to 200 pixels in total width:


   Text color
The fill or stroke color of the text is completed through the fillStyle and strokeStyle attributes of the 2D context. The implementation method is the same as that of the image. You can refer to HTML5 Canvas: the introduction in drawing a rectangle, which is not repeated here.

   Measure the text width
In the 2D context, there is a function that can be used to measure the width of the text and return the result value in pixels. This function cannot measure the height. This function is measureText (). The following is an example code:
  1. Var textMetrics = context. measureText ("measure this ");

  2. Var width = textMetrics. width;

By measuring the width of the text, we can know whether the current text can be placed in the current canvas container or for other purposes.

   Text baseline
The text baseline is used to determine how to interpret the y parameter in the fillText () and strokeText () functions. In general, it is how to locate the text in the vertical direction. Note that these interpretations may be slightly different in different browsers.

You can use the textBaseline attribute of the 2D context to set the text baseline.

The syntax of the text baseline is:
  1. Ctx. textBaseline =
  2. "Top" | "hanging" | "middle" | "alphabetic" | "ideographic" | "bottom ";

The following table lists the optional values and descriptions of the text baseline.
  
Value Description
Top The text is aligned based on the highest character in the text.
Hanging The text baseline is hanging baseline ). The value is basically the same as the value of top. In most cases, you may not see any difference.
Middle The text baseline is the text center line
Alphabetic The text baseline is a normal text baseline.
Ideographic The baseline of the text is the bottom of the horizontal font
Bottom The text is aligned based on the lowest character in the text.

After reading the above descriptions, you will be confused. Here is an example. We use the same y value (75) to draw a string of text, but set different baseline values for each text. In the image, we plot an execution of y = 75 to represent the baseline of all text.


The implementation code of the image above is as follows:
  1. Context. stokeStyle = "#000000 ";
  2. Context. lineWidth = 1;
  3. Context. beginPath ();
  4. Context. moveTo (0, 75 );
  5. Context. lineTo (500, 75 );
  6. Context. stroke ();
  7. Context. closePath ();

  8. Context. font = "16px Verdana ";
  9. Context. fillStyle = "#000000 ";

  10. Context. textBaseline = "top ";
  11. Context. fillText ("top", 0, 75 );

  12. Context. textBaseline = "hanging ";
  13. Context. fillText ("hanging", 40, 75 );

  14. Context. textBaseline = "middle ";
  15. Context. fillText ("middle", 120, 75 );

  16. Context. textBaseline = "alphabetic ";
  17. Context. fillText ("alphabetic", 200, 75 );

  18. Context. textBaseline = "ideographic ";
  19. Context. fillText ("ideographic", 300, 75 );

  20. Context. textBaseline = "bottom ";
  21. Context. fillText ("bottom-glyph", 400, 75 );

   Text alignment
The textAlignment attribute of the 2D context defines how the text is aligned horizontally. In other words, the textAlignment attribute defines the x-coordinate attribute of the text when the text is drawn.

The syntax of the textAlignment attribute is:
  1. Ctx. textAlign = "left" | "right" | "center" | "start" | "end ";

The following table lists the values of the textAlignment attribute and their descriptions.
 
Value Description
Start Draw text from the left of x
Left The text is drawn from the left of x, which is the same as the start attribute.
Center X coordinates are in the center of the text
End The x coordinate is at the end of the text.
Right The x coordinate is at the end of the text, which is the same as the end attribute.

The following example shows how to locate the various values of the textAlignment attribute. The x coordinate of a vertical line is x = 250, and the x attribute values of all words are also 250, but their textAlignment attribute values are different.


The implementation code of the above image is as follows:
  1. Context. stokeStyle = "#000000 ";
  2. Context. lineWidth = 1;
  3. Context. beginPath ();
  4. Context. moveTo (250, 0 );
  5. Context. lineTo (250,250 );
  6. Context. stroke ();
  7. Context. closePath ();

  8. Context. font = "16px Verdana ";
  9. Context. fillStyle = "#000000 ";

  10. Context. textAlign = "center ";
  11. Context. fillText ("center", 250, 20 );

  12. Context. textAlign = "start ";
  13. Context. fillText ("start", 250, 40 );

  14. Context. textAlign = "end ";
  15. Context. fillText ("end", 250, 60 );

  16. Context. textAlign = "left ";
  17. Context. fillText ("left", 250, 80 );

  18. Context. textAlign = "right ";
  19. Context. fillText ("right", 250,100 );

This article copyright belongs to the jQuery house, reproduced please indicate the source: http://www.htmleaf.com/ziliaoku/... g/201507172250.html



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.